コード例 #1
0
        private void NumberPages(string directoryPath, PdfMerge pdfMerge)
        {
            string      newFilePath      = System.IO.Path.Combine(directoryPath, pdfMerge.Filename);
            string      numberedFileName = String.Format("{0}_numbered{1}", System.IO.Path.GetFileNameWithoutExtension(pdfMerge.Filename), System.IO.Path.GetExtension(pdfMerge.Filename));
            string      numberedFilePath = System.IO.Path.Combine(directoryPath, numberedFileName);
            PdfDocument pdfDocument      = new PdfDocument(Helpers.GetPdfReader(newFilePath), new PdfWriter(numberedFilePath));
            Document    document         = new Document(pdfDocument);

            Paragraph paragraph = null;
            int       n         = pdfDocument.GetNumberOfPages();

            for (int i = 1; i <= n; i++)
            {
                //document.ShowTextAligned(new Paragraph(String.Format("page {0} of {1}", i, n)),
                //        50, 50, i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
                paragraph = new Paragraph(String.Format("page {0} of {1}", i, n));
                paragraph.SetWidthPercent(100);
                paragraph.SetBorder(new SolidBorder(1));
                paragraph.SetHorizontalAlignment(HorizontalAlignment.CENTER);
                document.ShowTextAligned(paragraph,
                                         50, 50, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
            }
            document.Close();

            // Now rename the new one back to the old name
            File.Delete(newFilePath);
            File.Move(numberedFilePath, newFilePath);
            //EventLogManager.WriteInformation(String.Format("{0}|{1}|{2}", newFilePath, numberedFileName, numberedFilePath));
        }
コード例 #2
0
        private void FinishPdf(string directoryPath, PdfMerge pdfMerge)
        {
            string newFilePath = System.IO.Path.Combine(directoryPath, pdfMerge.Filename);

            if (!String.IsNullOrWhiteSpace(pdfMerge.PrinterPath))
            {
                File.Copy(newFilePath, pdfMerge.PrinterPath, true);
            }

            if (!String.IsNullOrWhiteSpace(pdfMerge.DestinationPath))
            {
                string destinationPath = System.IO.Path.Combine(pdfMerge.DestinationPath, pdfMerge.Filename);
                File.Copy(newFilePath, destinationPath, true);
                //File.Move(newFilePath, destinationPath);
            }
        }
コード例 #3
0
        private void MergePdfs(string directoryPath, PdfMerge pdfMerge)
        {
            if (pdfMerge != null && pdfMerge.Pdfs != null && pdfMerge.Pdfs.Count > 0)
            {
                string      mergeFilePath = System.IO.Path.Combine(directoryPath, pdfMerge.Filename);
                PdfDocument pdfDocument   = null;
                PdfDocument mergedPdf     = null;
                PdfMerger   merger        = null;
                string      filePath      = null;

                try
                {
                    mergedPdf = new PdfDocument(new PdfWriter(mergeFilePath));
                    merger    = new PdfMerger(mergedPdf);
                    foreach (var pdf in pdfMerge.Pdfs)
                    {
                        filePath    = System.IO.Path.Combine(directoryPath, pdf.Filename);
                        pdfDocument = new PdfDocument(Helpers.GetPdfReader(filePath));
                        merger.Merge(pdfDocument, 1, pdfDocument.GetNumberOfPages());
                        pdfDocument.Close();
                    }

                    mergedPdf.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (pdfDocument != null)
                    {
                        pdfDocument.Close();
                    }
                    if (mergedPdf != null)
                    {
                        mergedPdf.Close();
                    }
                    if (merger != null)
                    {
                        merger.Close();
                    }
                }
            }
        }
コード例 #4
0
        private void FillFormFields(string directoryPath, PdfMerge pdfMerge)
        {
            if (pdfMerge != null && pdfMerge.Pdfs != null)
            {
                string       formPath;
                string       newFilePath;
                PdfDocument  document = null;
                PdfAcroForm  form;
                PdfFormField pdfFormField;

                foreach (var pdf in pdfMerge.Pdfs)
                {
                    try
                    {
                        formPath    = System.IO.Path.Combine(directoryPath, pdf.Filename);
                        newFilePath = System.IO.Path.Combine(
                            directoryPath,
                            String.Format("{0}.{1}", String.Format("{0}{1}", System.IO.Path.GetFileNameWithoutExtension(pdf.Filename), "_Revised"), System.IO.Path.GetExtension(pdf.Filename)));
                        document = new PdfDocument(Helpers.GetPdfReader(formPath), new PdfWriter(newFilePath));

                        form = PdfAcroForm.GetAcroForm(document, true);
                        if (pdf.Fields != null && pdf.Fields.Count > 0)
                        {
                            foreach (var field in pdf.Fields)
                            {
                                if (field.Value != null)
                                {
                                    pdfFormField = form.GetField(field.Name);

                                    if (pdfFormField != null)
                                    {
                                        form.GetField(field.Name).SetValue(field.Value);

                                        //form.GetField(field.Name).SetFontSize(0);
                                    }
                                    else
                                    {
                                        EventLogManager.WriteWarning(String.Format("Field '{0}' does not exist in '{1}'", field.Name, pdf.Filename));
                                        //document.Close();
                                        //throw new Exception(String.Format("Field '{0}' does not exist in '{1}'", field.Name, pdf.Filename));
                                    }
                                }
                            }
                        }

                        //The below will make sure the fields are not editable in
                        //the output PDF.
                        form.FlattenFields();  // Maybe make this an option in the json so we can partialy fill out forms at some point?
                        //document.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (document != null && !document.IsClosed())
                        {
                            document.Close();
                        }
                    }

                    // Now rename the new one back to the old name
                    File.Delete(formPath);
                    File.Move(newFilePath, formPath);
                }
            }
        }
コード例 #5
0
        private void ProcessDirectory(string directoryPath)
        {
            // DON'T TOUCH THE BACKUPS, ERRORS AND WORK DIRECTORIES.  Just in case they were made or renamed after the fact for some reason
            if (directoryPath != this._errorsPath && directoryPath != this._backupsPath && directoryPath != this._workPath)
            {
                string pdfJsonPath = System.IO.Path.Combine(directoryPath, "pdf.json");

                if (File.Exists(pdfJsonPath))
                {
                    string workPath = System.IO.Path.Combine(this._workPath, System.IO.Path.GetFileName(directoryPath));

                    try
                    {
                        CopyToDirectory(directoryPath, workPath);

                        PdfMerge pdfMerge = null;

                        string jsonPath = System.IO.Path.Combine(workPath, "pdf.json");
                        using (StreamReader r = Helpers.GetStreamReader(jsonPath))
                        {
                            string json = r.ReadToEnd();
                            pdfMerge = JsonConvert.DeserializeObject <PdfMerge>(json);
                        }

                        FillFormFields(workPath, pdfMerge);
                        MergePdfs(workPath, pdfMerge);
                        //NumberPages(workPath, pdfMerge);
                        FinishPdf(workPath, pdfMerge);

                        // Move original to backups directory
                        if (DoSaveBackups)
                        {
                            string backupsPath = System.IO.Path.Combine(this._backupsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, backupsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLogManager.WriteError(ex);

                        if (DoSaveErrors)
                        {
                            // Move original to errors directory
                            string errorsPath = System.IO.Path.Combine(this._errorsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, errorsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }

                    // Delete work directory
                    Directory.Delete(workPath, true);
                }
                else
                {
                    EventLogManager.WriteInformation(String.Format("No pdf.json file.  {0} skipped.", directoryPath));
                }
            }
        }