Esempio n. 1
0
        /// <summary>Adds the outline.</summary>
        /// <param name="tagWorker">the tag worker</param>
        /// <param name="element">the element</param>
        /// <param name="context">the processor context</param>
        /// <returns>the outline handler</returns>
        internal virtual OutlineHandler AddOutline(ITagWorker tagWorker, IElementNode element, ProcessorContext context
                                                   )
        {
            String tagName = element.Name();

            if (null != tagWorker && HasTagPriorityMapping(tagName) && context.GetPdfDocument() != null)
            {
                int level = (int)GetTagPriorityMapping(tagName);
                if (null == currentOutline)
                {
                    currentOutline = context.GetPdfDocument().GetOutlines(false);
                }
                PdfOutline parent = currentOutline;
                while (!levelsInProcess.IsEmpty() && level <= levelsInProcess.JGetFirst())
                {
                    parent = parent.GetParent();
                    levelsInProcess.JRemoveFirst();
                }
                String content = ((JsoupElementNode)element).Text();
                if (String.IsNullOrEmpty(content))
                {
                    content = GetUniqueID(tagName);
                }
                PdfOutline outline     = parent.AddOutline(content);
                String     destination = DESTINATION_PREFIX + GetUniqueID(DESTINATION_PREFIX);
                outline.AddDestination(PdfDestination.MakeDestination(new PdfString(destination)));
                destinationsInProcess.AddFirst(destination);
                levelsInProcess.AddFirst(level);
                currentOutline = outline;
            }
            return(this);
        }
