跳過到頁腳內容
使用 IRONPDF FOR JAVA

如何在 Java 中提取 PDF 資料

本教學將向您展示如何使用IronPDF for Java從 PDF 檔案中提取資料。教程將透過程式碼範例講解環境設定、庫導入、讀取輸入檔以及提取所需資料等步驟。

2. IronPDF Java PDF 庫

IronPDF 是一個軟體庫,它使開發人員能夠在 Java 應用程式中使用 IronPDF for Java 生成、編輯和提取 PDF 文件中的資料。 它允許您從 HTML 文件、圖像等建立 PDF ,以及合併多個 PDF分割 PDF 文件和操作現有 PDF。 IronPDF 也提供密碼保護功能,以及在 PDF 中添加數位簽章等功能。

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

3. 先決條件

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

  1. Java 安裝:確保您的系統上已安裝 Java,且其路徑已在環境變數中設定。 如果您尚未安裝 Java,請參考Java 網站上的此下載頁面以取得說明。
  2. Java IDE:安裝 Java IDE,例如 Eclipse 或 IntelliJ。 您可以從此Eclipse 下載頁面下載 Eclipse,從此IntelliJ 下載頁面下載IntelliJ。
  3. IronPDF 庫:下載 IronPDF 庫並將其作為依賴項新增至您的專案。 請造訪IronPDF 設定說明頁面以取得設定說明。
  4. Maven 安裝:在開始 PDF 轉換過程之前,應安裝 Maven 並將其整合到您的 IDE 中。 請參考JetBrains 網站上的 Maven 安裝教學課程,以了解如何安裝和整合 Maven。

4. IronPDF 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 Central 上的 IronPDF 庫頁面下載 JAR 檔案。

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

pom.xml檔案中新增依賴項後,檔案右上角會出現一個小圖示。

圖 4:如何在 Java 中從 PDF 擷取資料?點選浮動圖示自動安裝 Maven 依賴項。 點擊懸浮圖示即可自動安裝 Maven 依賴項。

點擊此按鈕安裝 IronPDF for Java 的 Maven 依賴項。 根據您的網路連線速度,這應該只需要幾分鐘。

5. 擷取數據

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

5.1 從PDF文件中擷取數據

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

如何在 Java 中從 PDF 中提取數據,圖 5:PDF 輸入 PDF Input

// 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);
    }
}
// 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.

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);
    }
}
// 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. 從表格資料中擷取數據

使用 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);
    }
}
// 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

6.結論

總之,本教學示範如何使用 IronPDF for Java 從 PDF 文件中提取數據,特別是表格數據。

更多信息,請參閱 IronPDF 網站上的PDF 文字擷取範例

IronPDF 是一個具有商業許可詳情的庫,起價為$799 。 不過,您可以使用 IronPDF 試用許可證進行免費試用,從而在生產環境中對其進行評估。

常見問題解答

如何用 Java 從 PDF 擷取文字?

您可以使用 IronPDF for Java 從 PDF 中提取文字,方法是使用 PdfDocument 類載入文件,並利用 extractAllText 方法擷取文字。

我可以用 Java 從 URL 擷取資料並將其轉換成 PDF 嗎?

是的,IronPDF for Java 允許您在運行時將 URL 轉換為 PDF,並使用 PdfDocument 類從中提取資料。

在 IntelliJ IDEA 中安裝 IronPdf 的步驟是什麼?

若要在 IntelliJ IDEA 中設定 IronPdf,請建立一個新的 Maven 專案,將 IronPDF 函式庫新增至 pom.xml 檔案,並點選出現的浮動圖示來安裝 Maven 的相依性。

在 Java 中使用 IronPDF 有哪些先決條件?

先決條件包括已安裝 Java、Java IDE(如 Eclipse 或 IntelliJ)、IronPDF 函式庫,以及已安裝 Maven 並與您的 IDE 整合。

如何使用 Java 從 PDF 擷取表格資料?

要使用 IronPDF for Java 從 PDF 中提取表格資料,請使用 PdfDocument 類載入 PDF 文件,並使用 extractAllText 方法擷取表格資料。

使用 IronPDF for Java 是否需要商業授權?

是的,IronPDF for Java 需要商業授權,但也提供免費試用版以供評估。

在哪裡可以找到在 Java 中使用 IronPDF 的教學?

IronPDF for Java 的使用教程和示例可在 IronPDF 網站上找到,尤其是在示例和教程部分。

IronPDF 為 Java 開發人員提供哪些功能?

IronPDF for Java 提供創建、編輯、合併、分割和處理 PDF 檔案的功能,以及使用密碼保護 PDF 和新增數位簽章的功能。

如何排除使用 Java 從 PDF 擷取資料的問題?

確保符合所有先決條件,例如擁有最新的 Java 版本、相容的 IDE 以及 IronPDF 函式庫。檢查 pom.xml 檔案中的 Maven 整合和函式庫依賴是否正確。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。