如何在 Java 中讀取 PDF 文件
本文將探討如何建立PDF閱讀器,以程式化方式在您的軟體應用中開啟PDF檔案。 要有效地執行此任務,IronPDF for Java是一個幫助在Java程式中使用檔名來開啟和閱讀PDF檔案的系統程式庫。
如何在Java中閱讀PDF檔案
- 下載IronPDF Java程式庫
- 使用
fromFile方法載入現有的PDF文件 - 呼叫
extractAllText方法提取嵌入在PDF中的文字 - 使用
extractTextFromPage方法從特定頁面提取文字 - 從由URL生成的PDF中檢索文字
IronPDF
IronPDF - Java程式庫是建立在已成功的.NET Framework之上。 這使得IronPDF成為相較於其他類別程式庫如Apache PDFBox,處理PDF文件的一個多功能工具。 它提供提取和解析內容,載入文字和載入圖片的功能。 它還提供自訂PDF頁面選項,如頁面佈局、邊距、頁眉和頁尾、頁面方向等。
除了這些,IronPDF還支持從其他文件格式轉換、使用密碼保護PDF、數位簽署、合併和拆分PDF文件。
如何在Java中閱讀PDF檔案
先決條件
要使用IronPDF製作Java PDF閱讀器,必須確保在電腦上安裝以下元件:
- JDK - Java開發工具包是建構和運行Java程式所需的。 如果未安裝,請從Oracle網站下載。
- IDE - 整合開發環境是一種幫助撰寫、編輯和偵錯程式的軟體。 下載任何Java的IDE,例如Eclipse、NetBeans、IntelliJ。
- Maven - Maven是一種自動化工具,幫助從中央儲存庫下載程式庫。 請從Apache Maven網站下載。
- IronPDF - 最後,IronPDF需要閱讀Java中的PDF文件。 這需要作為依賴項新增到您的Java Maven專案中。 在
pom.xml檔案中包含IronPDF元件與slf4j依賴,如下例所示:
<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>
新增必要的導入
首先,在Java原始檔案的頂部新增以下程式碼,以參考所有需要的IronPDF方法:
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library
然後,使用有效的許可金鑰配置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
注意:您可以獲得一個免費試用授權金鑰來建立、閱讀和列印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);
在上面的程式碼中,fromFile開啟了一個PDF文件。 方法Paths.get獲取文件的目錄,並準備從文件中提取內容。然後,[extractAllText](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText()讀取文件中的所有文字。
輸出如下:
閱讀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);
閱讀PDF文字輸出
在PageSelection類中可用的其他方法包括從多個頁面提取文字:[firstPage](/java/object-reference/api/com/ironsoftware/ironpdf/edit/PageSelection.html#firstPage())、[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);
從新文件中閱讀
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);
}
}
摘要
本文解釋了如何使用IronPDF在Java中開啟和閱讀PDF文件。
IronPDF幫助輕鬆從HTML或URL建立PDF,並從其他文件格式轉換。 它還幫助快速輕鬆地完成PDF相關任務。
試用IronPDF30天的免費試用,看看在生產環境中它能如何出色工作。 探索商業授權選擇,IronPDF的價格僅從$999開始。
常見問題
如何使用Java建立PDF閱讀器?
您可以使用IronPDF在Java中建立PDF閱讀器,通過使用`fromFile`方法載入PDF文件,然後使用例如`extractAllText`的方法來解析和操作內容。
在Java中使用IronPDF需要安裝哪些前置條件?
要在Java中使用IronPDF,您需要安裝Java Development Kit (JDK),設置整合的開發環境(IDE)如Eclipse或IntelliJ,配置Maven來管理依賴性,並在您的專案中包含IronPDF程式庫。
如何在Java中從PDF文件中提取文字?
要在Java中使用IronPDF從PDF文件中提取文字,您可以使用`extractAllText`方法來檢索整個文件的文字,或者使用`extractTextFromPage`從特定頁面提取文字。
我可以在Java中從URL生成PDF嗎?
是的,使用IronPDF,您可以使用`renderUrlAsPdf`方法從URL生成PDF,該方法將網頁內容轉換成PDF格式。
IronPDF是否支持在Java中為PDF新增密碼保護?
是的,IronPDF支持為PDF新增密碼保護,還有其他功能例如數位簽名、合併或拆分文件。
IronPDF可以在Java中將哪些文件格式轉換為PDF?
IronPDF可以將包括HTML和其他文件格式在內的多種格式轉換為PDF,提供了靈活的PDF生成和操作選項。
IronPDF在Java中是否有試用版本?
是的,IronPDF提供30天免費試用,讓您可以在購買授權之前測試其功能並評估其在Java應用中的性能。
如何在Java程式庫中使用從PDF文件的特定頁面提取文字?
使用IronPDF,您可以使用`extractTextFromPage`方法從PDF的特定頁面提取文字,該方法需要指定頁碼或範圍。





