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

如何在 Java 中讀取 PDF 文件

本文將探討如何創建PDF閱讀器,以程式化方式在您的軟體中打開PDF文件。 為了有效地執行此任務,IronPDF for Java 是一個幫助在Java程式中使用檔案名稱打開和閱讀PDF文件的系統庫。

class="hsg-featured-snippet">

如何在Java中閱讀PDF文件

  1. 下載IronPDF Java庫
  2. 使用fromFile方法載入已存在的PDF文件
  3. 調用extractAllText方法以提取PDF中的內嵌文本
  4. 使用extractTextFromPage方法從特定頁面提取文本
  5. 從URL渲染的PDF中檢索文本

IronPDF

IronPDF - Java 庫 是基於已經成功的 .NET Framework 構建的。 這使得IronPDF成為與其他類庫如Apache PDFBox相比,處理PDF文件的多功能工具。 它提供了提取和解析內容、載入文本和載入圖片的功能。 It also provides options to customize the PDF pages such as page layout, margins, header and footer, page orientation, and much more.

除此之外,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 - 最後,IronPDF是用於在Java中閱讀PDF文件所需的。 這需要作為依賴項添加到您的Java Maven項目中。 在pom.xml 文件中包括IronPDF構件和slf4j依賴項,如下例所示:
<!-- 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文件。該代碼簡單且有兩步驟以從文檔中提取文本:

// 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文本輸出

Other methods available in the PageSelection class which can be used to extract text from various pages include: [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, and [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任務。

試用IronPDF30天免費試用,看看它在生產中的效果如何。 探索

a href="/java/licensing/">IronPDF的商業許可選項,起價只需$799。

常見問題解答

我如何在 Java 中創建一個 PDF 閱讀器?

您可以使用 IronPDF 在 Java 中創建一個 PDF 閱讀器,通過利用 `fromFile` 方法加載 PDF 文件,然後使用像 `extractAllText` 這樣的方法解析和操作內容。

安裝 IronPDF 在 Java 中使用的先決條件有哪些步驟?

要在 Java 中使用 IronPDF,您需要安裝 Java Development Kit (JDK),設置像 Eclipse 或 IntelliJ 這樣的集成開發環境 (IDE),配置 Maven 進行依賴管理,並將 IronPDF 函式庫包含在您的項目中。

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

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

我可以在 Java 中從 URL 生成 PDF 嗎?

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

IronPDF 是否支持在 Java 中為 PDF 添加密碼保護?

是的,IronPDF 支持為 PDF 添加密碼保護,還有其他功能,如數字簽名和合併或拆分文件。

IronPDF 可以將哪些格式轉換為 PDF 並在 Java 中使用?

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