Beispiel #1
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc    = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document    doc       = new Document(pdfDoc);
            PdfPage     firstPage = pdfDoc.GetFirstPage();

            PdfCanvas canvas = new PdfCanvas(firstPage);

            canvas
            .MoveTo(36, 700)
            .LineTo(72, 760)
            .LineTo(144, 720)
            .LineTo(72, 730)
            .ClosePathStroke();

            Rectangle linkLocation = new Rectangle(36, 700, 144, 760);

            // Make the link destination page fit to the display
            PdfExplicitDestination destination    = PdfExplicitDestination.CreateFit(firstPage);
            PdfLinkAnnotation      linkAnnotation = new PdfLinkAnnotation(linkLocation)

                                                    // Set highlighting type which is enabled after a click on the annotation
                                                    .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)

                                                    // Create a link to the first page of the document.
                                                    .SetAction(PdfAction.CreateGoTo(destination));
            PdfArray arrayOfQuadPoints = new PdfArray(new int[] { 72, 730, 144, 720, 72, 760, 36, 700 });

            linkAnnotation.Put(PdfName.QuadPoints, arrayOfQuadPoints);

            firstPage.AddAnnotation(linkAnnotation);

            doc.Close();
        }
Beispiel #2
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc    = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            ImageData   img       = ImageDataFactory.Create(IMG);
            float       x         = 10;
            float       y         = 650;
            float       width     = img.GetWidth();
            float       height    = img.GetHeight();
            PdfPage     firstPage = pdfDoc.GetFirstPage();

            PdfCanvas canvas = new PdfCanvas(firstPage);

            canvas.AddImageAt(img, x, y, false);

            Rectangle linkLocation = new Rectangle(x, y, width, height);

            // Make the link destination page fit to the display
            PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetLastPage());
            PdfAnnotation          annotation  = new PdfLinkAnnotation(linkLocation)

                                                 // Set highlighting type which is enabled after a click on the annotation
                                                 .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)

                                                 // Create a link to the last page of the document.
                                                 .SetAction(PdfAction.CreateGoTo(destination))
                                                 .SetBorder(new PdfArray(new float[] { 0, 0, 0 }));

            firstPage.AddAnnotation(annotation);

            pdfDoc.Close();
        }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            int[]          borders        = new int[] { 0, 0, 1 };
            PdfDestination pdfDestination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(10));
            Rectangle      rect           = new Rectangle(0, 806, 595, 36);
            PdfAnnotation  a10            = new PdfLinkAnnotation(rect)
                                            .SetAction(PdfAction.CreateGoTo(pdfDestination))
                                            .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
                                            .SetPage(pdfDoc.GetPage(10)).SetBorder(new PdfArray(borders))
                                            .SetColor(new PdfArray(new float[] { 0, 1, 0 }));

            pdfDoc.GetPage(1).AddAnnotation(a10);
            PdfDestination d2 = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1));
            PdfAnnotation  a1 = new PdfLinkAnnotation(rect)
                                .SetAction(PdfAction.CreateGoTo(d2))
                                .SetHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH)
                                .SetPage(pdfDoc.GetPage(1))
                                .SetBorder(new PdfArray(borders))
                                .SetColor(new PdfArray(new float[] { 0, 1, 0 }));

            pdfDoc.GetPage(10).AddAnnotation(a1);

            pdfDoc.Close();
        }
        private MemoryStream CreatePdfWithGoToAnnot(bool isTheSameLink, bool diffDisplayOptions)
        {
            MemoryStream stream      = new MemoryStream();
            PdfDocument  pdfDocument = new PdfDocument(new PdfWriter(stream));

            pdfDocument.AddNewPage();
            pdfDocument.AddNewPage();
            pdfDocument.AddNewPage();
            Rectangle linkLocation             = new Rectangle(523, 770, 36, 36);
            PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDocument.GetPage(3));
            PdfAnnotation          annotation  = new PdfLinkAnnotation(linkLocation).SetAction(PdfAction.CreateGoTo(destination)
                                                                                               ).SetBorder(new PdfArray(new int[] { 0, 0, 1 }));

            pdfDocument.GetFirstPage().AddAnnotation(annotation);
            if (!isTheSameLink)
            {
                destination = (diffDisplayOptions) ? PdfExplicitDestination.Create(pdfDocument.GetPage(3), PdfName.XYZ, 350
                                                                                   , 350, 0, 0, 1) : PdfExplicitDestination.CreateFit(pdfDocument.GetPage(1));
            }
            annotation = new PdfLinkAnnotation(linkLocation).SetAction(PdfAction.CreateGoTo(destination)).SetBorder(new
                                                                                                                    PdfArray(new int[] { 0, 0, 1 }));
            pdfDocument.GetPage(2).AddAnnotation(annotation);
            pdfDocument.Close();
            return(stream);
        }
