在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
本文將展示如何在 Java 中使用 PDF 庫讀取 PDF 文件,用於示範 Java 項目,名為IronPDF Java Library Overview,以讀取 PDF 文件中的文本和元數據類型的對象,同時創建加密的文檔。
使用 Java 安裝 PDF 庫來閱讀 PDF 檔案。
在專案中匯入依賴項以使用 PDF 文件。
使用[PdfDocument.fromFile
方法文件](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#fromFile(java.nio.file.Path)載入現有的 PDF 檔案。
使用[PDF文字提取方法說明](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#extractAllText())方法提取PDF文件中的文字。
使用[PDF 元數據檢索教程](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#getMetadata())方法創建 Metadata 對象。
為了簡化在 Java 中讀取 PDF 檔的過程,開發者經常依賴第三方程式庫來提供全面且高效的解決方案。 其中一個突出的資料庫是 IronPDF for Java。
IronPDF 設計為對開發者友好,提供一個簡單明了的 API,使 PDF 頁面操作的複雜性變得簡單。 使用 IronPDF,Java 開發人員可以無縫地將 PDF 閱讀功能整合到其專案中,減少開發時間和精力。 此程式庫支援多種PDF功能,使其成為適用於各種使用情境的多功能選擇。
主要功能包括能夠從不同格式創建PDF文件,包括HTML、JavaScript、CSS、XML文檔和各種圖片格式。 此外,IronPDF 提供將頁首和頁尾添加到 PDF、在 PDF 文件中創建表格等功能。
若要設置 IronPDF,請確保您擁有可靠的 Java 編譯器。 本文建議使用 IntelliJ IDEA。
啟動 IntelliJ IDEA 並創建一個新的 Maven 項目。
pom.xml
文件。插入以下 Maven 依賴以整合 IronPDF: :ProductInstall
:ProductInstall
讓我們探討一個簡單的 Java 程式碼範例,來演示如何使用 IronPDF 讀取 PDF 文件的內容。在此範例中,我們將重點放在從 PDF 文檔提取文本的方法上。
// Importing necessary classes from IronPDF and Java libraries
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Class definition
class Test {
public static void main(String[] args) throws IOException {
// Setting the license key for IronPDF (replace "License-Key" with a valid key)
License.setLicenseKey("License-Key");
// Loading a PDF document from the file "html_file_saved.pdf"
PdfDocument pdf = PdfDocument.fromFile(Paths.get("html_file_saved.pdf"));
// Extracting all text content from the PDF document
String text = pdf.extractAllText();
// Printing the extracted text to the console
System.out.println(text);
}
}
// Importing necessary classes from IronPDF and Java libraries
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Class definition
class Test {
public static void main(String[] args) throws IOException {
// Setting the license key for IronPDF (replace "License-Key" with a valid key)
License.setLicenseKey("License-Key");
// Loading a PDF document from the file "html_file_saved.pdf"
PdfDocument pdf = PdfDocument.fromFile(Paths.get("html_file_saved.pdf"));
// Extracting all text content from the PDF document
String text = pdf.extractAllText();
// Printing the extracted text to the console
System.out.println(text);
}
}
此 Java 代碼利用 IronPDF 庫從指定的 PDF 文件中提取文本。它將導入 Java 庫並設置許可證密鑰,這是使用該庫的前提條件。 然後,代碼從文件 "html_file_saved.pdf" 加載 PDF 文件,並將其所有文本內容提取為內部字符串緩衝區。 提取的文本被存储在一个变量中,随后打印到控制台。
主控台輸出
除了文本提取之外,IronPDF 還擴展了對從 PDF 文件中提取元數據的支持。 為了說明這個功能,我們來探討一個 Java 程式碼範例,展示從 PDF 文件檢索中繼資料的過程。
// Importing necessary classes from IronPDF and Java libraries
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.metadata.MetadataManager;
import java.io.IOException;
import java.nio.file.Paths;
// Class definition
class Test {
public static void main(String[] args) throws IOException {
// Setting the license key for IronPDF (replace "License-Key" with a valid key)
License.setLicenseKey("License-Key");
// Loading a PDF document from the file "html_file_saved.pdf"
PdfDocument document = PdfDocument.fromFile(Paths.get("html_file_saved.pdf"));
// Creating a MetadataManager object to access document metadata
MetadataManager metadata = document.getMetadata();
// Extracting the author information from the document metadata
String author = metadata.getAuthor();
// Printing the extracted author information to the console
System.out.println(author);
}
}
// Importing necessary classes from IronPDF and Java libraries
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.metadata.MetadataManager;
import java.io.IOException;
import java.nio.file.Paths;
// Class definition
class Test {
public static void main(String[] args) throws IOException {
// Setting the license key for IronPDF (replace "License-Key" with a valid key)
License.setLicenseKey("License-Key");
// Loading a PDF document from the file "html_file_saved.pdf"
PdfDocument document = PdfDocument.fromFile(Paths.get("html_file_saved.pdf"));
// Creating a MetadataManager object to access document metadata
MetadataManager metadata = document.getMetadata();
// Extracting the author information from the document metadata
String author = metadata.getAuthor();
// Printing the extracted author information to the console
System.out.println(author);
}
}
此 Java 代碼使用 IronPDF 庫從 PDF 文件中提取元數據,特別是作者信息。 它首先從檔案 "html_file_saved.pdf" 加載 PDF 文件。該代碼使用MetadataManager 類文檔檢索文件的元數據,特別是獲取作者信息。 提取的作者詳細資訊存儲在變數中,並列印到控制台。
主控台輸出
總之,在 Java 程式中閱讀現有的 PDF 文件是一項寶貴的技能,為開發者帶來無限的可能性。 無論是提取文字、圖片或其他數據,能夠以程序化方式操作 PDF 是許多應用程式中至關重要的方面。 IronPDF for Java 是一個強大且高效的解決方案,適用於希望將 PDF 閱讀功能整合到其 Java 專案中的開發人員。
透過遵循安裝步驟並探索提供的程式碼範例,開發人員可以快速利用IronPDF的強大功能來創建新文件,並輕鬆處理與PDF相關的任務。 除此之外,還可以進一步探索其在創建加密文件方面的功能。
IronPDF 產品入口 為其開發人員提供廣泛的支援。 要了解有關 IronPDF for Java 如何運作的更多資訊,請造訪這些全面的文檔頁面。 此外,IronPDF 提供了一個免費試用許可證優惠頁面,這是一個探索 IronPDF 及其功能的絕佳機會。