Esempio n. 2
0
        public virtual void LinkTest02()
        {
            String      outFileName = destinationFolder + "linkTest02.pdf";
            String      cmpFileName = sourceFolder + "cmp_linkTest02.pdf";
            FileStream  file        = new FileStream(outFileName, FileMode.Create);
            PdfWriter   writer      = new PdfWriter(file);
            PdfDocument pdfDoc      = new PdfDocument(writer);
            Document    doc         = new Document(pdfDoc);

            doc.Add(new AreaBreak()).Add(new AreaBreak());
            PdfArray array = new PdfArray();

            array.Add(doc.GetPdfDocument().GetPage(1).GetPdfObject());
            array.Add(PdfName.XYZ);
            array.Add(new PdfNumber(36));
            array.Add(new PdfNumber(100));
            array.Add(new PdfNumber(1));
            PdfDestination dest   = PdfDestination.MakeDestination(array);
            PdfAction      action = PdfAction.CreateGoTo(dest);
            Link           link   = new Link("TestLink", action);

            doc.Add(new Paragraph(link));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Esempio n. 3
0
        private void AddOutlineToPage(PdfOutline outline, PdfDictionary item, IDictionary <String, PdfObject> names
                                      )
        {
            PdfObject dest = item.Get(PdfName.Dest);

            if (dest != null)
            {
                PdfDestination destination = PdfDestination.MakeDestination(dest);
                outline.SetDestination(destination);
                AddOutlineToPage(outline, names);
            }
            else
            {
                //Take into account outlines that specify their destination through an action
                PdfDictionary action = item.GetAsDictionary(PdfName.A);
                if (action != null)
                {
                    PdfName actionType = action.GetAsName(PdfName.S);
                    //Check if it is a go to action
                    if (PdfName.GoTo.Equals(actionType))
                    {
                        //Retrieve destination if it is.
                        PdfObject destObject = action.Get(PdfName.D);
                        if (destObject != null)
                        {
                            //Page is always the first object
                            PdfDestination destination = PdfDestination.MakeDestination(destObject);
                            outline.SetDestination(destination);
                            AddOutlineToPage(outline, names);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            string titleDestination = "title";

            PdfFont   font  = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLDOBLIQUE);
            Paragraph title = new Paragraph("This is the title with the font HELVETICA 16")
                              .SetFont(font).SetFontSize(16);

            title.SetDestination(titleDestination);
            doc.Add(title);

            // It is an alternative for iText5 Chapter class, because
            // iText5 Chapter class also creates bookmarks automatically.
            PdfOutline root = pdfDoc.GetOutlines(false);

            root.AddOutline("This is the title")
            .AddDestination(PdfDestination.MakeDestination(new PdfString(titleDestination)));

            Paragraph p = new Paragraph("This is the paragraph with the default font");

            doc.Add(p);

            doc.Close();
        }
Esempio n. 5
0
        private void GetNextItem(PdfDictionary item, PdfOutline parent, IDictionary <String, PdfObject> names)
        {
            if (null == item)
            {
                return;
            }
            PdfOutline outline = new PdfOutline(item.GetAsString(PdfName.Title).ToUnicodeString(), item, parent);
            PdfObject  dest    = item.Get(PdfName.Dest);

            if (dest != null)
            {
                PdfDestination destination = PdfDestination.MakeDestination(dest);
                outline.SetDestination(destination);
                AddOutlineToPage(outline, names);
            }
            parent.GetAllChildren().Add(outline);
            PdfDictionary processItem = item.GetAsDictionary(PdfName.First);

            if (processItem != null)
            {
                GetNextItem(processItem, outline, names);
            }
            processItem = item.GetAsDictionary(PdfName.Next);
            if (processItem != null)
            {
                GetNextItem(processItem, parent, names);
            }
        }
Esempio n. 6
0
        /// <summary>Adds the outline and the destination.</summary>
        /// <remarks>
        /// Adds the outline and the destination.
        /// Adds the outline and its corresponding the destination to the PDF document
        /// if the priority mapping is set for the element.
        /// </remarks>
        /// <param name="tagWorker">the tag worker</param>
        /// <param name="element">the element</param>
        /// <param name="context">the processor context</param>
        /// <returns>the outline handler</returns>
        internal virtual OutlineHandler AddOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode element, ProcessorContext
                                                                    context)
        {
            String tagName = element.Name();

            if (null != tagWorker && HasTagPriorityMapping(tagName) && context.GetPdfDocument() != null)
            {
                int level = (int)GetTagPriorityMapping(tagName);
                if (null == currentOutline)
                {
                    currentOutline = context.GetPdfDocument().GetOutlines(false);
                }
                PdfOutline parent = currentOutline;
                while (!levelsInProcess.IsEmpty() && level <= levelsInProcess.JGetFirst())
                {
                    parent = parent.GetParent();
                    levelsInProcess.JRemoveFirst();
                }
                PdfOutline outline     = parent.AddOutline(GenerateUniqueOutlineName(element));
                String     destination = GenerateUniqueDestinationName(element);
                outline.AddDestination(PdfDestination.MakeDestination(new PdfString(destination)));
                destinationsInProcess.AddFirst(destination);
                levelsInProcess.AddFirst(level);
                currentOutline = outline;
            }
            return(this);
        }
Esempio n. 7
0
        public virtual void MakeDestination01Test()
        {
            String      srcFile     = sourceFolder + "cmp_structureDestination01Test.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfReader(srcFile));
            PdfObject   destObj     = ((PdfLinkAnnotation)pdfDocument.GetPage(2).GetAnnotations()[0]).GetAction().Get(PdfName
                                                                                                                      .D);
            PdfDestination destWrapper = PdfDestination.MakeDestination(destObj);

            NUnit.Framework.Assert.AreEqual(typeof(PdfStructureDestination), destWrapper.GetType());
        }
        public virtual PdfOutline CreateOutline(PdfOutline outline, PdfDocument pdf, String title, String name)
        {
            if (outline == null)
            {
                outline = pdf.GetOutlines(false);
                outline = outline.AddOutline(title);
                outline.AddDestination(PdfDestination.MakeDestination(new PdfString(name)));
                return(outline);
            }
            PdfOutline kid = outline.AddOutline(title);

            kid.AddDestination(PdfDestination.MakeDestination(new PdfString(name)));
            return(outline);
        }
Esempio n. 9
0
        private void GetNextItem(PdfDictionary item, PdfOutline parent, IDictionary <String, PdfObject> names)
        {
            if (null == item)
            {
                return;
            }
            PdfOutline outline = new PdfOutline(item.GetAsString(PdfName.Title).ToUnicodeString(), item, parent);
            PdfObject  dest    = item.Get(PdfName.Dest);

            if (dest != null)
            {
                PdfDestination destination = PdfDestination.MakeDestination(dest);
                outline.SetDestination(destination);
                AddOutlineToPage(outline, names);
            }
            else
            {
                //Take into account outlines that specify their destination through an action
                PdfDictionary action = item.GetAsDictionary(PdfName.A);
                if (action != null)
                {
                    PdfName actionType = action.GetAsName(PdfName.S);
                    //Check if it a go to action
                    if (PdfName.GoTo.Equals(actionType))
                    {
                        //Retrieve destination if it is.
                        PdfObject destObject = action.Get(PdfName.D);
                        if (destObject != null)
                        {
                            //Page is always the first object
                            PdfDestination destination = PdfDestination.MakeDestination(destObject);
                            outline.SetDestination(destination);
                            AddOutlineToPage(outline, names);
                        }
                    }
                }
            }
            parent.GetAllChildren().Add(outline);
            PdfDictionary processItem = item.GetAsDictionary(PdfName.First);

            if (processItem != null)
            {
                GetNextItem(processItem, outline, names);
            }
            processItem = item.GetAsDictionary(PdfName.Next);
            if (processItem != null)
            {
                GetNextItem(processItem, parent, names);
            }
        }
Esempio n. 10
0
        public virtual void AddOutlinesWithNamedDestinations01()
        {
            PdfReader reader = new PdfReader(new FileStream(sourceFolder + "iphone_user_guide.pdf", FileMode.Open, FileAccess.Read
                                                            ));
            String      filename = destinationFolder + "outlinesWithNamedDestinations01.pdf";
            FileStream  fos      = new FileStream(filename, FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos);
            PdfDocument pdfDoc   = new PdfDocument(reader, writer);
            PdfArray    array1   = new PdfArray();

            array1.Add(pdfDoc.GetPage(2).GetPdfObject());
            array1.Add(PdfName.XYZ);
            array1.Add(new PdfNumber(36));
            array1.Add(new PdfNumber(806));
            array1.Add(new PdfNumber(0));
            PdfArray array2 = new PdfArray();

            array2.Add(pdfDoc.GetPage(3).GetPdfObject());
            array2.Add(PdfName.XYZ);
            array2.Add(new PdfNumber(36));
            array2.Add(new PdfNumber(806));
            array2.Add(new PdfNumber(1.25));
            PdfArray array3 = new PdfArray();

            array3.Add(pdfDoc.GetPage(4).GetPdfObject());
            array3.Add(PdfName.XYZ);
            array3.Add(new PdfNumber(36));
            array3.Add(new PdfNumber(806));
            array3.Add(new PdfNumber(1));
            pdfDoc.AddNamedDestination("test1", array2);
            pdfDoc.AddNamedDestination("test2", array3);
            pdfDoc.AddNamedDestination("test3", array1);
            PdfOutline root         = pdfDoc.GetOutlines(false);
            PdfOutline firstOutline = root.AddOutline("Test1");

            firstOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test1")));
            PdfOutline secondOutline = root.AddOutline("Test2");

            secondOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test2")));
            PdfOutline thirdOutline = root.AddOutline("Test3");

            thirdOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test3")));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, sourceFolder + "cmp_outlinesWithNamedDestinations01.pdf"
                                                                             , destinationFolder, "diff_"));
        }
Esempio n. 11
0
        public virtual void AddOutlinesWithNamedDestinations01()
        {
            String      filename = DESTINATION_FOLDER + "outlinesWithNamedDestinations01.pdf";
            PdfReader   reader   = new PdfReader(SOURCE_FOLDER + "iphone_user_guide.pdf");
            PdfWriter   writer   = new PdfWriter(filename);
            PdfDocument pdfDoc   = new PdfDocument(reader, writer);
            PdfArray    array1   = new PdfArray();

            array1.Add(pdfDoc.GetPage(2).GetPdfObject());
            array1.Add(PdfName.XYZ);
            array1.Add(new PdfNumber(36));
            array1.Add(new PdfNumber(806));
            array1.Add(new PdfNumber(0));
            PdfArray array2 = new PdfArray();

            array2.Add(pdfDoc.GetPage(3).GetPdfObject());
            array2.Add(PdfName.XYZ);
            array2.Add(new PdfNumber(36));
            array2.Add(new PdfNumber(806));
            array2.Add(new PdfNumber(1.25));
            PdfArray array3 = new PdfArray();

            array3.Add(pdfDoc.GetPage(4).GetPdfObject());
            array3.Add(PdfName.XYZ);
            array3.Add(new PdfNumber(36));
            array3.Add(new PdfNumber(806));
            array3.Add(new PdfNumber(1));
            pdfDoc.AddNamedDestination("test1", array2);
            pdfDoc.AddNamedDestination("test2", array3);
            pdfDoc.AddNamedDestination("test3", array1);
            PdfOutline root         = pdfDoc.GetOutlines(false);
            PdfOutline firstOutline = root.AddOutline("Test1");

            firstOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test1")));
            PdfOutline secondOutline = root.AddOutline("Test2");

            secondOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test2")));
            PdfOutline thirdOutline = root.AddOutline("Test3");

            thirdOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("test3")));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, SOURCE_FOLDER + "cmp_outlinesWithNamedDestinations01.pdf"
                                                                             , DESTINATION_FOLDER, "diff_"));
        }
Esempio n. 12
0
        public virtual void AddOutlinesWithNamedDestinations02()
        {
            String      filename = destinationFolder + "outlinesWithNamedDestinations02.pdf";
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(filename));
            PdfArray    array1   = new PdfArray();

            array1.Add(pdfDoc.AddNewPage().GetPdfObject());
            array1.Add(PdfName.XYZ);
            array1.Add(new PdfNumber(36));
            array1.Add(new PdfNumber(806));
            array1.Add(new PdfNumber(0));
            PdfArray array2 = new PdfArray();

            array2.Add(pdfDoc.AddNewPage().GetPdfObject());
            array2.Add(PdfName.XYZ);
            array2.Add(new PdfNumber(36));
            array2.Add(new PdfNumber(806));
            array2.Add(new PdfNumber(1.25));
            PdfArray array3 = new PdfArray();

            array3.Add(pdfDoc.AddNewPage().GetPdfObject());
            array3.Add(PdfName.XYZ);
            array3.Add(new PdfNumber(36));
            array3.Add(new PdfNumber(806));
            array3.Add(new PdfNumber(1));
            pdfDoc.AddNamedDestination("page1", array2);
            pdfDoc.AddNamedDestination("page2", array3);
            pdfDoc.AddNamedDestination("page3", array1);
            PdfOutline root         = pdfDoc.GetOutlines(false);
            PdfOutline firstOutline = root.AddOutline("Test1");

            firstOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("page1")));
            PdfOutline secondOutline = root.AddOutline("Test2");

            secondOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("page2")));
            PdfOutline thirdOutline = root.AddOutline("Test3");

            thirdOutline.AddDestination(PdfDestination.MakeDestination(new PdfString("page3")));
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, sourceFolder + "cmp_outlinesWithNamedDestinations02.pdf"
                                                                             , destinationFolder, "diff_"));
        }
