跳至頁尾內容
USING 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 及向 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> 
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</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);
    }
}
// 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. 從表格資料中提取資料

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);
    }
}
// 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 是一個擁有商業授權詳情的程式庫,起價為 $999 但是,您可以在生產中使用 IronPDF 試用授權免費試用

常見問題

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

您可以使用IronPDF for Java通過PdfDocument類載入文件,並利用extractAllText方法來檢索文字。

我可以在Java中從URL提取資料並將其轉換為PDF嗎?

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

在IntelliJ IDEA中設置IronPDF的步驟是什麼?

要在IntelliJ IDEA中設置IronPDF,請建立一個新的Maven項目,將IronPDF程式庫新增到您的pom.xml文件中,並通過點擊出現的浮動圖標來安裝Maven依賴項。

使用IronPDF in Java的先決條件是什麼?

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

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

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

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

是的,IronPDF for Java需要商業授權,但可用於評估目的的免費試用。

在哪裡可以找到使用IronPDF in Java的教程?

使用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來說,他的工作是有意義的,因為它有價值且對社會有真正的影響。

Iron 支援團隊

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