IRONSOFTWAREHOME
USING IRONPDF FOR JAVA

如何在 Java 中提取 PDF 資料

Curtis Chau
Curtis Chau
Updated: 2026年4月21日

本教程將向您展示如何使用IronPDF for Java從 PDF 文件中提取資料。設置環境、導入程式庫、讀取輸入文件以及提取所需資料均通過程式碼範例進行了解釋。

2. IronPDF Java PDF 專案庫

IronPDF 是一個軟體程式庫,提供開發人員在其 Java 應用程式中使用 IronPDF for Java從 PDF 文件中提取資料的能力。 它允許您通過 HTML 文件、圖片等建立 PDF,並且可以合併多個 PDF,拆分 PDF 文件及操作現有的 PDF。 IronPDF 還提供了使用密碼保護功能來加密 PDF 及向 PDF 新增數位簽名的功能,以及其他功能。

IronPDF for Java 由 Iron Software 開發和維護。 其最受好評的功能之一是從 PDF 文件中提取文字和資料,以及從 HTML 和 URL 中提取。

3. 先決條件

要使用 IronPDF 從 PDF 文件中提取資料,您必須滿足以下先決條件:

  1. **Java 安裝:**確保 Java 已安裝在您的系統上,並且其路徑已設置在環境變數中。 如果您尚未安裝 Java,請參考 Java 網站上的此下載頁面以獲取說明。
  2. **Java IDE:**安裝像 Eclipse 或 IntelliJ 的 Java IDE。 您可以從此Eclipse 下載頁面下載 Eclipse,從此IntelliJ 下載頁面下載 IntelliJ。
  3. **IronPDF 程式庫:**下載並將 IronPDF 程式庫作為相依性新增到您的專案中。 請存取 IronPDF 設置說明頁以獲取設置指示。
  4. **Maven 安裝:**在開始 PDF 轉換過程之前,應該安裝 Maven 並將其整合到您的 IDE 中。 請參考 JetBrains 上的這個Maven 安裝教程,以便安裝和整合 Maven。

4. 安裝 IronPDF for Java

IronPDF for Java 的安裝既簡單又不複雜,前提是所有的要求均已滿足。 本指南將使用 JetBrains 的 IntelliJ IDEA 來演示如何安裝並運行範例程式碼。

下面是需要做的事情:

  • **打開 IntelliJ IDEA:**在您的系統上啟動 JetBrains IntelliJ IDEA。
  • **建立 Maven 專案:**在 IntelliJ IDEA 中建立一個新的 Maven 專案。 這將為安裝 IronPDF for Java 提供一個合適的環境。

如何在 Java 中從 PDF 中提取資料,圖1:在 IntelliJ 中建立新的 Maven 專案 在 IntelliJ 中建立新的 Maven 專案

  • 會出現一個新窗口。 輸入專案名稱,然後點擊完成。

如何在 Java 中從 PDF 中提取資料,圖2:給 Maven 專案命名並點擊完成 給 Maven 專案命名並點擊完成

  • 一旦您點擊完成,將打開一個包含 pom.xml 的新專案。 這將用於新增 IronPDF Java Maven 相依性。

如何在 Java 中從 PDF 中提取資料,圖3:pom.xml 文件 pom.xml 文件

在 pom.xml 文件中新增以下相依性,或者您可以從 Sonatype 中央上的 IronPDF 程式庫頁面下載 JAR 文件。

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version> <!-- replace with the latest version -->
</dependency>
XML

一旦您將相依性放入 pom.xml 文件中,文件的右上角會出現一個小圖標。

如何在 Java 中從 PDF 中提取資料,圖4:點擊浮動圖標以自動安裝 Maven 相依性 點擊浮動圖標以自動安裝 Maven 相依性

通過點擊此按鈕安裝 IronPDF for Java 的 Maven 相依性。 根據您的網速,這應該只需幾分鐘。

5. 提取資料

IronPDF 是一個 Java 程式庫,用於建立、編輯和從 PDF 文件中提取資料。 它提供了一個簡單的 API 來從 PDF 文件、URL 和表中提取文字。

5.1. 從 PDF 文件中提取資料

using IronPDF for Java,您可以輕鬆地從 PDF 文件中提取文字資料。 下面是從 PDF 文件中提取資料的範例程式碼。

如何在 Java 中從 PDF 中提取資料,圖5:PDF 輸入 PDF 輸入

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF document from the specified file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("business plan.pdf"));
        
        // Extract all text from the PDF document
        String text = pdf.extractAllText();
        
        // Print the extracted text to the console
        System.out.println("Text extracted from the PDF: " + text);
    }
}
Java

源程式碼生成了下面給出的輸出:

> Text extracted from the PDF:
> 
> CRAFT-ARENA
> 
> Muhammad Waleed Butt
> 
> Hassan Khan
> 
> ABOUT US
> 
> Craft-Arena is a partnership based business that will help local crafters of Pakistan to sell their handicrafts at good prices and helps them earn a good living.
Text

5.2. 從 URL 中提取資料

IronPDF for Java 在運行時將 URL 轉換為 PDF 並從中提取文字。 此範例將展示從 URL 中提取文字的源程式碼。

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        // Convert a URL to a PDF and load it into a PdfDocument
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java/");
        
        // Extract all text from the PDF document
        String text = pdf.extractAllText();
        
        // Print the extracted text to the console
        System.out.println("Text extracted from the URLs: " + text);
    }
}
Java

如何在 Java 中從 PDF 中提取資料,圖6:提取的網頁資料 提取的網頁資料

5.3. 從表格資料中提取資料

using IronPDF for Java 從 PDF 中提取表格資料非常簡單; 所有需要的是包含表格的 PDF 並運行下面的程式碼即可。

如何在 Java 中從 PDF 中提取資料,圖7:範例 PDF 表格輸入 範例 PDF 表格輸入

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF document from the specified file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("table.pdf"));
        
        // Extract all text from the PDF document, including table data
        String text = pdf.extractAllText();
        
        // Print the extracted table data to the console
        System.out.print("Text extracted from the Marked tables: " + text);
    }
}
Java
> Test Case Description Expected Result Actual Result Status
> 
> 1 Test login functionality User should be able to log in with valid credentials
> 
> User log in successfully Pass
> 
> 2 Test search functionality Search results should be relevant and accurate
> 
> Search is accurate and provide relevant products Pass
> 
> 3 Test checkout process User should be able to complete a purchase successfully
> 
> User can purchase successfully Pass
Text

6. 結論

總結來說,本教程展示了如何使用 IronPDF for Java 從 PDF 文件中提取資料,尤其是表格資料。

有關更多資訊,請參閱 IronPDF 網站上的從 PDF 提取文字範例。

IronPDF 是一個擁有商業授權詳情的程式庫,起價為 $999 但是,您可以在生產中使用 IronPDF 試用授權免費試用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預訂您的免費 即時演示
Booking Badge

全球數百萬工程師的信任

Iron Software的客戶商標
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊始終保持機密。
全球數百萬工程師的信任
Iron Software的客戶商標
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
C# 用於PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解決方案資源管理器,右鍵點選參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronPdf"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

或者點擊此處下載Windows安裝程式。

  1. 下載並解壓IronPDF到類似~/Libs的位置,位於您的解決方案目錄中
  2. 在Visual Studio解決方案資源管理器,右鍵點選參考。選擇瀏覽,"IronPdf.dll"

授權從$999起