Beispiel #5
0
        private static void MergePdfFiles(string outputFile, List <string> sourceFiles)
        {
            using (var writer = new PdfWriter(new FileStream(outputFile, FileMode.OpenOrCreate)))
            {
                using (var outputDocument = new PdfDocument(writer))
                {
                    outputDocument.InitializeOutlines();
                    var        rootOutline = outputDocument.GetOutlines(false);
                    PdfOutline rootO       = rootOutline.AddOutline("Root");
                    int        pages       = 1;
                    int        count       = 1;
                    foreach (var inputFile in sourceFiles)
                    {
                        using (var inputDoc = new PdfDocument(new PdfReader((inputFile))))
                        {
                            for (int i = 1; i <= inputDoc.GetNumberOfPages(); i++)
                            {
                                var newp     = outputDocument.AddNewPage();
                                var canvas   = new PdfCanvas(newp);
                                var origPage = inputDoc.GetPage(i);
                                var copy     = origPage.CopyAsFormXObject(outputDocument);
                                canvas.AddXObject(copy, 0, 0);
                                copy.Flush();
                                origPage = null;
                                canvas.Release();
                                newp.Flush();
                                writer.Flush();
                                canvas = null;
                                newp   = null;
                            }

                            var subPages = inputDoc.GetNumberOfPages();
                            //PdfOutline pdfOutine = inputDoc.GetOutlines(false);

                            /*var link1 = rootO.AddOutline(count.ToString());
                             * link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages)));
                             * pages += subPages;*/

                            PdfOutline pdfOutine = inputDoc.GetOutlines(false);

                            //pdfOutine.GetAllChildren().FirstOrDefault().AddOutline;
                            foreach (var aOutline in pdfOutine.GetAllChildren())
                            {
                                var link1 = rootO.AddOutline(aOutline.GetTitle(), aOutline.pos);
                                link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages)));
                            }
                            pages += subPages;


                            count++;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// creates link to specific page
        /// </summary>
        /// <param name="pageIndex">page index</param>
        /// <param name="textLink">text link</param>
        /// <returns>paragraph link</returns>
        protected Paragraph CreateLinkToPage(int pageIndex, string textLink)
        {
            //PdfArray array = new PdfArray();
            //array.Add(document.GetPdfDocument().GetPage(pageIndex).GetPdfObject());
            //array.Add(PdfName.Fit);
            //PdfDestination dest = PdfDestination.MakeDestination(array);
            PdfExplicitDestination dest = PdfExplicitDestination.CreateFit(pdfDocument.GetPage(pageIndex));
            Paragraph link = new Paragraph(new Link(textLink, PdfAction.CreateGoTo(dest)));

            return(link);
        }
        private static void MergePdfsWithBookmarks(List <string> InFiles, string OutFile)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile));

            pdfDoc.InitializeOutlines();

            PdfMerger merger = new PdfMerger(pdfDoc, true, true);



            List <PdfOutline> listItem = new List <PdfOutline>();

            InFiles.ForEach(srcFile =>
            {
                PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile));
                int pageCount = firstSourcePdf.GetNumberOfPages();
                merger.Merge(firstSourcePdf, 1, pageCount);

                firstSourcePdf.GetOutlines(false).GetDestination();
                firstSourcePdf.Close();

                if (pageCount % 2 == 1)
                {
                    pdfDoc.AddNewPage(iTextPageSize.A4);
                }
            });

            PdfOutline rootOutline = pdfDoc.GetOutlines(false);

            listItem.AddRange(rootOutline.GetAllChildren());

            rootOutline.GetAllChildren().Clear();

            string     parentTitle = Path.GetFileNameWithoutExtension(OutFile);
            PdfOutline parent      = rootOutline.AddOutline(parentTitle);

            foreach (var item in listItem)
            {
                parent.AddOutline(item);
            }

            PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage());
            string stringDest = Guid.NewGuid().ToString();

            pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject());
            parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest)));

            if (pdfDoc.GetNumberOfPages() % 2 == 1)
            {
                pdfDoc.AddNewPage(iTextPageSize.A4);
            }

            pdfDoc.Close();
        }
        public static void BeforeClass()
        {
            CreateDestinationFolder(destinationFolder);
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "documentWithOutlines.pdf"));

            pdfDoc.GetCatalog().SetPageMode(PdfName.UseOutlines);
            PdfPage    firstPage     = pdfDoc.AddNewPage();
            PdfPage    secondPage    = pdfDoc.AddNewPage();
            PdfOutline rootOutline   = pdfDoc.GetOutlines(false);
            PdfOutline firstOutline  = rootOutline.AddOutline("First Page");
            PdfOutline secondOutline = rootOutline.AddOutline("Second Page");

            firstOutline.AddDestination(PdfExplicitDestination.CreateFit(firstPage));
            secondOutline.AddDestination(PdfExplicitDestination.CreateFit(secondPage));
            pdfDoc.Close();
        }
        public virtual void SetupAddOutlineInNotOutlineMode()
        {
            String      filename        = sourceFolder + "iphone_user_guide.pdf";
            PdfReader   reader          = new PdfReader(filename);
            PdfWriter   writer          = new PdfWriter(destinationFolder + "addOutlinesWithoutOutlineModeResult.pdf");
            PdfDocument pdfDoc          = new PdfDocument(reader, writer);
            PdfOutline  outlines        = new PdfOutline(pdfDoc);
            PdfOutline  firstPage       = outlines.AddOutline("firstPage");
            PdfOutline  firstPageChild  = firstPage.AddOutline("firstPageChild");
            PdfOutline  secondPage      = outlines.AddOutline("secondPage");
            PdfOutline  secondPageChild = secondPage.AddOutline("secondPageChild");

            firstPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            firstPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            secondPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            secondPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            pdfDoc.Close();
        }
