コード例 #1
0
 internal override void OnDocumentSaved(DocumentSavedEventArgs doc)
 {
     if (doc.DocID > 0 && doc.ImageID > 0)
     {
         docControl.FileName = "";
         Slave.DeleteFile(_filename);
         base.OnDocumentSaved(doc);
     }
 }
コード例 #2
0
 internal override void OnDocumentSaved(DocumentSavedEventArgs doc)
 {
     Controls.DocControl doccont = null;
     if (docControl != null)
     {
         doccont             = docControl;
         docControl.FileName = "";
     }
     Slave.DeleteFile(filename);
     base.OnDocumentSaved(doccont, doc);
 }
コード例 #3
0
        private void cdialog_DialogEvent() //object source, Lib.Win.DialogEventArgs e)
        {
            if (DialogResult != DialogResult.OK)
            {
                return;
            }

            string addFileName = Tag as string;

            if (string.IsNullOrEmpty(addFileName) || !System.IO.File.Exists(addFileName))
            {
                return;
            }
            try
            {
                int  documentID = DocumentID;
                int  imageID    = ImageID;
                bool isPDF      = Environment.IsPdf(addFileName);
                int  pagesCount = PagesCount;
                if (imageID > 0)
                {
                    int page = Page - 1;
                    Kesco.Lib.Win.Document.Dialogs.AddDBDocDialog.AddAndSave(0, imageID, addFileName, page, pagesCount, documentID, 0, 0, Lib.Win.Document.Environment.GetRandomLocalServer(), Kesco.Lib.Win.Document.Dialogs.AddDBDocDialog.TypeSave.SaveAll);
                    Slave.DeleteFile(addFileName);
                }
                else
                {
                    DateTime   creationTime = DateTime.Now;
                    ServerInfo server;
                    string     fName;
                    if (Environment.MoveFile(addFileName, ref creationTime, out fName, out server))
                    {
                        Environment.DocImageData.DocImageInsert(server.ID, fName, ref imageID, ref documentID, 0, "", DateTime.MinValue, "", "", false, creationTime, 0, false, isPDF ? "PDF" : "TIF", pagesCount);
                    }
                }

                if (!string.IsNullOrEmpty(SrcFileName) && !SrcFileName.Equals(addFileName) && System.IO.File.Exists(SrcFileName))
                {
                    try
                    {
                        if (!isPDF)
                        {
                            string tempFileName = Lib.Win.Document.Environment.LibTiff.DeletePart(SrcFileName, SrcStartPage - 1, SrcPagesCount, SrcFileName);
                            if (!string.IsNullOrEmpty(tempFileName) && System.IO.File.Exists(tempFileName))
                            {
                                System.IO.File.Copy(tempFileName, SrcFileName, true);
                                Slave.DeleteFile(tempFileName);
                            }
                        }
                        else
                        {
                            Lib.Win.Document.Environment.PDFHelper.DelPart(SrcFileName, SrcStartPage, SrcStartPage + SrcPagesCount - 1);
                        }
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                Lib.Win.Data.Env.WriteToLog(ex);
            }
        }
コード例 #4
0
        private void dialog_DialogEvent() //object source, Lib.Win.DialogEventArgs e)
        {
            //Dialogs.NewImageAddDialog dialog = e.Dialog as Dialogs.NewImageAddDialog;
            if (string.IsNullOrEmpty(FileName) || DocID <= 0 || DialogResult == DialogResult.Cancel)
            {
                return;
            }
            try
            {
                int    docId       = DocID;
                string newFileName = FileName;

                if (IsSavePart)
                {
                    newFileName = Lib.Win.Document.Environment.GenerateFullFileName(Path.GetExtension(FileName).TrimStart('.'));
                    if (IsPdf)
                    {
                        Lib.Win.Document.Environment.PDFHelper.SavePart(FileName, newFileName, MinPage, MaxPage);
                    }
                    else
                    {
                        Lib.Win.Document.Environment.LibTiff.SavePart(FileName, MinPage - 1, PagesCount, newFileName, null);
                    }
                }

                switch (DialogResult)
                {
                case DialogResult.OK:
                    DateTime   creationTime = DateTime.Now;
                    ServerInfo server;
                    string     fName;

                    if (Lib.Win.Document.Environment.MoveFile(newFileName, ref creationTime, out fName, out server))
                    {
                        int imgID = 0;
                        Environment.DocImageData.DocImageInsert(server.ID, fName, ref imgID, ref docId, 0, "", DateTime.MinValue, "", "", false, creationTime, 0, isImageMain, ImageType, PagesCount);
                    }
                    if (IsSavePart)
                    {
                        if (!IsPdf)
                        {
                            string tempFileName = Lib.Win.Document.Environment.LibTiff.DeletePart(FileName, MinPage - 1, PagesCount, FileName);
                            if (!string.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName))
                            {
                                File.Copy(tempFileName, FileName, true);
                                Slave.DeleteFile(tempFileName);
                            }
                        }
                        else
                        {
                            Lib.Win.Document.Environment.PDFHelper.DelPart(FileName, MinPage, MaxPage);
                        }
                    }
                    break;

                case DialogResult.Yes:
                    Lib.Win.Document.Select.SelectImageDialog cdialog = new Lib.Win.Document.Select.SelectImageDialog {
                        DocumentID = docId, PagesCount = PagesCount, Tag = newFileName, SrcFileName = FileName, SrcStartPage = MinPage, SrcPagesCount = MaxPage - MinPage + 1
                    };
                    cdialog.Show();
                    break;
                }
            }
            catch (Exception ex)
            {
                Lib.Win.Data.Env.WriteToLog(ex);
            }
        }
コード例 #5
0
        private void DelPart(string srcFile, string dstFile, int page_from, int page_to, bool savePermission)
        {
            PdfReader basePDFReader = null;

            string tmpF = (srcFile == dstFile) ? Path.GetTempFileName() : dstFile;

            FileAttributes fa = FileAttributes.Normal;

            try
            {
                if (srcFile == dstFile)
                {
                    if (((int)File.GetAttributes(srcFile) & (int)FileAttributes.ReadOnly) ==
                        (int)FileAttributes.ReadOnly)
                    {
                        try
                        {
                            fa = File.GetAttributes(srcFile);
                            File.SetAttributes(srcFile, FileAttributes.Normal);
                        }
                        catch (Exception ex)
                        {
                            ErrorShower.OnShowError(null, ex.Message,
                                                    Environment.StringResources.GetString("DocControl_Page_Error1"));
                            return;
                        }

                        FileStream fs = null;
                        try
                        {
                            fs = File.Open(dstFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                        }
                        catch (IOException)
                        {
                            ErrorShower.OnShowError(null,
                                                    string.Format(
                                                        Environment.StringResources.GetString("DocControl_PDF_FileBusy"),
                                                        dstFile),
                                                    Environment.StringResources.GetString("DocControl_Page_Error1"));
                            return;
                        }
                        catch (UnauthorizedAccessException ue)
                        {
                            ErrorShower.OnShowError(null, ue.Message,
                                                    Environment.StringResources.GetString("DocControl_Page_Error1"));
                            return;
                        }
                        finally
                        {
                            if (fs != null)
                            {
                                fs.Close();
                            }
                        }
                    }
                }

                using (
                    var fio = new FileStream(srcFile, FileMode.Open, FileAccess.Read,
                                             FileShare.ReadWrite | FileShare.Delete))
                {
                    string password = String.Empty;
                    try
                    {
                        fio.Position  = 0;
                        basePDFReader = new PdfReader(fio, Encoding.UTF8.GetBytes(password));

                        if (!basePDFReader.IsOpenedWithFullPermissions)
                        {
                            throw new BadPasswordException("");
                        }
                    }
                    catch (BadPasswordException)
                    {
                        while (basePDFReader == null || !basePDFReader.IsOpenedWithFullPermissions)
                        {
                            if (basePDFReader != null)
                            {
                                basePDFReader.Close();
                            }

                            password = String.Empty;
                            if (
                                Controls.PdfViewControl.InputBox.Show(Environment.StringResources.GetString("DocControl_PDF_Encrypted"),
                                                                      Environment.StringResources.GetString("DocControl_PDF_EnterPass"),
                                                                      ref password) == DialogResult.Cancel)
                            {
                                return;
                            }

                            try
                            {
                                fio.Position  = 0;
                                basePDFReader = new PdfReader(fio, Encoding.UTF8.GetBytes(password));
                            }
                            catch (BadPasswordException)
                            {
                            }
                        }
                    }

                    if (basePDFReader.NumberOfPages == 0)
                    {
                        throw new Exception("В документе отсутствуют страницы. Операция сохранения невозможна!");
                    }

                    page_from = (page_from <= 0 || page_from > basePDFReader.NumberOfPages) ? 1 : page_from;
                    page_to   = (page_to <= 0 || page_to > basePDFReader.NumberOfPages)
                                  ? basePDFReader.NumberOfPages
                                  : page_to;

                    if ((page_to - page_from + 1) == basePDFReader.NumberOfPages)
                    {
                        return;
                    }

                    int i = page_from - 1;
                    int j = 1;

                    var pages = new int[basePDFReader.NumberOfPages];

                    pages = pages.Select(x => x = j++).ToArray();
                    pages = pages.Where(x => x <page_from || x> page_to).ToArray();

                    using (var file_stream = new FileStream(tmpF, FileMode.Create, FileAccess.Write, FileShare.None))
                        using (var document = new iTextSharp.text.Document())
                            using (var pdfWriter = new PdfCopy(document, file_stream))
                            {
                                pdfWriter.CompressionLevel = PdfStream.BEST_COMPRESSION;
                                pdfWriter.SetFullCompression();
                                pdfWriter.RotateContents = true;

                                if (savePermission && password != String.Empty)
                                {
                                    pdfWriter.SetEncryption(null, Encoding.UTF8.GetBytes(password), basePDFReader.Permissions,
                                                            PdfWriter.STANDARD_ENCRYPTION_128);
                                }

                                document.Open();

                                foreach (int numberPage in pages)
                                {
                                    PdfImportedPage pp = pdfWriter.GetImportedPage(basePDFReader, numberPage);
                                    pdfWriter.AddPage(pp);
                                }
                                document.Close();
                            }
                }
                if (srcFile == dstFile)
                {
                    File.Copy(tmpF, dstFile, true);
                }
            }
            catch (Exception ex)
            {
                Data.Env.WriteToLog(ex);
            }
            finally
            {
                if (basePDFReader != null)
                {
                    basePDFReader.Close();
                }

                if (srcFile == dstFile)
                {
                    Slave.DeleteFile(tmpF);

                    if (fa != File.GetAttributes(dstFile))
                    {
                        try
                        {
                            File.SetAttributes(dstFile, fa);
                        }
                        catch (Exception ex)
                        {
                            Data.Env.WriteToLog(ex);
                        }
                    }
                }
            }
        }