在 JAVA 中使用 IRONPDF 如何在 Java 中读取 PDF 文件 Darrius Serrant 已更新:2025年7月28日 下载 IronPDF Maven 下载 JAR 下载 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 这篇文章将探讨如何创建一个PDF阅读器,以编程方式在您的软件应用程序中打开PDF文件。 要有效地执行此任务,IronPDF for Java 是一款有助于在Java程序中使用文件名打开和读取PDF文件的系统库。 ## 如何用 Java 阅读 PDF 文件 下载 IronPDF for Java 库。 使用 `fromFile` 方法加载现有 PDF 文档 调用 `extractAllText` 方法提取 PDF 中的嵌入文本 使用 `extractTextFromPage` 方法从特定页面提取文本 从由URL呈现的PDF中检索文本 IronPDF IronPDF - Java Library 构建在已成功的.NET Framework之上。 与Apache PDFBox等其他类库相比,这使得IronPDF成为处理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> 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())读取文档中的所有文本。 输出如下: 读取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 读取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 从新文件读取 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天免费试用,了解它在生产环境中如何为您有效工作。 探索IronPDF的商业许可选项,起始价格仅从$799。 常见问题解答 如何在Java中创建PDF阅读器? 您可以使用IronPDF在Java中创建PDF阅读器,利用`fromFile`方法加载PDF文档,然后使用类似`extractAllText`的方法解析和操作内容。 在Java中使用IronPDF安装先决条件的步骤是什么? 要在Java中使用IronPDF,您需要安装Java开发工具包(JDK),设置集成开发环境(IDE),如Eclipse或IntelliJ,配置Maven进行依赖管理,并在项目中包括IronPDF库。 25. 我如何在 Java 中从 PDF 文件提取文本? 要在Java中使用IronPDF从PDF文件中提取文本,您可以使用`extractAllText`方法检索整个文档的文本,或使用`extractTextFromPage`从特定页面提取文本。 我可以在Java中从URL生成PDF吗? 是的,使用IronPDF,您可以通过`renderUrlAsPdf`方法从URL生成PDF,该方法将网页内容转换为PDF格式。 IronPDF是否支持在Java中为PDF添加密码保护? 是的,IronPDF支持为PDF添加密码保护,以及数字签名和合并或拆分文档等其他功能。 IronPDF在Java中可以将哪些文件格式转换为PDF? IronPDF可以将多种文件格式转换为PDF,包括HTML和其他文档格式,提供PDF生成和操作的灵活选项。 Java中有可用的IronPDF试用版吗? 是的,IronPDF提供30天的免费试用,允许您在购买许可证之前测试其功能并评估其在Java应用程序中的性能。 如何使用Java库从PDF文档的特定页面中提取文本? 使用IronPDF,您可以通过`extractTextFromPage`方法从PDF的特定页面中提取文本,该方法需要指定页面编号或范围。 Darrius Serrant 立即与工程团队聊天 全栈软件工程师(WebOps) Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。 相关文章 已更新2026年1月18日 如何在 Java 中将 TIFF 转换为 PDF 本完整指南将引导您在 Java 中使用 IronPDF 无缝地将 TIFF 图像转换为 PDF。 阅读更多 已更新2025年7月28日 如何在 Java 中将 PDF 转换为 PDFA 在本文中,我们将探讨如何在 Java 中使用 IronPDF 将 PDF 文件转换为 PDF/A 格式。 阅读更多 已更新2025年7月28日 如何在 Java 中创建 PDF 文档 本文将提供一个关于在 Java 中处理 PDF 的全面指南,涵盖关键概念、最佳库和示例。 阅读更多 10. 如何在 Java 中拆分 PDF 文件HTML2PDF Java (代码示例教程)