【问题标题】:How To find Cross References(Internal Links) In Pdf File Using ItextSharp Lib如何使用 ItextSharp Lib 在 Pdf 文件中查找交叉引用(内部链接)
【发布时间】:2014-02-22 09:54:00
【问题描述】:

您好,我正在使用 ItextSharp 在 pdf 文件中搜索交叉引用(内部链接)。我已经完成了外部链接。

如果您有任何解决方案,请发布。

//Get the current page
PdfDictionary PageDictionary = R.GetPageN(page);

//Get all of the annotations for the current page
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);

//Make sure we have something
if ((Annots == null) || (Annots.Length == 0))
// return null;
{

    Console.WriteLine("nothing");
}

//Loop through each annotation
if (Annots != null)
{

    foreach (PdfObject A in Annots.ArrayList)
    {
        //Convert the itext-specific object as a generic PDF object
        PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);

        //Make sure this annotation has a link
        if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
            continue;

        //Make sure this annotation has an ACTION
        if (AnnotationDictionary.Get(PdfName.A) == null)
            continue;

        //Get the ACTION for the current annotation
        PdfDictionary AnnotationAction = AnnotationDictionary.GetAsDict(PdfName.A);
        //  PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);

        //Test if it is a URI action (There are tons of other types of actions, some of which might mimic URI, such as JavaScript, but those need to be handled seperately)
        if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
        {
            PdfString Destination = AnnotationAction.GetAsString(PdfName.URI);

            string url1 = Destination.ToString();
        }
    }
}

【问题讨论】:

  • 向我们展示您是如何找到外部链接的,我们将告诉您必须找到内部链接。 (只有很小的区别。)预计其他 cmets 会问到目前为止您尝试了什么?
  • 嗨,布鲁诺,请检查我更新的代码
  • 好的,您已经非常接近解决方案了。看我的回答。

标签: c#-4.0 itextsharp


【解决方案1】:

您已经完成了大部分工作。请看以下屏幕截图:

您会看到页面的/Annots 数组。您已经在代码中解析了该数组,并且跳过了所有不属于 /Subtype /Link 或没有 /A 键的注释,这非常好。

目前,您只查找 /S 类型为 /URI 的值。您您已经完成了外部链接,但事实并非如此:您还应该查找/S/GoToR 的条目(远程转到)。如果您想要内部链接,您需要寻找等于/GoTo/GoToE 和(将来)/GoToDp/S 值。也许您还想删除 /JavaScript 操作,因为它们也可用于跳转到特定页面。

请下载The ABC of PDF 并查看表 3.11 了解更多信息。 (本书免费提供。)

【讨论】:

  • 嗨,布鲁诺,你的意思是说我需要在这里更改,因为我更改了,但它没有提供内部链接(交叉引用)请帮助“if (AnnotationAction.Get(PdfName.S).Equals(PdfName. GOTOE)) { PdfString Destination1 = AnnotationAction.GetAsString(PdfName.GOTOE);"
  • 向我们展示 PDF。请注意,您在评论中仅提及 GoToE。这是一个嵌入文件的链接,您不会找到很多包含它们的 PDF。您会发现更多将/GoTo 作为/S 条目值的内部链接。你没看我书的表 3.11 吗?
  • 布鲁诺我读过书,但是当我提到 (pdfName.s)equals(pdfname.goto) 但我找不到任何内部链接时,你能帮我写一些代码吗?
  • 如果您不共享 PDF,我无法帮助您。例如:也许内部链接是使用 JavaScript 创建的(如我的回答中所述)。您是否使用 RUPS(我用来截屏的工具)查看了您的文件?
  • 我不会在 StackOverflow 上分享我的邮件,你也不应该(你也不应该大喊大叫)。如果您想联系 iText 的某个人,请访问他们的网站:itextpdf.com/support
猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2011-10-12
  • 2022-01-11
  • 2014-08-12
  • 2016-10-05
相关资源
最近更新 更多