Esempio n. 13
0
        public virtual void LinkTest03()
        {
            String      outFileName = destinationFolder + "linkTest03.pdf";
            String      cmpFileName = sourceFolder + "cmp_linkTest03.pdf";
            PdfDocument pdfDoc      = new PdfDocument(new PdfWriter(outFileName));
            Document    doc         = new Document(pdfDoc);
            PdfArray    array       = new PdfArray();

            array.Add(doc.GetPdfDocument().AddNewPage().GetPdfObject());
            array.Add(PdfName.XYZ);
            array.Add(new PdfNumber(36));
            array.Add(new PdfNumber(100));
            array.Add(new PdfNumber(1));
            PdfDestination dest = PdfDestination.MakeDestination(array);
            Link           link = new Link("TestLink", dest);

            link.SetAction(PdfAction.CreateURI("http://itextpdf.com/", false));
            doc.Add(new Paragraph(link));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Esempio n. 14
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();
        }
Esempio n. 15
0
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

            pdf.GetCatalog().SetOpenAction(PdfDestination.MakeDestination(new PdfString("toc")));
            pdf.GetCatalog().SetAdditionalAction(PdfName.WC, PdfAction.CreateJavaScript("app.alert('Thank you for reading');"
                                                                                        ));
            pdf.AddNewPage().SetAdditionalAction(PdfName.O, PdfAction.CreateJavaScript("app.alert('This is where it starts!');"
                                                                                       ));
            // 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, Util.Pair <String, int> > > toc = new List <Util.Pair
                                                                                  <String, 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);
                    toc.Add(new Util.Pair <string, Util.Pair <string, int> >(name, 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").SetDestination("toc");
            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, Util.Pair <String, int> > entry in toc)
            {
                Util.Pair <String, int> text = entry.Value;
                p = new Paragraph().AddTabStops(tabstops).Add(text.Key).Add(new Tab()).Add(text.Value.ToString()).SetAction
                        (PdfAction.CreateGoTo(entry.Key));
                document.Add(p);
            }
            PdfPage page = pdf.GetLastPage();

            page.SetAdditionalAction(PdfName.C, PdfAction.CreateJavaScript("app.alert('Goodbye last page!');"));
            //Close document
            document.Close();
        }