IRONSOFTWAREHOME
在 JAVA 中使用 IRONPDF

如何在 Java 中从 PDF 中提取数据

Curtis Chau
Curtis Chau
Updated: 2026年4月21日

本教程将向您展示如何使用IronPDF 适用于 Java从PDF文件中提取数据。设置环境、导入库、读取输入文件和提取所需数据的步骤均通过代码示例进行解释。

2. IronPDF for Java PDF 库

IronPDF是一个软件库,为开发人员提供了在Java应用程序中生成、编辑和提取PDF文件数据的能力。 它允许您从HTML文档、图片等创建PDF,以及合并多个PDF、拆分PDF文件和操作现有PDF。 IronPDF还提供了通过密码保护功能和向PDF添加数字签名来保护PDF文件的功能,还有其他功能。

IronPDF for Java由Iron Software开发和维护。 其评价最高的功能之一是从PDF文件以及HTML和URL中提取文本和数据。

3. 先决条件

要使用IronPDF从PDF文件中提取数据,您必须满足以下先决条件:

  1. **Java安装:**确保Java已安装在您的系统上,并且其路径设置在环境变量中。 如果您尚未安装Java,请参考Java网站上的下载页面以获取说明。
  2. **Java IDE:**安装像Eclipse或IntelliJ这样的Java IDE。 您可以从这个Eclipse下载页面下载Eclipse,从这个IntelliJ下载页面下载IntelliJ。
  3. **IronPDF库:**下载并将IronPDF库作为项目中的依赖项添加。 访问IronPDF安装说明页面获取安装说明。
  4. **Maven安装:**在开始PDF转换过程之前,应安装Maven并集成到您的IDE中。 请参考这个JetBrains上的Maven安装教程以了解安装和集成Maven的步骤。

4. IronPDF for Java安装

如果符合所有要求,安装IronPDF for Java非常简单。 本指南将使用JetBrains的IntelliJ IDEA演示安装并运行示例代码。

以下是要执行的操作:

  • **打开IntelliJ IDEA:**在系统上启动JetBrains IntelliJ IDEA。
  • **创建一个Maven项目:**在IntelliJ IDEA中创建一个新的Maven项目。 这将为安装IronPDF for Java提供合适的环境。

如何从PDF中提取数据Java,图1:IntelliJ中的新Maven项目 New Maven Project in IntelliJ

  • 新窗口将打开。 输入项目名称并单击 "完成"。

如何从PDF中提取数据Java,图2:命名Maven项目并点击完成 Name the Maven Project and click Finish

  • 点击完成后,会打开一个带有pom.xml的新项目。 这将用于添加IronPDF Java Maven依赖项。

如何从PDF中提取数据Java,图3:pom.xml文件 The pom.xml file

在pom.xml文件中添加以下依赖项,或者您可以从Sonatype Central上的IronPDF库页面下载JAR文件。

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version> <!-- replace with the latest version -->
</dependency>
XML

一旦您将依赖项放置在pom.xml文件中,文件的右上角将出现一个小图标。

如何从PDF中提取数据Java,图4:点击浮动图标自动安装Maven依赖 Click the floating icon to install the Maven dependencies automatically

通过点击此按钮安装IronPDF for Java的Maven依赖项。 根据您的互联网连接速度,这应该只需几分钟。

5. 提取数据

IronPDF是一个用于创建、编辑和从PDF文档中提取数据的Java库。 它提供了一个简单的API来从PDF文件、URL和表格中提取文本。

5.1. 从PDF文档中提取数据

使用IronPDF 适用于 Java,您可以轻松地从PDF文档中提取文本数据。 以下是从PDF文件中提取数据的示例代码。

如何从PDF中提取数据Java,图5:PDF输入 PDF Input

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF document from the specified file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("business plan.pdf"));
        
        // Extract all text from the PDF document
        String text = pdf.extractAllText();
        
        // Print the extracted text to the console
        System.out.println("Text extracted from the PDF: " + text);
    }
}
Java

源代码生成以下输出:

> Text extracted from the PDF:
> 
> CRAFT-ARENA
> 
> Muhammad Waleed Butt
> 
> Hassan Khan
> 
> ABOUT US
> 
> Craft-Arena is a partnership based business that will help local crafters of Pakistan to sell their handicrafts at good prices and helps them earn a good living.
Text

5.2. 从URLs中提取数据

IronPDF for Java在运行时将URL转换为PDF并从中提取文本。 此示例将展示从URLs中提取文本的源代码。

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        // Convert a URL to a PDF and load it into a PdfDocument
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java/");
        
        // Extract all text from the PDF document
        String text = pdf.extractAllText();
        
        // Print the extracted text to the console
        System.out.println("Text extracted from the URLs: " + text);
    }
}
Java

如何从PDF中提取数据Java,图6:提取的网页数据 Extracted Web Page Data

5.3. 从表格数据中提取数据

要从PDF中提取表格数据,使用IronPDF for Java非常简单; 您只需拥有一个包含表格的PDF,并运行以下代码。

如何从PDF中提取数据Java,图7:示例PDF表格输入 Sample PDF Table Input

// Import the necessary IronPDF package for working with PDF documents
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Load the PDF document from the specified file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("table.pdf"));
        
        // Extract all text from the PDF document, including table data
        String text = pdf.extractAllText();
        
        // Print the extracted table data to the console
        System.out.print("Text extracted from the Marked tables: " + text);
    }
}
Java
> Test Case Description Expected Result Actual Result Status
> 
> 1 Test login functionality User should be able to log in with valid credentials
> 
> User log in successfully Pass
> 
> 2 Test search functionality Search results should be relevant and accurate
> 
> Search is accurate and provide relevant products Pass
> 
> 3 Test checkout process User should be able to complete a purchase successfully
> 
> User can purchase successfully Pass
Text

6.结论

总之,本教程演示了如何使用IronPDF for Java从PDF文件中提取数据,特别是表格数据。

有关更多信息,请参考IronPDF网站上的从PDF提取文本示例。

IronPDF是一个包含商业许可证详细信息的库,起价为$999。 不过,您可以使用IronPDF试用许可证进行免费试用在生产中评估它。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

相关文章

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起