Beispiel #10
0
        public virtual void CreateSimpleDocWithOutlines()
        {
            String      filename = "simpleDocWithOutlines.pdf";
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(destinationFolder + filename));

            pdfDoc.GetCatalog().SetPageMode(PdfName.UseOutlines);
            PdfPage    firstPage     = pdfDoc.AddNewPage();
            PdfPage    secondPage    = pdfDoc.AddNewPage();
            PdfOutline rootOutline   = pdfDoc.GetOutlines(false);
            PdfOutline firstOutline  = rootOutline.AddOutline("First Page");
            PdfOutline secondOutline = rootOutline.AddOutline("Second Page");

            firstOutline.AddDestination(PdfExplicitDestination.CreateFit(firstPage));
            secondOutline.AddDestination(PdfExplicitDestination.CreateFit(secondPage));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder, "diff_"));
        }
Beispiel #11
0
        public virtual void AddOutlineInNotOutlineMode()
        {
            String      filename        = "addOutlineInNotOutlineMode.pdf";
            PdfReader   reader          = new PdfReader(sourceFolder + "iphone_user_guide.pdf");
            PdfWriter   writer          = new PdfWriter(destinationFolder + filename);
            PdfDocument pdfDoc          = new PdfDocument(reader, writer);
            PdfOutline  outlines        = new PdfOutline(pdfDoc);
            PdfOutline  firstPage       = outlines.AddOutline("firstPage");
            PdfOutline  firstPageChild  = firstPage.AddOutline("firstPageChild");
            PdfOutline  secondPage      = outlines.AddOutline("secondPage");
            PdfOutline  secondPageChild = secondPage.AddOutline("secondPageChild");

            firstPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            firstPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            secondPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            secondPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder, "diff_"));
        }
