Exemplo n.º 1
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc           = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            int         insertPageNumber = 4;

            // Copier contains the additional logic to copy acroform fields to a new page.
            // PdfPageFormCopier uses some caching logic which can potentially improve performance
            // in case of the reusing of the same instance.
            PdfPageFormCopier formCopier = new PdfPageFormCopier();
            PdfDocument       insertDoc  = new PdfDocument(new PdfReader(INSERT));

            insertDoc.CopyPagesTo(1, 1, pdfDoc, insertPageNumber, formCopier);
            insertDoc.Close();

            PdfOutline outlines = pdfDoc.GetOutlines(false);
            PdfOutline outline  = outlines.GetAllChildren()[0].AddOutline("Hello", insertPageNumber - 1);

            outline.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(insertPageNumber)));

            pdfDoc.Close();
        }
Exemplo n.º 2
0
        private void mergePdfForms(String dest, PdfReader[] readers)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            // This method initializes an outline tree of the document and sets outline mode to true.
            pdfDoc.InitializeOutlines();

            // Copier contains the logic to copy only acroform fields to a new page.
            // PdfPageFormCopier uses some caching logic which can potentially improve performance
            // in case of the reusing of the same instance.
            PdfPageFormCopier formCopier = new PdfPageFormCopier();

            foreach (PdfReader reader in readers)
            {
                PdfDocument readerDoc = new PdfDocument(reader);
                readerDoc.CopyPagesTo(1, readerDoc.GetNumberOfPages(), pdfDoc, formCopier);
                readerDoc.Close();
            }

            pdfDoc.Close();
        }
Exemplo n.º 3
0
        protected void ManipulatePdf(String dest)
        {
            PdfWriter         writer     = new PdfWriter(dest);
            PdfPageFormCopier formCopier = new PdfPageFormCopier();

            // In smart mode when resources (such as fonts, images,...) are encountered,
            // a reference to these resources is saved in a cache and can be reused.
            // This mode reduces the file size of the resulting PDF document.
            writer.SetSmartMode(true);
            PdfDocument pdfDoc = new PdfDocument(writer);

            // Initialize an outline tree of the document and sets outline mode to true
            pdfDoc.InitializeOutlines();

            using (StreamReader streamReader = new StreamReader(DATA))
            {
                // Read first line with headers,
                // do nothing with this line, because headers are already filled in form
                String line = streamReader.ReadLine();

                while ((line = streamReader.ReadLine()) != null)
                {
                    // Сreate a PDF in memory
                    ByteArrayOutputStream baos        = new ByteArrayOutputStream();
                    PdfDocument           pdfInnerDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
                    PdfAcroForm           form        = PdfAcroForm.GetAcroForm(pdfInnerDoc, true);

                    // Parse text line and fill all fields of form
                    FillAndFlattenForm(line, form);
                    pdfInnerDoc.Close();

                    // Copy page with current filled form to the result pdf document
                    pdfInnerDoc = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray())));
                    pdfInnerDoc.CopyPagesTo(1, pdfInnerDoc.GetNumberOfPages(), pdfDoc, formCopier);
                    pdfInnerDoc.Close();
                }
            }

            pdfDoc.Close();
        }
