Ejemplo 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);
        }
Ejemplo n.º 2
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);
        }
            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);
                Rectangle      rect = this.GetOccupiedAreaBBox();
                PdfDestination dest = PdfExplicitDestination.CreateFitH(drawContext.GetDocument().GetLastPage(), rect.GetTop
                                                                            ());
                PdfOutline outline = this.parent.AddOutline(this.title);

                outline.AddDestination(dest);
            }
Ejemplo n.º 4
0
            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);
                Rectangle      rect = GetOccupiedAreaBBox();
                PdfPage        page = drawContext.GetDocument().GetPage(GetOccupiedArea().GetPageNumber());
                PdfDestination dest = PdfExplicitDestination.CreateXYZ(page, rect.GetLeft(), rect.GetTop(), 0);

                list.Add(new KeyValuePair <String, PdfDestination>(((Text)modelElement).GetText(), dest));

                PdfOutline curOutline = root.AddOutline(((Text)modelElement).GetText());

                curOutline.AddDestination(dest);
            }
        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);
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
0
        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);
                }
            }
        }
Ejemplo n.º 9
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();
        }