Beispiel #12
0
        public virtual void StructureDestination02Test()
        {
            String                  srcFile          = sourceFolder + "customRolesMappingPdf2.pdf";
            String                  outFile          = destinationFolder + "structureDestination02Test.pdf";
            String                  cmpFile          = sourceFolder + "cmp_structureDestination02Test.pdf";
            PdfDocument             document         = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile));
            PdfStructElem           imgElement       = new PdfStructElem((PdfDictionary)document.GetPdfObject(13));
            PdfStructureDestination dest             = PdfStructureDestination.CreateFit(imgElement);
            PdfPage                 secondPage       = document.AddNewPage();
            PdfPage                 thirdPage        = document.AddNewPage();
            PdfLinkAnnotation       linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15));
            PdfAction               gotoStructAction = PdfAction.CreateGoTo(PdfExplicitDestination.CreateFit(thirdPage));

            gotoStructAction.Put(PdfName.SD, dest.GetPdfObject());
            linkExplicitDest.SetAction(gotoStructAction);
            secondPage.AddAnnotation(linkExplicitDest);
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_"
                                                                             ));
        }
Beispiel #13
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();
        }
Beispiel #14
0
 private static void AddRecursively(List <IBookmark> addedMarks, PdfDocument doc,
                                    PdfOutline outlineParent, IBookmark bookmarkParent = null)
 {
     if (bookmarkParent != null)
     {
         foreach (IBookmark current in addedMarks.FindAll(x => x.ParentId == bookmarkParent.Id))
         {
             var subParent = outlineParent.AddOutline(current.Title);
             subParent.AddDestination(PdfExplicitDestination.CreateFit(doc.GetPage(current.StartPage)));
             AddRecursively(addedMarks, doc, subParent, current);
         }
     }
     else
     {
         foreach (IBookmark current in addedMarks.FindAll(x => x.ParentId == Guid.Empty))
         {
             var subParent = outlineParent.AddOutline(current.Title);
             subParent.AddDestination(PdfExplicitDestination.CreateFit(doc.GetPage(current.StartPage)));
             AddRecursively(addedMarks, doc, subParent, current);
         }
     }
 }
        public virtual void SetupAddOutlinesToDocumentTest()
        {
            String      filename = sourceFolder + "iphone_user_guide.pdf";
            PdfReader   reader   = new PdfReader(filename);
            PdfWriter   writer   = new PdfWriter(destinationFolder + "addOutlinesResult.pdf");
            PdfDocument pdfDoc   = new PdfDocument(reader, writer);

            pdfDoc.SetTagged();
            PdfOutline outlines        = pdfDoc.GetOutlines(false);
            PdfOutline firstPage       = outlines.AddOutline("firstPage");
            PdfOutline firstPageChild  = firstPage.AddOutline("firstPageChild");
            PdfOutline secondPage      = outlines.AddOutline("secondPage");
            PdfOutline secondPageChild = secondPage.AddOutline("secondPageChild");

            firstPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            firstPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            secondPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            secondPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            outlines.GetAllChildren()[0].GetAllChildren()[1].AddOutline("testOutline", 1).AddDestination(PdfExplicitDestination
                                                                                                         .CreateFit(pdfDoc.GetPage(102)));
            pdfDoc.Close();
        }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc       = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Rectangle   linkLocation = new Rectangle(523, 770, 36, 36);

            int[] borders = { 0, 0, 1 };

            // Make the link destination page fit to the display
            PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(3));
            PdfAnnotation          annotation  = new PdfLinkAnnotation(linkLocation)

                                                 // Set highlighting type which is enabled after a click on the annotation
                                                 .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)

                                                 // Add link to the 3rd page.
                                                 .SetAction(PdfAction.CreateGoTo(destination))
                                                 .SetBorder(new PdfArray(borders));

            pdfDoc.GetFirstPage().AddAnnotation(annotation);

            pdfDoc.Close();
        }
