/**************************************************************************/

        public List <KeyValuePair <string, string> > GetOutLinks()
        {
            int TotalPages = this.Pdf.GetNumberOfPages();
            List <KeyValuePair <string, string> > OutLinks = new List <KeyValuePair <string, string> >(TotalPages);

            for (int i = 1; i <= TotalPages; i++)
            {
                PdfPage Page             = this.Pdf.GetPage(i);
                int     AnnotationsCount = Page.GetAnnotations().Count;

                if (AnnotationsCount > 0)
                {
                    IList <PdfAnnotation> Annotations = Page.GetAnnotations();

                    foreach (PdfAnnotation Annotation in Annotations)
                    {
                        if (Annotation.GetSubtype().Equals(PdfName.Link))
                        {
                            PdfLinkAnnotation LinkAnnotation = (PdfLinkAnnotation)Annotation;

                            if (LinkAnnotation.GetAction().Get(PdfName.URI) != null)
                            {
                                string Url = LinkAnnotation.GetAction().Get(PdfName.URI).ToString();

                                if ((!string.IsNullOrEmpty(Url)) && (!string.IsNullOrWhiteSpace(Url)))
                                {
                                    try
                                    {
                                        Uri ParsedUri = new Uri(Url);
                                        if (ParsedUri.Scheme.ToLower().Equals("http") || ParsedUri.Scheme.ToLower().Equals("https"))
                                        {
                                            KeyValuePair <string, string> LinkPair = new KeyValuePair <string, string>(
                                                LinkAnnotation.GetAction().Get(PdfName.URI).ToString(),
                                                "PDF Annotation Link"
                                                );
                                            OutLinks.Add(LinkPair);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        this.DebugMsg(ex.Message);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(OutLinks);
        }
示例#2
0
        public void OneFile_LinksToTheNextFile_UpdatesLink(string exeFileName)
        {
            HtmlToPdfRunner runner = new HtmlToPdfRunner(exeFileName);

            string htmlFile2Contents = @"
<html>
  <head>
  </head>
  <body>
   Page 2
  </body>
</html>";

            using (TempHtmlFile htmlFile2 = new TempHtmlFile(htmlFile2Contents))
            {
                string htmlFile1Contents = $@"
<html>
  <head>
  </head>
  <body>
   Page 1
   <br/>
   <a href=""{htmlFile2.FilePath}"">Page 2</a>
  </body>
</html>";
                using (TempHtmlFile htmlFile1 = new TempHtmlFile(htmlFile1Contents))
                {
                    using (TempPdfFile pdfFile = new TempPdfFile(this.TestContext))
                    {
                        string             commandLine = $"\"{htmlFile1.FilePath}\" \"{htmlFile2.FilePath}\" \"{pdfFile.FilePath}\"";
                        HtmlToPdfRunResult result      = runner.Run(commandLine);
                        Assert.AreEqual(0, result.ExitCode, result.Output);

                        using (PdfReader pdfReader = new PdfReader(pdfFile.FilePath))
                        {
                            using (PdfDocument pdfDocument = new PdfDocument(pdfReader))
                            {
                                Assert.AreEqual(2, pdfDocument.GetNumberOfPages());

                                // get the first page
                                PdfPage pdfPage = pdfDocument.GetPage(1);

                                // get link annotations
                                List <PdfLinkAnnotation> linkAnnotations = pdfPage.GetAnnotations().OfType <PdfLinkAnnotation>().ToList();
                                Assert.AreEqual(1, linkAnnotations.Count);

                                // get the first link annotation
                                PdfLinkAnnotation linkAnnotation = linkAnnotations.ElementAt(0);
                                Assert.IsNotNull(linkAnnotation);

                                // get action
                                PdfDictionary action = linkAnnotation.GetAction();
                                Assert.IsNotNull(action);

                                // get GoTo sub-type
                                PdfName s = action.GetAsName(PdfName.S);

                                if (exeFileName == HtmlToPdfRunner.HtmlToPdfExe)
                                {
                                    Assert.AreEqual(PdfName.GoTo, s);

                                    // get destination
                                    PdfArray             destination = action.GetAsArray(PdfName.D);
                                    PdfIndirectReference destinationPageReference = destination.GetAsDictionary(0).GetIndirectReference();
                                    PdfName   zoom       = destination.GetAsName(1);
                                    PdfNumber pageOffset = destination.GetAsNumber(2);

                                    // get expected values
                                    PdfPage              pdfPage2              = pdfDocument.GetPage(2);
                                    PdfDictionary        page2Dictionary       = pdfPage2.GetPdfObject();
                                    PdfIndirectReference expectedPageReference = page2Dictionary.GetIndirectReference();
                                    PdfName              expectedZoom          = PdfName.FitH;
                                    float expectedPageOffset = pdfPage2.GetPageSize().GetTop();

                                    // assert
                                    Assert.AreEqual(expectedPageReference, destinationPageReference);
                                    Assert.AreEqual(expectedZoom, zoom);
                                    Assert.AreEqual(expectedPageOffset, pageOffset.FloatValue());
                                }
                                else if (exeFileName == HtmlToPdfRunner.WkhtmltopdfExe)
                                {
                                    Assert.AreEqual(PdfName.URI, s);

                                    PdfString uri = action.GetAsString(PdfName.URI);
                                    Assert.AreEqual(htmlFile2.FilePath, HttpUtility.UrlDecode(uri.ToString()));
                                }
                            }
                        }
                    }
                }
            }
        }