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

如何在 Java 中讀取 PDF 文件

本文將探討如何建立 PDF 閱讀器,以便以程式設計方式在您的軟體應用程式中開啟 PDF 檔案。 為了有效地執行此任務,IronPDF for Java 就是這樣一個系統庫,它可以幫助 Java 程式使用檔案名稱開啟和讀取 PDF 檔案。

IronPDF。

IronPDF - Java 函式庫是基於已經非常成功的 .NET Framework 所建構的。 與其他類別庫(例如 Apache PDFBox)相比,這使得 IronPDF 成為處理 PDF 文件的多功能工具。 它提供了提取和解析內容、載入文字和載入圖像的功能。 它還提供了自訂 PDF 頁面的選項,例如頁面佈局、邊距、頁首和頁尾頁面方向等等。

除此之外,IronPDF 還支援從其他文件格式轉換、使用密碼保護 PDF、數位簽章、合併和拆分 PDF 文件。

如何在Java中讀取PDF文件

先決條件

若要使用 IronPDF 建立 Java PDF 閱讀器,必須確保電腦上安裝了以下元件:

  1. JDK - Java 開發工具包是建置和運行 Java 程式所必需的。 如果尚未安裝,請從Oracle網站下載。
  2. IDE - 整合開發環境是一種幫助編寫、編輯和調試程式的軟體。 下載任一款Java整合開發環境(IDE),例如Eclipse、NetBeans、IntelliJ等。
  3. Maven - Maven 是一個自動化工具,可以幫助從中央儲存庫下載庫。 從Apache Maven 網站下載。
  4. IronPDF - 最後,Java 需要 IronPDF 來讀取 PDF 文件。 需要將此作為依賴項新增至您的 Java Maven 專案。 將 IronPDF 元件和 slf4j 依賴項加入pom.xml檔中,如下例所示:
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
<!-- Add Maven dependencies for IronPDF -->
<dependencies>
    <!-- IronPDF Dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <!-- SLF4J Dependency necessary for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>
XML

新增必要的導入

首先,在 Java 原始檔的頂部新增以下程式碼,以引用 IronPDF 中所有必要的方法:

import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
JAVA

接下來,使用有效的許可證密鑰配置 IronPDF 以使用其功能。 在主方法中呼叫setLicenseKey方法。

License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full version
JAVA

注意:您可以獲得免費試用許可證金鑰來建立、閱讀和列印 PDF 文件。

用 Java 讀取現有 PDF 文件

閱讀 PDF 文件,必須先存在 PDF 文件,或者可以建立一個 PDF 文件。 本文將使用一個已建立的 PDF 檔案。程式碼很簡單,只需兩個步驟即可從文件中提取文字:

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract all text from the PDF
String text = pdf.extractAllText();
// Print the extracted text
System.out.println(text);
JAVA

