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

如何在 Java 中讀取 PDF 文件

這篇文章將探討如何創建一個PDF閱讀器,以程式化的方式在您的軟體應用程式中打開PDF文件。 為了有效地完成此任務,IronPDF for Java是幫助在Java程式中使用文件名開啟和閱讀PDF文件的程式庫之一。

IronPDF

IronPDF - Java程式庫建立在已經成功的.NET Framework之上。 這使IronPDF成為相比其他類似Apache PDFBox類別庫更為靈活的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 - 最後,IronPDF是需要在Java中閱讀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文件。代碼簡單且分為兩個步驟來從文件中提取文本:

// 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中特定頁面讀取內容。 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在 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 來說,工作令人滿意因為它被重視且有實際影響。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me