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

如何在 Java 中提取 PDF 資料

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

2. IronPDF Java PDF 庫

IronPDF是一個軟體庫,它使開發人員能夠在 Java 應用程式中使用IronPDF 適用於 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 適用於 Java 就非常簡單。 本指南將使用 JetBrains 的 IntelliJ IDEA 來示範安裝和執行範例程式碼。

具體做法如下:

-開啟 IntelliJ IDEA:在您的系統上啟動 JetBrains IntelliJ IDEA。 -建立Maven專案:在 IntelliJ IDEA 中,建立一個新的Maven專案。 這將為安裝IronPDF 適用於 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> 
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version> 
</dependency>
XML

將相依性放入 pom.xml 檔案後,檔案右上角會出現一個小圖示。

如何在Java中從PDF中提取數據,圖4:點擊浮動圖示自動安裝Maven依賴項 點選懸浮圖示即可自動安裝Maven依賴項。

點選此按鈕安裝IronPDF 適用於 Java 的Maven相依性。 根據您的網路連線速度,這應該只需要幾分鐘。

5. 擷取數據

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

5.1 從PDF文件中擷取數據

使用IronPDF 適用於 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);
    }
}
// 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 適用於 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 適用於 Java 從 PDF 文件中提取數據,特別是表格數據。

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

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

常見問題解答

如何在Java中從PDF中提取文字?

您可以使用IronPDF 適用於 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庫,以及已安裝並整合到 IDE 中的 Maven。

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

要使用IronPDF 適用於 Java 從 PDF 中提取表格數據,請使用PdfDocument類別載入 PDF 文檔,並使用extractAllText方法檢索表格資料。

使用IronPDF 適用於 Java 是否需要商業許可?

是的, IronPDF for Java 需要商業許可證,但提供免費試用版供評估使用。

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

您可以在IronPDF網站上找到有關使用IronPDF 適用於 Java 的教學課程和範例,尤其是在範例和教學課程部分。

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 來說,工作令人滿意因為它被重視且有實際影響。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我