使用 GroupDocs.Viewer for Java 提取 PDF 文字

Extracting text from PDFs is a core requirement for many data‑driven applications. In this tutorial we’ll walk you through how to extract pdf content efficiently with the GroupDocs Viewer Java library. Whether you need to index documents, run analytics, or migrate legacy archives, the steps below give you a complete, production‑ready solution.

提取 PDF 文字與 GroupDocs.Viewer for Java

快速回答

  • 什麼函式庫最適合 pdf 文字提取? GroupDocs.Viewer Java 提供強大的 pdf text extraction api.
  • 可以從多頁 PDF 提取文字嗎? 可以 – viewer 會自動遍歷每一頁與每一行。
  • 生產環境需要授權嗎? 需要商業授權;提供免費試用供評估。
  • 支援哪個 Java 版本? JDK 1.8+(最新 LTS 版亦可)。
  • Maven 是唯一的相依加入方式嗎? 建議使用 Maven,也可以使用 Gradle 或手動加入 JAR。

什麼是 PDF 文字提取,為何使用 GroupDocs Viewer?

The pdf text extraction api reads the textual layer of a PDF without rendering the visual content. This approach is far faster than raster‑based OCR and preserves the original document structure. GroupDocs Viewer Java adds extra value by handling complex layouts, encrypted files, and multi‑page documents out‑of‑the‑box.

先決條件

Before you start, make sure you have:

  • Java Development Kit (JDK) 1.8+ installed.
  • Maven for dependency management (or Gradle if you prefer).
  • Access to a GroupDocs Viewer for Java license (free trial or purchased).
  • Basic Java knowledge – you’ll be writing a few try‑with‑resources blocks.

設定 GroupDocs.Viewer for Java

Add the GroupDocs repository and dependency to your pom.xml:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/viewer/java/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-viewer</artifactId>
      <version>25.2</version>
   </dependency>
</dependencies>

取得授權

  • Free Trial – perfect for exploring the pdf text extraction api.
  • Temporary License – extended testing without a credit card.
  • Full Purchase – required for commercial deployments.

實作指南

Below is a concise, step‑by‑step walkthrough of how to extract PDF text with GroupDocs Viewer Java.

1. 初始化 Viewer 物件

try (Viewer viewer = new Viewer("YOUR_DOCUMENT_DIRECTORY/SAMPLE_PDF")) {
    // Initialization complete, proceed to next steps.
}

The Viewer instance points to the PDF you want to process. Using a try‑with‑resources block guarantees that native resources are released automatically.

2. 設定 ViewInfoOptions 以提取文字

ViewInfoOptions viewInfoOptions = ViewInfoOptions.forHtmlView();
viewInfoOptions.setExtractText(true);

Setting setExtractText(true) tells the pdf text extraction api to include raw text in the view information.

3. 取得文件資訊

PdfViewInfo viewInfo = (PdfViewInfo) viewer.getViewInfo(viewInfoOptions);

PdfViewInfo gives you access to each page, line, and its textual value.

4. 遍歷頁面與行(提取多頁 PDF 文字)

for (Page page : viewInfo.getPages()) {
    for (Line line : page.getLines()) {
        System.out.println(line.getValue());
    }
}

This loop prints every line of text, handling extract multi page pdf scenarios automatically. You can replace System.out.println with code that writes to a file, database, or search index.

故障排除提示

  • Double‑check the file path; a wrong path throws FileNotFoundException.
  • Ensure setExtractText(true) is called; otherwise only visual data is returned.
  • For encrypted PDFs, pass the password via Viewer constructor overload.

實務應用

GroupDocs Viewer’s extract pdf text java capabilities unlock many real‑world use cases:

  1. Data Migration – Move legacy PDF archives into searchable databases.
  2. Content Analysis – Feed extracted text into NLP pipelines for sentiment or keyword extraction.
  3. Document Management Systems (DMS) – Auto‑index documents for fast retrieval.

效能考量

When working with large files or batch jobs:

  • Memory Management – Process pages inside the try block to let the garbage collector reclaim memory promptly.
  • Streaming – For extremely large PDFs, consider processing pages one at a time rather than loading the entire document.
  • Threading – Parallelize extraction across multiple files, but keep a single Viewer instance per thread.

常見問題與解決方案

問題解決方案
OutOfMemoryError on big PDFsIncrease JVM heap (-Xmx2g) and process pages sequentially.
No text returned for scanned PDFsUse OCR add‑on or a dedicated OCR library; GroupDocs Viewer extracts only embedded text.
License error on productionVerify that the license file is correctly placed and the trial period has not expired.

常見問題

Q: 可以在正式伺服器上使用 GroupDocs.Viewer 嗎?
A: 可以,但必須擁有有效的商業授權。免費試用僅限開發與測試。

Q: 文字提取會影響 PDF 的 metadata 嗎?
A: Extraction 只讀取內容;除非您自行修改,metadata 不會改變。

Q: 除了 PDF,GroupDocs Viewer 還支援哪些檔案格式?
A: 它支援 Word、Excel、PowerPoint、圖片等多種格式,是多功能的文件檢視器。

Q: 能否從受密碼保護的 PDF 提取文字?
A: 當然可以 – 在建立 Viewer 實例時傳入密碼即可。

Q: 如何提升成千上萬 PDF 批次處理的效能?
A: 使用執行緒池,為每個檔案建立獨立的 Viewer 實例,並密切監控記憶體使用情況。

資源


Last Updated: 2026-05-06
Tested With: GroupDocs.Viewer Java 25.2
Author: GroupDocs