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.º 2
0
            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);
                String name = "dest" + (counter++);

                int pageNumber = occupiedArea.GetPageNumber();

                toc.Add(new KeyValuePair <String, KeyValuePair <String, int> >(((Text)modelElement).GetText(),
                                                                               new KeyValuePair <String, int>(name, pageNumber)));

                PdfPage page = drawContext.GetDocument().GetPage(pageNumber);

                drawContext.GetDocument().AddNamedDestination(name,
                                                              PdfExplicitDestination.CreateFitH(page, page.GetPageSize().GetTop()).GetPdfObject());
            }
        public virtual void CreatePdf(String dest)
        {
            PdfDocument    pdf      = new PdfDocument(new PdfWriter(dest));
            Document       document = new Document(pdf);
            PdfDestination jekyll   = PdfExplicitDestination.CreateFitH(1, 416);
            PdfDestination hyde     = PdfExplicitDestination.CreateXYZ(1, 150, 516, 2);
            PdfDestination jekyll2  = PdfExplicitDestination.CreateFitR(2, 50, 380, 130, 440);

            document.Add(new Paragraph().Add(new Link("Link to Dr. Jekyll", jekyll)));
            document.Add(new Paragraph().Add(new Link("Link to Mr. Hyde", hyde)));
            document.Add(new Paragraph().Add(new Link("Link to Dr. Jekyll on page 2", jekyll2)));
            document.Add(new Paragraph().SetFixedPosition(50, 400, 80).Add("Dr. Jekyll"));
            document.Add(new Paragraph().SetFixedPosition(150, 500, 80).Add("Mr. Hyde"));
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            document.Add(new Paragraph().SetFixedPosition(50, 400, 80).Add("Dr. Jekyll on page 2"));
            document.Close();
        }
Ejemplo n.º 4
0
 /// <summary>Creates a GoToR action, or remote action (section 12.6.4.3 of ISO 32000-1).</summary>
 /// <param name="filename">the remote destination file to jump to</param>
 /// <param name="pageNum">the remote destination document page to jump to</param>
 /// <param name="newWindow">a flag specifying whether to open the destination document in a new window</param>
 /// <returns>created action</returns>
 public static iText.Kernel.Pdf.Action.PdfAction CreateGoToR(String filename, int pageNum, bool newWindow)
 {
     return(CreateGoToR(new PdfStringFS(filename), PdfExplicitDestination.CreateFitH(pageNum, 10000), newWindow
                        ));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Updates the links.
        /// </summary>
        /// <param name="pdfFilePath">The PDF file path.</param>
        /// <param name="htmlToPdfFiles">The HTML to PDF files.</param>
        /// <param name="logger">The logger.</param>
        internal static void UpdateLinks(
            string pdfFilePath,
            IReadOnlyCollection <HtmlToPdfFile> htmlToPdfFiles,
            ILogger logger)
        {
            string tempFilePath = Path.GetTempFileName();

            using (PdfReader pdfReader = new PdfReader(pdfFilePath))
            {
                using (PdfWriter pdfWriter = new PdfWriter(tempFilePath))
                {
                    using (iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader, pdfWriter))
                    {
                        int pageCount = pdfDocument.GetNumberOfPages();
                        for (int i = 1; i <= pageCount; i++)
                        {
                            // get page
                            PdfPage pdfPage = pdfDocument.GetPage(i);

                            // get link annotations
                            IEnumerable <PdfLinkAnnotation> linkAnnotations = pdfPage.GetAnnotations().OfType <PdfLinkAnnotation>();
                            foreach (PdfLinkAnnotation linkAnnotation in linkAnnotations)
                            {
                                // get action
                                PdfDictionary action = linkAnnotation.GetAction();
                                if (action == null)
                                {
                                    continue;
                                }

                                PdfName s = action.GetAsName(PdfName.S);
                                if (s != PdfName.URI)
                                {
                                    continue;
                                }

                                PdfString uriPdfString = action.GetAsString(PdfName.URI);
                                if (!Uri.TryCreate(uriPdfString.GetValue(), UriKind.RelativeOrAbsolute, out Uri uri))
                                {
                                    continue;
                                }

                                if (!uri.IsFile)
                                {
                                    continue;
                                }

                                string htmlFilePath = uri.LocalPath.ToLower();

                                if (!htmlToPdfFiles.Any(x => string.Compare(x.Input, htmlFilePath, StringComparison.OrdinalIgnoreCase) == 0))
                                {
                                    // ex. when printing PDF from TOC.html by itself
                                    logger.LogDebug($"Could not find '{htmlFilePath}'. Referenced in '{pdfFilePath}' on page {i}.");
                                    continue;
                                }

                                HtmlToPdfFile linkedHtmlToPdfFile = htmlToPdfFiles.Single(x => x.Input == htmlFilePath);
                                int           linkedPageNumber    = linkedHtmlToPdfFile.OutputPdfFilePageNumber;

                                PdfPage linkedPage;
                                try
                                {
                                    // http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfDestination.html
                                    linkedPage = pdfDocument.GetPage(linkedPageNumber);
                                }
                                catch (Exception ex)
                                {
                                    throw new PdfPageNotFoundException(linkedPageNumber, linkedHtmlToPdfFile.Input, ex);
                                }

                                float top = linkedPage.GetPageSize().GetTop();
                                PdfExplicitDestination destination = PdfExplicitDestination.CreateFitH(linkedPage, top);
                                PdfAction newAction = PdfAction.CreateGoTo(destination);

                                linkAnnotation.SetAction(newAction);
                            }
                        }
                    }
                }
            }

            File.Delete(pdfFilePath);
            File.Move(tempFilePath, pdfFilePath);
        }