Beispiel #17
0
        public virtual void DestTest01()
        {
            String            srcFile          = sourceFolder + "simpleNoLinks.pdf";
            String            outFile          = destinationFolder + "destTest01.pdf";
            String            cmpFile          = sourceFolder + "cmp_destTest01.pdf";
            PdfDocument       document         = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile));
            PdfPage           firstPage        = document.GetPage(1);
            PdfLinkAnnotation linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15));

            linkExplicitDest.SetAction(PdfAction.CreateGoTo(PdfExplicitDestination.CreateFit(document.GetPage(2))));
            firstPage.AddAnnotation(linkExplicitDest);
            PdfLinkAnnotation      linkStringDest = new PdfLinkAnnotation(new Rectangle(35, 760, 160, 15));
            PdfExplicitDestination destToPage3    = PdfExplicitDestination.CreateFit(document.GetPage(3));
            String stringDest = "thirdPageDest";

            document.AddNamedDestination(stringDest, destToPage3.GetPdfObject());
            linkStringDest.SetAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest)));
            firstPage.AddAnnotation(linkStringDest);
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_"
                                                                             ));
        }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            // Make the link destination page fit to the display
            PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(3));
            Link link = new Link(
                "This is a link. Click it and you'll be forwarded to another page in this document.",

                // Add link to the 3rd page.
                PdfAction.CreateGoTo(destination));

            // Set highlighting type which is enabled after a click on the annotation
            link.GetLinkAnnotation().SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
            Paragraph p = new Paragraph(link).SetWidth(240);

            doc.ShowTextAligned(p, 320, 695, 1, TextAlignment.LEFT,
                                VerticalAlignment.TOP, 0);

            doc.Close();
        }
        private void addOutline(List <MergeItem> mergeTree,
                                PdfOutline child, int currDepth)
        {
            int pageNumber;

            foreach (MergeItem mi in mergeTree)
            {
                if (mi.fileItem.isMissing)
                {
                    continue;
                }

                pageNumber = findPageNumber(mi);

                if (pageNumber == -2)
                {
                    continue;
                }

                PdfOutline grandChild =
                    child.AddOutline(mi.bookmarkTitle);

                grandChild.SetOpen(!COLLAPSE_FILE_BOOKMARKS);


                if (pageNumber >= 0)
                {
                    grandChild.AddDestination(
                        PdfExplicitDestination.CreateFit(pageNumber));
                }

                if (mi.mergeItems != null)
                {
                    addOutline(mi.mergeItems, grandChild, currDepth + 1);
                }
            }
        }
Beispiel #20
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc  = new PdfDocument(new PdfWriter(dest));
            PdfDocument srcDoc1 = new PdfDocument(new PdfReader(SRC1));
            PdfDocument srcDoc2 = new PdfDocument(new PdfReader(SRC2));
            PdfDocument srcDoc3 = new PdfDocument(new PdfReader(SRC3));

            int numberOfPages1 = srcDoc1.GetNumberOfPages();
            int numberOfPages2 = srcDoc2.GetNumberOfPages();
            int numberOfPages3 = srcDoc3.GetNumberOfPages();

            PdfMerger merger = new PdfMerger(pdfDoc);

            merger.SetCloseSourceDocuments(true)
            .Merge(srcDoc1, 1, numberOfPages1)
            .Merge(srcDoc2, 1, numberOfPages2)
            .Merge(srcDoc3, 1, numberOfPages3);

            PdfOutline rootOutline = pdfDoc.GetOutlines(false);

            int        page       = 1;
            PdfOutline helloWorld = rootOutline.AddOutline("Hello World");

            helloWorld.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page)));
            page += numberOfPages1;

            PdfOutline link1 = helloWorld.AddOutline("link1");

            link1.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page)));
            page += numberOfPages2;

            PdfOutline link2 = rootOutline.AddOutline("Link 2");

            link2.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page)));

            pdfDoc.Close();
        }
