【问题标题】:Hyperlink Detection from PDFPDF中的超链接检测
【发布时间】:2014-06-09 17:30:12
【问题描述】:

我有一些包含 URL 和 mailto 形式的超链接的 PDF。现在是否有任何方法或工具(可能是第 3 方)从 PDF 中提取超链接元信息,如坐标、链接类型和目标地址。非常感谢任何帮助。

我已经尝试使用 iText 和 PDFBox,但没有取得重大成功,甚至一些第三方软件也没有为我提供所需的输出。

我使用 iText 在 Java 中尝试了以下代码

        PdfReader myReader = new PdfReader("pdf File Path");
        PdfDictionary pageDict = myReader.getPageN(1);
        PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS);
        System.out.println(annots);
        ArrayList<String> dests = new ArrayList<String>();
        if(annots != null) 
        {
            for(int i=0; i<annots.size(); ++i) 
            {
                PdfDictionary annotDict = annots.getAsDict(i);
                PdfName subType = annotDict.getAsName(PdfName.SUBTYPE);
                if (subType != null && PdfName.LINK.equals(subType)) 
                {
                    PdfDictionary action = annotDict.getAsDict(PdfName.A);
                    if(action != null && PdfName.URI.equals(action.getAsName(PdfName.S))) 
                    {
                        dests.add(action.getAsString(PdfName.URI).toString());
                    } // else { its an internal link }
                }
            }
        }        
        System.out.println(dests);

【问题讨论】:

  • @Bobrovsky 的答案中的示例使用 Doxotic 搜索链接注释,使用 iText 或 PDFBox 搜索链接注释的设计也类似。因此,您确定文档中的那些链接确实是链接注释吗?例如。 Adobe Reader 有一个选项可以使内容中的地址可点击,就好像它们是链接注释一样,而它们不是。也许这样的功能让你相信有链接注释,而实际上没有。 (顺便说一句,您可能想提供您尝试过的代码;也许它是错误的。)
  • 非常感谢 mkl 你已经完成了。实际上,我的代码运行良好,这是创建悬停链接的 Adob​​e 的属性。您能否提供 Adob​​e 的规格以创建此类属性,以便我检查它
  • Adobe Reader 只是在页面内容中搜索它认为的 URL 并使其具有交互性。您可以在首选项中打开和关闭此行为。我不知道要提供哪些规范
  • 干杯,我已经从“编辑”>“首选项”>“常规”选项中检查了它,然后取消选中“从 URL 创建链接”选项。再次感谢您的帮助。

标签: pdf hyperlink adobe itext pdf-extraction


【解决方案1】:

您可以使用Docotic.Pdf library 进行链接提取(免责声明:我为公司工作)。

以下是打开指定文件、查找所有超链接、收集每个链接的位置信息并在每个链接周围绘制矩形的代码。

之后,代码会创建新的 PDF(带有矩形链接)和包含收集信息的文本文件。最后,两个创建的文件都在默认查看器中打开。

public static void ListAndHighlightLinks(string inputFile, string outputFile, string outputTxt)
{
    using (PdfDocument doc = new PdfDocument(inputFile))
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < doc.Pages.Count; i++)
        {
            PdfPage page = doc.Pages[i];
            foreach (PdfWidget widget in page.Widgets)
            {
                PdfActionArea actionArea = widget as PdfActionArea;
                if (actionArea == null)
                    continue;

                PdfUriAction linkAction = actionArea.Action as PdfUriAction;
                if (linkAction == null)
                    continue;

                Uri url = linkAction.Uri;
                PdfRectangle rect = actionArea.BoundingBox;

                // add information about found link into string buffer
                sb.Append("Page ");
                sb.Append(i.ToString());
                sb.Append(" : ");
                sb.Append(rect.ToString());
                sb.Append(" ");
                sb.AppendLine(url.ToString());

                // draw rectangle around found link
                page.Canvas.DrawRectangle(rect);
            }
        }

        // save document with highlighted links and text information about links to files
        doc.Save(outputFile);
        System.IO.File.WriteAllText(outputTxt, sb.ToString());

        // open created PDF and text file in default viewers
        System.Diagnostics.Process.Start(outputTxt);
        System.Diagnostics.Process.Start(outputFile);
    }
}

您可以将示例代码与这样的调用一起使用:

ListAndHighlightLinks("input.pdf", "output.pdf", "links.txt");

【讨论】:

    【解决方案2】:

    如果您的 pdf 受版权保护,则需要从第 1 步开始,如果可以免费复制,则可以从第 2 步开始

    第 1 步:将您的 pdf 转换为 word .doc:使用 Adob​​e Acrobat Pro 或在线 pdf 到 word 转换器:

    http://www.pdfonline.com/pdf2word/index.asp
    

    第二步:这里将整个文档复制粘贴到输入窗口中,也可以下载轻量级html工具:

    http://www.surf7.net/services/value-added-services/free-web-tools/email-extractor-lite/
    

    选择“url”作为“要提取的地址类型”,选择您的分隔符,点击提取,就是这样。

    希望它能奏效。

    【讨论】:

    • 我已经尝试过 Acrobat pro,但在某些情况下根本无法这样做。但是如何根据 x 和 y 来捕获 PDF 中包含超链接的坐标。
    【解决方案3】:

    一种可能性是在 Acrobat 中使用自定义 JavaScript,它会枚举页面上的“单词”,然后读出它们的 Quads。从中您可以获得创建链接的坐标(或与页面上的链接进行比较)以及实际文本(即“单词”。

    如果“仅”设置现有链接的边框,您还可以执行另一个 Acrobat JavaScript 枚举文档的链接,并设置它们的边框颜色属性(您可能还需要设置宽度) .

    (如果您更喜欢“购买”而不是“制作”,请随时私下与我联系;这些内容是我的标准“曲目”的一部分)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多