在上面的程式碼中, fromFile 開啟一個 PDF 文件。 Paths.get方法取得檔案所在的目錄,並準備從中提取檔案內容。然後, [extractAllText](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText()方法讀取文件中的所有文字。

輸出結果如下:

如何在 Java 中讀取 PDF 文件,圖 1:讀取 PDF 文字輸出 讀取 PDF 文字輸出

讀取特定頁面上的文本

IronPDF也可以讀取PDF中特定頁面的內容。 extractTextFromPage方法使用PageSelection物件來接受要從中讀取文字的頁面範圍。

在以下範例中,文字是從 PDF 文件的第二頁提取的。 PageSelection.singlePage接受要擷取的頁面的索引(索引從 0 開始)。

// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
// Load the PDF document from file
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
// Extract text from the second page (page index based, starts at 0, so 1 means second page)
String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
// Print the extracted text from the specified page
System.out.println(text);
JAVA

如何在 Java 中讀取 PDF 文件,圖 2:讀取 PDF 文字輸出 讀取 PDF 文字輸出

PageSelection類別中還有其他方法可用於從各種頁面中提取文本,包括: [firstPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#lastPage() )、 [lastPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#firstPage() )、 [pageRange](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#pageRange(int,int) )和[allPages](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#allPages() )。

從新生成的 PDF 檔案讀取文本

也可以從 HTML 檔案或 URL 產生的新 PDF 檔案中進行文字搜尋。 以下範例程式碼根據 URL 產生 PDF 文件,並提取網站中的所有文字。

// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
// Generate PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
// Extract all text from the generated PDF
String text = pdf.extractAllText();
// Print the extracted text from the URL
System.out.println("Text extracted from the website: " + text);
JAVA

如何在 Java 中讀取 PDF 文件,圖 3:從新文件讀取 從新檔案讀取

IronPDF 也可以用於從 PDF 文件中提取影像

完整的程式碼如下:

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

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

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

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

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the IronPDF license key for commercial use
        License.setLicenseKey("YOUR LICENSE KEY HERE");

        // Read text from a specific page in an existing PDF
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/sample.pdf"));
        String text = pdf.extractTextFromPage(PageSelection.singlePage(1));
        System.out.println(text);

        // Read all text from a PDF generated from a URL
        pdf = PdfDocument.renderUrlAsPdf("https://unsplash.com/");
        text = pdf.extractAllText();
        System.out.println("Text extracted from the website: " + text);
    }
}
JAVA

摘要

本文介紹如何使用 IronPDF 在 Java 中開啟和讀取 PDF 檔案。

IronPDF 可以幫助使用者輕鬆地從 HTML 或 URL 建立 PDF,並轉換不同的文件格式。 它還有助於快速輕鬆地完成 PDF 任務。

免費試用 IronPDF 30 天,看看它在實際生產環境中是否能很好地滿足您的需求。 探索 IronPDF 的商業許可選項,價格僅從$799起。

常見問題解答

如何在 Java 中建立 PDF 閱讀器?

您可以使用 IronPDF for Java 創建一個 PDF 閱讀器,方法是利用 `fromFile` 方法載入 PDF 文件,然後再使用 `extractAllText` 等方法來解析和處理內容。

在 Java 中使用 IronPDF 的安裝前提步驟是什麼?

若要在 Java 中使用 IronPDF,您需要安裝 Java 開發套件 (JDK)、設定整合開發環境 (IDE),例如 Eclipse 或 IntelliJ、配置 Maven 進行相依性管理,並在專案中包含 IronPDF 函式庫。

如何用 Java 從 PDF 檔案中萃取文字?

要使用 IronPDF for Java 從 PDF 檔中提取文字,您可以使用 `extractAllText` 方法擷取整個文件的文字,或使用 `extractTextFromPage` 方法從特定頁面提取文字。

我可以用 Java 從 URL 產生 PDF 嗎?

是的,使用 IronPDF,您可以使用 `renderUrlAsPdf` 方法從 URL 生成 PDF,該方法可將網頁內容轉換為 PDF 格式。

IronPDF 是否支持在 Java 中为 PDF 添加密码保护?

是的,IronPDF 支援為 PDF 加入密碼保護,以及其他功能,例如數位簽章、合併或分割文件。

IronPDF 可將哪些檔案格式轉換為 Java PDF?

IronPDF 可以將各種檔案格式轉換為 PDF,包括 HTML 和其他文件格式,為 PDF 生成和操作提供靈活的選擇。

IronPDF in Java 是否有試用版?

是的,IronPDF 提供 30 天的免費試用,讓您可以在購買授權之前測試其功能,並評估其在 Java 應用程式中的效能。

如何使用 Java 函式庫從 PDF 文件中的特定頁面擷取文字?

使用 IronPdf,您可以通過使用 `extractTextFromPage` 方法從 PDF 中的特定頁面中提取文本,該方法需要指定頁數或範圍。

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

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

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

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