Exemplo n.º 4
0
        protected void ManipulatePdf(String dest)
        {
            MemoryStream baos   = new MemoryStream();
            PdfDocument  pdfDoc = new PdfDocument(new PdfWriter(baos));
            Document     doc    = new Document(pdfDoc);

            // Copier contains the additional logic to copy acroform fields to a new page.
            // PdfPageFormCopier uses some caching logic which can potentially improve performance
            // in case of the reusing of the same instance.
            PdfPageFormCopier formCopier = new PdfPageFormCopier();

            // Copy all merging file's pages to the temporary pdf file
            Dictionary <String, PdfDocument> filesToMerge = InitializeFilesToMerge();
            Dictionary <int, String>         toc          = new Dictionary <int, String>();
            int page = 1;

            foreach (KeyValuePair <String, PdfDocument> entry in filesToMerge)
            {
                PdfDocument srcDoc        = entry.Value;
                int         numberOfPages = srcDoc.GetNumberOfPages();

                toc.Add(page, entry.Key);

                for (int i = 1; i <= numberOfPages; i++, page++)
                {
                    Text text = new Text(String.Format("Page {0}", page));
                    srcDoc.CopyPagesTo(i, i, pdfDoc, formCopier);

                    // Put the destination at the very first page of each merged document
                    if (i == 1)
                    {
                        text.SetDestination("p" + page);

                        PdfOutline rootOutLine = pdfDoc.GetOutlines(false);
                        PdfOutline outline     = rootOutLine.AddOutline("p" + page);
                        outline.AddDestination(PdfDestination.MakeDestination(new PdfString("p" + page)));
                    }

                    doc.Add(new Paragraph(text)
                            .SetFixedPosition(page, 549, 810, 40)
                            .SetMargin(0)
                            .SetMultipliedLeading(1));
                }
            }

            PdfDocument tocDoc = new PdfDocument(new PdfReader(SRC3));

            tocDoc.CopyPagesTo(1, 1, pdfDoc, formCopier);
            tocDoc.Close();

            // Create a table of contents
            float tocYCoordinate = 750;
            float tocXCoordinate = doc.GetLeftMargin();
            float tocWidth       = pdfDoc.GetDefaultPageSize().GetWidth() - doc.GetLeftMargin() - doc.GetRightMargin();

            foreach (KeyValuePair <int, String> entry in toc)
            {
                Paragraph p = new Paragraph();
                p.AddTabStops(new TabStop(500, TabAlignment.LEFT, new DashedLine()));
                p.Add(entry.Value);
                p.Add(new Tab());
                p.Add(entry.Key.ToString());
                p.SetAction(PdfAction.CreateGoTo("p" + entry.Key));
                doc.Add(p
                        .SetFixedPosition(pdfDoc.GetNumberOfPages(), tocXCoordinate, tocYCoordinate, tocWidth)
                        .SetMargin(0)
                        .SetMultipliedLeading(1));

                tocYCoordinate -= 20;
            }

            foreach (PdfDocument srcDocument in filesToMerge.Values)
            {
                srcDocument.Close();
            }

            doc.Close();

            PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
            PdfDocument srcPdfDoc = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray()),
                                                                  new ReaderProperties()));

            srcPdfDoc.InitializeOutlines();

            // Create a copy order list and set the page with a table of contents as the first page
            int        tocPageNumber      = srcPdfDoc.GetNumberOfPages();
            List <int> copyPagesOrderList = new List <int>();

            copyPagesOrderList.Add(tocPageNumber);
            for (int i = 1; i < tocPageNumber; i++)
            {
                copyPagesOrderList.Add(i);
            }

            srcPdfDoc.CopyPagesTo(copyPagesOrderList, resultDoc, formCopier);

            srcPdfDoc.Close();
            resultDoc.Close();
        }
        public virtual void CreatePdf(String dest)
        {
            PdfDocument       pdfDocument = new PdfDocument(new PdfWriter(dest));
            PdfPageFormCopier formCopier  = new PdfPageFormCopier();

            using (StreamReader sr = File.OpenText(DATA))
            {
                String line;
                bool   headerLine = true;
                int    i          = 1;
                while ((line = sr.ReadLine()) != null)
                {
                    if (headerLine)
                    {
                        headerLine = false;
                        continue;
                    }

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    PdfDocument           sourcePdfDocument = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
                    //Rename fields
                    i++;
                    PdfAcroForm form = PdfAcroForm.GetAcroForm(sourcePdfDocument, true);
                    form.RenameField("name", "name_" + i);
                    form.RenameField("abbr", "abbr_" + i);
                    form.RenameField("capital", "capital_" + i);
                    form.RenameField("city", "city_" + i);
                    form.RenameField("population", "population_" + i);
                    form.RenameField("surface", "surface_" + i);
                    form.RenameField("timezone1", "timezone1_" + i);
                    form.RenameField("timezone2", "timezone2_" + i);
                    form.RenameField("dst", "dst_" + i);
                    //Fill out fields
                    StringTokenizer tokenizer = new StringTokenizer(line, ";");
                    IDictionary <String, PdfFormField> fields = form.GetFormFields();
                    PdfFormField toSet;
                    fields.TryGetValue("name_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("abbr_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("capital_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("city_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("population_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("surface_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("timezone1_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("timezone2_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("dst_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    sourcePdfDocument.Close();
                    sourcePdfDocument = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray())));
                    //Copy pages
                    sourcePdfDocument.CopyPagesTo(1, sourcePdfDocument.GetNumberOfPages(), pdfDocument, formCopier);
                    sourcePdfDocument.Close();
                }
            }

            pdfDocument.Close();
        }
Exemplo n.º 6
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            // Initialize a resultant document outlines in order to copy outlines from the source documents.
            // Note that outlines still could be copied even if in destination document outlines
            // are not initialized, by using PdfMerger with mergeOutlines vakue set as true
            pdfDoc.InitializeOutlines();

            // Copier contains the additional logic to copy acroform fields to a new page.
            // PdfPageFormCopier uses some caching logic which can potentially improve performance
            // in case of the reusing of the same instance.
            PdfPageFormCopier formCopier = new PdfPageFormCopier();

            // Copy all merging file's pages to the result pdf file
            Dictionary <String, PdfDocument> filesToMerge = InitializeFilesToMerge();
            Dictionary <int, String>         toc          = new Dictionary <int, String>();
            int page = 1;

            foreach (KeyValuePair <String, PdfDocument> entry in filesToMerge)
            {
                PdfDocument srcDoc        = entry.Value;
                int         numberOfPages = srcDoc.GetNumberOfPages();

                toc.Add(page, entry.Key);

                for (int i = 1; i <= numberOfPages; i++, page++)
                {
                    Text text = new Text(String.Format("Page {0}", page));
                    srcDoc.CopyPagesTo(i, i, pdfDoc, formCopier);

                    // Put the destination at the very first page of each merged document
                    if (i == 1)
                    {
                        text.SetDestination("p" + page);
                    }

                    doc.Add(new Paragraph(text).SetFixedPosition(page, 549, 810, 40)
                            .SetMargin(0)
                            .SetMultipliedLeading(1));
                }
            }

            PdfDocument tocDoc = new PdfDocument(new PdfReader(SRC3));

            tocDoc.CopyPagesTo(1, 1, pdfDoc, formCopier);
            tocDoc.Close();

            // Create a table of contents
            float tocYCoordinate = 750;
            float tocXCoordinate = doc.GetLeftMargin();
            float tocWidth       = pdfDoc.GetDefaultPageSize().GetWidth() - doc.GetLeftMargin() - doc.GetRightMargin();

            foreach (KeyValuePair <int, String> entry in toc)
            {
                Paragraph p = new Paragraph();
                p.AddTabStops(new TabStop(500, TabAlignment.LEFT, new DashedLine()));
                p.Add(entry.Value);
                p.Add(new Tab());
                p.Add(entry.Key.ToString());
                p.SetAction(PdfAction.CreateGoTo("p" + entry.Key));
                doc.Add(p.SetFixedPosition(pdfDoc.GetNumberOfPages(), tocXCoordinate, tocYCoordinate, tocWidth)
                        .SetMargin(0)
                        .SetMultipliedLeading(1));

                tocYCoordinate -= 20;
            }

            foreach (PdfDocument srcDoc in filesToMerge.Values)
            {
                srcDoc.Close();
            }

            doc.Close();
        }