如何使用ItextSharp Lib在Pdf文件中查找交叉引用(内部链接)

时间:2014-02-22 09:54:01

标签: c#-4.0 itextsharp

您好我正在使用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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你已经完成了大部分工作。请看下面的截图:

enter image description here

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

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

请下载The ABC of PDF并查看表3.11以获取更多信息。 (这本书是免费提供的。)