简体   繁体   English

如何修改现有 PDF 中的外部链接,使其指向使用 IText7 和 C# 的文档中的内部页面?

[英]How do I modify external link in an existing PDF such that it points to an internal page within document using IText7 and C#?

I have an existing PDF file which has links to external PDF files.我有一个现有的 PDF 文件,其中包含指向外部 PDF 文件的链接。 I want to edit these links to make them point to pages within the same PDF document.我想编辑这些链接,使它们指向同一个 PDF 文档中的页面。 This functionality was working previously with iTextsharp, now I'm migrating to iText7 but can't get it to work.此功能以前与 iTextsharp 一起使用,现在我正在迁移到 iText7 但无法使其正常工作。

Below is a sample code that I have tried and feel is very close to the solution but something is missing.下面是我尝试过的示例代码,感觉非常接近解决方案,但缺少一些东西。 This code basically loads up a PDF with 2 pages.这段代码基本上加载了一个有 2 页的 PDF。 The first page has around 15 links all pointing to an external file.第一页有大约 15 个链接,都指向一个外部文件。 I'm trying to edit the links so that they take the user to page 2 of the same document.我正在尝试编辑链接,以便它们将用户带到同一文档的第 2 页。 I am able to load all the links and query their values, but changing them doesn't happen.我能够加载所有链接并查询它们的值,但不会更改它们。

    private bool MakeLinksInternal(string inputFile)
    {
      if (Path.GetExtension(inputFile).ToLower() != ".pdf")
        return false;

      using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile)))
      {
        //get the index page
        PdfPage pdfPage = pdfDocument.GetPage(1);
        //get all of the link annotations for the current page
        var annots = pdfPage.GetAnnotations().Where(a => a.GetSubtype().Equals(PdfName.Link));

        //Make sure we have something
        if ((annots == null) || (annots.Count() == 0))
          return true;

        foreach (PdfLinkAnnotation linkAnnotation in annots)
        {
          //get action associated to the annotation
          var action = linkAnnotation.GetAction();
          if (action == null)
            continue;

          // Test if it is a URI action 
          if (action.Get(PdfName.S).Equals(PdfName.URI)
            || action.Get(PdfName.S).Equals(PdfName.GoToR))
          {
            action.Remove(PdfName.S);

            action.Put(PdfName.S, PdfName.GoTo);

            var newLocalDestination = new PdfArray();
            newLocalDestination.Add(pdfDocument.GetPage(2).GetPdfObject());
            newLocalDestination.Add(PdfName.Fit);

            action.Put(PdfName.D, newLocalDestination);
          }

        }
        pdfDocument.Close();
      }

      return true;
    }

This is my first question in stackoverflow so please be kind if I have made any mistakes in creating this post.这是我在stackoverflow中的第一个问题,所以如果我在创建这篇文章时犯了任何错误,请多多关照。

You create the PdfDocument like this您像这样创建PdfDocument

using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile)))

This creates a PdfDocument for reading only.这将创建一个只读的PdfDocument If you want to also write the changes you apply, you have to use如果您还想编写您应用的更改,则必须使用

using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile), new PdfWriter(outputFile)))

You should use different names for inputFile and outputFile .您应该为inputFileoutputFile使用不同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM