跳至頁尾內容
使用 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 程式庫,以及已安裝並整合到 IDE 中的 Maven。

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

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

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

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

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

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

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

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

如何使用 Java 解決從 PDF 提取資料時遇到的問題?

請確保滿足所有先決條件,例如已安裝最新版本的 Java、相容的 IDE 和 IronPDF 程式庫。檢查pom.xml檔案中的 Maven 整合和庫依賴項是否正確。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。