2014-04-24 70 views
0

我有一些包含URL和mailto形式的超鏈接的PDF。現在是否有任何方式或工具(可能是第三方)從PDF中提取超鏈接元信息,如座標,鏈接類型和目標地址。任何幫助,高度讚賞。來自PDF的超鏈接檢測

我已經嘗試使用iText和PDFBox,但沒有大的成功,甚至有些第三方軟件也不能提供我想要的輸出。

我曾嘗試利用iText

 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); 
+1

@ Bobrovsky答案中的示例使用Doxotic搜索鏈接註釋,並使用iText搜索鏈接註釋或PDFBox將被類似地設計您確定文檔中的鏈接確實是鏈接註釋嗎?例如。 Adobe Reader有一個選項,它使內容中的地址可點擊,就好像它們是鏈接註釋,而不是。也許這樣的功能讓你相信有鏈接註釋,而實際上並沒有。 (順便說一句,你可能想提供你嘗試過的代碼;也許這是錯誤的)。 – mkl

+0

非常感謝你已經完成了它。其實我的代碼工作正常,它是Adobe創建懸停鏈接的財產。您能否爲我提供Adobe創建此類媒體資源的規範以便我可以檢查它 –

+1

Adob​​e Reader只是在頁面內容中搜索它認爲是URL的內容並使它們互動。您可以在首選項中打開或關閉此行爲。我不知道哪些* specs *提供。 – mkl

回答

0

您可以使用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"); 
0

,如果你的PDF文件被複制保護,你需要開始與步驟1中,如果他們可以自由地複製,您可以用第2步開始在Java中下面的代碼

第1步:將您的PDF文檔到Word的.doc:使用Adobe Acrobat Pro或在線PDF到Word轉換器:

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

第2步:在這裏複製粘貼整個文件到輸入窗口,您也可以下載HTML輕量級工具:

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

選擇「網址」爲「地址的方法來提取」,選擇您的分隔符,打提取物,僅此而已。

希望它的作品歡呼聲。

+0

我曾嘗試過Acrobat Pro,但在某些情況下它只是無法做到這一點。但是,如何在x和y方面捕獲超鏈接吸收在PDF中的座標。 –

0

一種可能是在Acrobat中使用自定義JavaScript,這將枚舉「單詞」網頁上,然後閱讀他們的四邊形。從這裏你可以獲得創建鏈接的座標(或者與網頁上的鏈接進行比較)以及實際文本(即「單詞」)

如果是「僅」設置現有鏈接的邊框,還可以使用另一個Acrobat JavaScript枚舉文檔的鏈接,並設置其邊框顏色屬性(並且您可能還需要設置寬度)

(如果您更喜歡「購買「over」使「隨意與我私密聯繫;這些東西是我標準」劇目「的一部分)