Beispiel #21
0
        public virtual void AddOutlinesToDocumentTest()
        {
            PdfReader   reader   = new PdfReader(sourceFolder + "iphone_user_guide.pdf");
            String      filename = "addOutlinesToDocumentTest.pdf";
            PdfWriter   writer   = new PdfWriter(destinationFolder + filename);
            PdfDocument pdfDoc   = new PdfDocument(reader, writer);

            pdfDoc.SetTagged();
            PdfOutline outlines        = pdfDoc.GetOutlines(false);
            PdfOutline firstPage       = outlines.AddOutline("firstPage");
            PdfOutline firstPageChild  = firstPage.AddOutline("firstPageChild");
            PdfOutline secondPage      = outlines.AddOutline("secondPage");
            PdfOutline secondPageChild = secondPage.AddOutline("secondPageChild");

            firstPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            firstPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)));
            secondPage.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            secondPageChild.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(2)));
            outlines.GetAllChildren()[0].GetAllChildren()[1].AddOutline("testOutline", 1).AddDestination(PdfExplicitDestination
                                                                                                         .CreateFit(pdfDoc.GetPage(102)));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder, "diff_"));
        }
Beispiel #22
0
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            PdfFont  font     = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont  bold     = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3))
            .SetFont(font).SetFontSize(11);
            StreamReader sr = File.OpenText(SRC);
            String       name;
            String       line;
            Paragraph    p;
            bool         title   = true;
            int          counter = 0;
            IList <Util.Pair <String, int> > toc = new List <Util.Pair <String, int> >();

            while ((line = sr.ReadLine()) != null)
            {
                p = new Paragraph(line);
                p.SetKeepTogether(true);
                if (title)
                {
                    name = String.Format("title{0:00}", counter++);
                    p.SetFont(bold).SetFontSize(12).SetKeepWithNext(true).SetDestination(name);
                    title = false;
                    document.Add(p);
                    // The following line is problematic when using setKeepWithNext
                    toc.Add(new Util.Pair <string, int>(line, pdf.GetNumberOfPages()));
                }
                else
                {
                    p.SetFirstLineIndent(36);
                    if (String.IsNullOrEmpty(line))
                    {
                        p.SetMarginBottom(12);
                        title = true;
                    }
                    else
                    {
                        p.SetMarginBottom(0);
                    }
                    document.Add(p);
                }
            }
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            p = new Paragraph().SetFont(bold).Add("Table of Contents");
            document.Add(p);
            toc.RemoveAt(0);
            IList <TabStop> tabstops = new List <TabStop>();

            tabstops.Add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
            foreach (Util.Pair <String, int> entry in toc)
            {
                p = new Paragraph().AddTabStops(tabstops).Add(entry.Key).Add(new Tab()).Add(entry.Value.ToString()).SetAction
                        (PdfAction.CreateGoTo(PdfExplicitDestination.CreateFit(entry.Value)));
                document.Add(p);
            }
            //Close document
            document.Close();
        }
        internal PdfDocument Merge(string outputFile, PdfMergeTree mergeTree)
        {
            Form1.configProgressBar(mergeTree.count);

            listOptions();

            if (!VerifyOutputFile(outputFile))
            {
                throw new IOException("Invalid Output File");
            }

            PdfWriter writer = new PdfWriter(outputFile);

            destPdf = new PdfDocument(writer);

            destPdfOutlines = destPdf.GetOutlines(false);

            PdfMerger merger = new PdfMerger(destPdf, MERGE_TAGS, MERGE_BOOKMARKS);

            merger.SetCloseSourceDocuments(false);

            logMsgDbLn2("");

            int pageCount = merge(merger, mergeTree.GetMergeItems, 1) - 1;

            logMsgFmtln("total page count", pageCount);

            if (pageCount < 1)
            {
                destPdf.Close();
                return(null);
            }

            if (KEEP_IMPORT_BOOKMARKS && ADD_BOOKMARK_FOR_EACH_FILE)
            {
                // process and adjust bookmarks
                MergeOutlines(destPdf.GetOutlines(true).GetAllChildren(),
                              mergeTree.GetMergeItems);
            }

            if (!(KEEP_IMPORT_BOOKMARKS && !ADD_BOOKMARK_FOR_EACH_FILE))
            {
                // eliminate all of the current bookmarks
                destPdfOutlines = clearOutline(destPdf);
            }

            if (ADD_BOOKMARK_FOR_EACH_FILE)
            {
                // add the new bookmarks
                addOutline(mergeTree.GetMergeItems, destPdfOutlines, 0);
            }
            // all complete
            // leave open for further processing
            //			destPdf.Close();

            // return the final PDF document

            PdfCatalog destPdfCatalog = destPdf.GetCatalog();

            destPdfCatalog.SetPageMode(PdfName.UseOutlines);
            destPdfCatalog.SetPageLayout(PdfName.SinglePage);
            destPdfCatalog.SetOpenAction(PdfExplicitDestination.CreateFit(1));

            return(destPdf);
        }
        public virtual void CreatePdf(String dest)
        {
            PdfDocument   pdf      = new PdfDocument(new PdfWriter(dest));
            Document      document = new Document(pdf);
            PdfAction     js       = PdfAction.CreateJavaScript("app.alert('Boo!');");
            PdfAnnotation la1      = ((PdfLinkAnnotation) new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0)).SetHighlightMode(
                                          PdfAnnotation.HIGHLIGHT_INVERT).SetAction(js)).SetBorderStyle(PdfAnnotation.STYLE_UNDERLINE);
            Link link1 = new Link("here", (PdfLinkAnnotation)la1);

            document.Add(new Paragraph().Add("Click ").Add(link1).Add(" if you want to be scared."));
            PdfAnnotation la2 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0)).SetDestination(PdfExplicitDestination
                                                                                                .CreateFit(2)).SetHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH).SetBorderStyle(PdfAnnotation.STYLE_INSET
                                                                                                                                                                             );
            Link link2 = new Link("next page", (PdfLinkAnnotation)la2);

            document.Add(new Paragraph().Add("Go to the ").Add(link2).Add(" if you're too scared."));
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            document.Add(new Paragraph().Add("There, there, everything is OK."));
            document.Close();
        }
        private static void MergePdfs(List <string> InFiles, string OutFile)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile));

            pdfDoc.InitializeOutlines();

            //PdfMerger merger = new PdfMerger(pdfDoc, false, false);

            PdfOutline rootOutline = pdfDoc.GetOutlines(true);
            string     parentTitle = Path.GetFileNameWithoutExtension(OutFile);
            PdfOutline parent      = rootOutline.AddOutline(parentTitle);


            //parent.AddAction(PdfAction.CreateGoTo(
            //        PdfExplicitRemoteGoToDestination.CreateFit(1)));



            int pageNumber = 0;

            foreach (var srcFile in InFiles)
            {
                string title = Path.GetFileNameWithoutExtension(srcFile);

                PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile));

                int pageCount = firstSourcePdf.GetNumberOfPages();

                for (int i = 1; i < pageCount + 1; i++)
                {
                    var page       = firstSourcePdf.GetPage(i);
                    int pageRotate = page.GetRotation();
                    if (pageRotate != 0)
                    {
                        page.SetRotation(0);
                    }
                    PdfPage newPage = firstSourcePdf.GetPage(i).CopyTo(pdfDoc);
                    pdfDoc.AddPage(newPage);
                }

                //merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());

                //firstSourcePdf.CopyPagesTo(1, firstSourcePdf.GetNumberOfPages(), pdfDoc);
                //int all = pdfDoc.GetNumberOfPages();

                PdfExplicitDestination dd = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(pageNumber + 1));
                string tt = Guid.NewGuid().ToString();
                pdfDoc.AddNamedDestination(tt, dd.GetPdfObject());

                PdfOutline kid = parent.AddOutline(title);
                kid.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(tt)));
                //kid.AddAction(PdfAction.CreateGoTo(
                //PdfExplicitRemoteGoToDestination.CreateFit(pageNumber)));

                var bb = parent.GetAllChildren();

                pageNumber += pageCount;
                firstSourcePdf.Close();

                if (pageCount % 2 == 1)
                {
                    pdfDoc.AddNewPage(iTextPageSize.A4);
                    pageNumber += 1;
                }
            }



            PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage());
            string stringDest = Guid.NewGuid().ToString();

            pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject());
            parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest)));

            if (pdfDoc.GetNumberOfPages() % 2 == 1)
            {
                pdfDoc.AddNewPage(pageNumber, iTextPageSize.A4);
            }

            pdfDoc.Close();
        }