跳過到頁腳內容
使用 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 檔案中,如下例所示:

<dependencies>

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
</dependencies>

<dependencies>

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>your-version-here</version>
    </dependency>

    <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 和[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的商業許可選項,價格僅從 $999 起。

常見問題解答

如何使用Java建立PDF閱讀器?

您可以使用IronPDF在 Java 中建立一個 PDF 閱讀器,方法是利用 `fromFile` 方法來載入 PDF 文檔,然後使用 `extractAllText` 等方法來解析和操作內容。

在 Java 中使用IronPDF需要安裝哪些先決條件?

要在 Java 中使用IronPDF ,您需要安裝 Java 開發工具包 (JDK),設定整合開發環境 (IDE),例如 Eclipse 或 IntelliJ,配置 Maven 進行依賴管理,並將IronPDF庫包含在您的專案中。

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

要使用IronPDF從 Java 中的 PDF 文件中提取文本,可以使用 `extractAllText` 方法檢索整個文檔的文本,或者使用 `extractTextFromPage` 方法從特定頁面提取文本。

我可以用Java根據URL產生PDF嗎?

是的,使用IronPDF,您可以透過 `renderUrlAsPdf` 方法從 URL 產生 PDF,該方法可以將 Web 內容轉換為 PDF 格式。

IronPDF是否支援在 Java 中為 PDF 新增密碼保護?

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

IronPDF可以用 Java 將哪些文件格式轉換為 PDF?

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

IronPDF有沒有Java版試用版?

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

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

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

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

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

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

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

鋼鐵支援團隊

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