如何在 Java 中读取 PDF 文件
这篇文章将探讨如何创建一个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依赖项,如下面的示例所示:
<!-- 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>添加必要的导入
首先,在Java源文件顶部添加以下代码,以引用来自IronPDF的所有必需方法:
import com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF libraryimport com.ironsoftware.ironpdf.*;
// Necessary imports from IronPDF library接下来,配置IronPDF并使用有效的许可证密钥以使用其方法。 在主方法中调用setLicenseKey方法。
License.setLicenseKey("Your license key");
// Set your IronPDF license key - required for full versionLicense.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](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#fromFile(java.nio.file.Path))打开一个PDF文档。 Paths.get方法获取文件的目录,准备从文件中提取内容。然后,[extractAllText](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#extractAllText())读取文档中的所有文本。
输出如下:
读取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);
读取PDF文本输出
在PageSelection类中可用于从各种页面提取文本的其他方法包括:[firstPage](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#lastPage(),[lastPage](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#firstPage(),[pageRange](/java/object-reference/api/com/Iron Software/ironpdf/edit/PageSelection.html#pageRange(int,int),和[allPages](/java/object-reference/api/com/Iron Software/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的商业授权选项,起价仅为$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的特定页面中提取文本,该方法需要指定页面编号或范围。










