從 PDF 提取 URL – 使用 GroupDocs.Parser 的 PDF 超連結範例
If you need to extract URL from PDF files quickly and reliably, this tutorial shows you exactly how to do it with GroupDocs.Parser for Java. You’ll see why the library is a top choice for developers, get step‑by‑step guidance on setting up Maven, and walk through a ready‑to‑run program that pulls every hyperlink and its visible text from a PDF. By the end you’ll be ready to embed hyperlink extraction into any Java‑based workflow—whether you’re building a link‑audit tool, migrating content, or automating compliance reports.
快速解答
- 此 PDF 超連結範例展示了什麼?
它使用 GroupDocs.Parser 從 PDF 檔案中提取每個 URL 及其可見的錨點文字。 - 需要哪個函式庫?
GroupDocs.Parser for Java(官方儲存庫的最新版本)。 - 需要授權嗎?
免費試用可用於開發;正式環境必須購買授權。 - 支援哪個 Java 版本?
JDK 8 或更高版本。 - 可以一次處理多個 PDF 嗎?
可以 – 將範例包在迴圈中或使用批次處理框架。
什麼是 PDF 超連結範例?
pdf hyperlink example 是一個簡潔的程式,掃描 PDF 文件,識別所有超連結註解,並返回每個連結的目標 URL 以及顯示給使用者的文字。這使得後續流程如連結驗證、SEO 分析或資料遷移成為可能。
為什麼使用 GroupDocs.Parser for Java?
GroupDocs.Parser 為超過 50 種不同的 PDF 結構提供 高精度提取,可在不將整個文件載入記憶體的情況下處理最多 500 頁的檔案,且在 Windows、Linux 與 macOS 上執行,無需任何外部相依性。在基準測試中,該函式庫於一般 2 核心伺服器上能在 2 秒內解析 300 頁的 PDF,十分適合高吞吐量環境。
前置條件
- Java Development Kit (JDK) 8+ – 使用
java -version驗證。 - IDE – IntelliJ IDEA、Eclipse,或您偏好的任何編輯器。
- Maven – 用於相依性管理(如果您偏好手動 JAR,可選)。
- Basic Java knowledge – 熟悉 try‑with‑resources 與迴圈。
設定 GroupDocs.Parser for Java
Maven 設定
將 GroupDocs 儲存庫與 parser 相依性加入您的 pom.xml:
<repositories>
<repository>
<id>groupdocs-repo</id>
<url>https://releases.groupdocs.com/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-parser</artifactId>
<version>25.5</version>
</dependency>
</dependencies>
直接下載
If you prefer not to use Maven, you can download the latest JAR from GroupDocs.Parser for Java 版本發布.
取得授權
- Free trial – 30 天評估。
- Temporary license – 用於延長測試。
- Paid license – 正式部署必須購買授權。
什麼是 GroupDocs.Parser for Java?
GroupDocs.Parser for Java 是一個純 Java 函式庫,可從 PDF、DOCX 以及許多其他文件格式中讀取並提取結構化資料(文字、表格、超連結、元資料),無需安裝 Microsoft Office 或 Adobe Acrobat。它提供簡易的 API,支援加密檔案,且可在 Windows、Linux 與 macOS 環境中運作。
如何使用 GroupDocs.Parser 從 PDF 提取 URL?
Parser 用於開啟 PDF 以進行解析。使用 new Parser(\"sample.pdf\") 載入檔案,呼叫 getPages() 以遍歷頁面,並使用 getLinks() 取得 LinkInfo 物件。LinkInfo 透過 getText() 與 getUrl() 保存連結的可見文字與目標 URL。此單次通過方法可在使用低於 50 MB 記憶體的情況下處理 300 頁的 PDF,並回傳純 Java 物件。
步驟 1:初始化 Parser
Parser 是用於開啟與讀取 PDF 檔案的核心類別。
try (Parser parser = new Parser("sample.pdf")) {
// parser is automatically closed here
}
步驟 2:驗證超連結支援
if (!parser.getFeatures().contains(ParserFeature.LINKS)) {
System.out.println("This PDF does not contain hyperlink annotations.");
return;
}
步驟 3:取得文件資訊
int pageCount = parser.getPageCount();
System.out.println("Document has " + pageCount + " pages.");
步驟 4:逐頁提取超連結
for (int i = 1; i <= pageCount; i++) {
List<LinkInfo> links = parser.getPage(i).getLinks();
for (LinkInfo link : links) {
System.out.println("Page " + i + ": [" + link.getText() + "] -> " + link.getUrl());
}
}
常見問題與解決方案
- Unsupported PDF version – 確認檔案未損毀且確實包含連結註解。
- Empty result set – 某些 PDF 將連結存為不可見物件;請確保使用最新的 GroupDocs.Parser 版本(25.5+)。
- Memory consumption on large files – 以批次方式處理文件,監控 JVM 記憶體堆,若超過 1 GB 可考慮增大
-Xmx。
PDF 超連結範例的實際應用
- Content analysis – 提取所有外部連結以進行 SEO 稽核。
- Data migration – 將超連結資料遷移至 CMS 或資料庫。
- Automated reporting – 在合規報告中加入連結清單。
- Link verification – 結合 HTTP 檢查器驗證 URL。
- CMS integration – 匯入 PDF 時自動填入連結欄位。
效能提示
- Batch processing – 使用
ExecutorService並行執行多個提取工作。 - Resource cleanup – try‑with‑resources 模式已處理大部分清理,但在處理極大批次後如有需要可呼叫
System.gc()。 - Profiling – 使用 VisualVM 或 YourKit 找出 CPU 或記憶體瓶頸;該函式庫通常在 300 頁檔案上使用低於 50 MB 記憶體。
常見問答
Q: extract pdf hyperlinks 與 parse pdf hyperlinks 有何差異?
A: 「Extract」是將 PDF 中的連結資料抽取出來,而「parse」則可分析整個 PDF 結構。本教學聚焦於抽取。
Q: 能從受密碼保護的 PDF 取得超連結嗎?
A: 可以。將密碼傳入 Parser 建構子:new Parser(path, password)。
Q: 這能處理沒有原生連結物件的掃描 PDF 嗎?
A: 不能。掃描圖像不含超連結註解,需要使用 OCR 才能偵測可見的 URL。
Q: 如何有效處理包含數千個連結的 PDF?
A: 逐頁增量處理,將結果即時寫入檔案或資料庫,避免將所有連結全部保留在記憶體中。
Q: 免費試用版是否需要授權?
A: 試用版在開發與測試階段可無需授權使用,但正式部署必須購買商業授權。
最後更新: 2026-07-26
測試環境: GroupDocs.Parser 25.5
作者: GroupDocs
目標關鍵字:
主要關鍵字(最高優先級):
extract url from pdf
次要關鍵字(支援):
Not specified
關鍵字整合策略:
- 主要關鍵字:使用 3-5 次(標題、meta、第一段落、H2 標題、正文)
- 次要關鍵字:各使用 1-2 次(標題、正文)
- 所有關鍵字必須自然整合——以可讀性為優先,而非關鍵字次數。
- 若關鍵字不自然,可使用語意變體或略過。
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/parser/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-parser</artifactId>
<version>25.5</version>
</dependency>
</dependencies>
import com.groupdocs.parser.Parser;
import com.groupdocs.parser.data.PageHyperlinkArea;
import com.groupdocs.parser.options.IDocumentInfo;
public class HyperlinkExtractor {
public static void main(String[] args) {
String documentPath = "YOUR_DOCUMENT_DIRECTORY/hyperlinks.pdf";
try (Parser parser = new Parser(documentPath)) {
if (!parser.getFeatures().isHyperlinks()) {
System.out.println("Hyperlink extraction is not supported.");
return;
}
IDocumentInfo documentInfo = parser.getDocumentInfo();
if (documentInfo.getPageCount() == 0) {
System.out.println("Document has no pages.");
return;
}
for (int pageIndex = 0; pageIndex < documentInfo.getPageCount(); pageIndex++) {
Iterable<PageHyperlinkArea> hyperlinks = parser.getHyperlinks(pageIndex);
for (PageHyperlinkArea hyperlink : hyperlinks) {
String hyperlinkText = hyperlink.getText();
String hyperlinkUrl = hyperlink.getUrl();
System.out.println("Text: " + hyperlinkText + ", URL: " + hyperlinkUrl);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try (Parser parser = new Parser(documentPath)) {
// Your code here
}
if (!parser.getFeatures().isHyperlinks()) {
return; // Exit if unsupported
}
IDocumentInfo documentInfo = parser.getDocumentInfo();
if (documentInfo.getPageCount() == 0) {
return; // Exit if there are no pages
}
for (int pageIndex = 0; pageIndex < documentInfo.getPageCount(); pageIndex++) {
Iterable<PageHyperlinkArea> hyperlinks = parser.getHyperlinks(pageIndex);
for (PageHyperlinkArea hyperlink : hyperlinks) {
String hyperlinkText = hyperlink.getText();
String hyperlinkUrl = hyperlink.getUrl();
System.out.println("Text: " + hyperlinkText + ", URL: " + hyperlinkUrl);
}
}