0

我有一些包含 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);
4

3 回答 3

0

如果您的 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”作为“要提取的地址类型”,选择你的分隔符,点击提取,就是这样。

希望它工作欢呼。

于 2014-04-24T12:25:33.200 回答
0

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

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

之后,代码会创建新的 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");
于 2014-04-24T15:21:56.067 回答
0

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

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

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

于 2014-04-26T22:29:43.703 回答