从pdf文件中读取超链接

我正在尝试阅读pdf文件并从此文件中获取所有超链接。 我正在使用iTextSharp用于C#.net。

PdfReader reader = new PdfReader("test.pdf"); List list = reader.GetLinks(36); 

这个方法“GetLinks”返回一个包含大量链接信息的列表,但是这个方法没有返回我想要的值,超链接字符串我确切知道第36页有超链接

PdfReader.GetLinks()仅用于文档内部的链接,而不是外部超链接。 为什么? 我不知道。

下面的代码基于我之前编写的代码,但我将其限制为存储在PDF中的链接作为PdfName.URI 。 它可以将链接存储为最终执行相同操作的Javascript,并且可能还有其他类型,但您需要检测它。 我不相信规范中有任何内容表明链接实际上需要是一个URI,它只是暗示,所以下面的代码返回一个你可以(可能)自己转换为URI的字符串。

  private static List GetPdfLinks(string file, int page) { //Open our reader PdfReader R = new PdfReader(file); //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; List Ret = new List(); //Loop through each annotation 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 = (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); if (Destination != null) Ret.Add(Destination.ToString()); } } return Ret; } 

并称之为:

  string myfile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf"); List Links = GetPdfLinks(myfile, 1); 

我注意到,PDF上看起来像URL的任何文本都可以被PDF vewer模拟为注释链接。 在Adobe Acrobat中,在名为“从URL创建链接”的常规选项卡下有一个页面显示首选项,用于控制此选项。 我正在编写代码来删除URL链接注释,但却发现没有。 但是Acrobat会自动将看起来像URL的文本转换为看似注释链接的内容。