使用Java从PDF表单中提取数据

本简短教程描述了使用Java从PDF表单中提取数据的过程。它分享了设置IDE的详细信息,提供了编写程序的步骤列表,以及演示如何使用Java从PDF表单中导出数据的示例代码。详细介绍了如何访问表单中的所有或选定字段并根据需求进行处理。

使用Java从PDF表单字段中提取数据的步骤

  1. 设置IDE以使用Aspose.PDF for Java提取表单数据
  2. 创建一个包含文本框字段和示例数据的PDF文件
  3. 将包含表单和输入字段的PDF文件加载到Document对象中
  4. 从加载的文档中访问表单中的字段集合
  5. 遍历所有字段并获取完整名称和值以在控制台上显示

这些步骤解释了如何使用Java从PDF中提取表单字段。创建一个包含表单字段和示例数据的PDF文件,或加载包含表单数据的现有文件。从文档的Form属性中访问字段集合,遍历所有字段并显示所需的属性。

使用Java从可填写的PDF中提取数据的代码

import com.aspose.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
// Load Aspose PDF license
License license = new License();
license.setLicense("license.lic");
// Generate PDF with input fields
createPdfWithFields();
// Open and process the generated PDF file
Document pdfDocument = new Document("UserForm.pdf");
// Retrieve and display form fields
Field[] formFields = pdfDocument.getForm().getFields();
for (Field formField : formFields) {
System.out.println("Field Name: " + formField.getFullName());
System.out.println("Field Content: " + formField.getValue());
}
// Release resources
pdfDocument.close();
}
private static void createPdfWithFields() {
// Instantiate new PDF document
Document pdfFile = new Document();
for (int pageIndex = 1; pageIndex <= 3; pageIndex++) {
Page newPage = pdfFile.getPages().add();
for (int fieldIndex = 1; fieldIndex <= 4; fieldIndex++) {
// Define a text input field
TextBoxField inputField = new TextBoxField(newPage,
new Rectangle(120, fieldIndex * 90, 320, (fieldIndex + 1) * 90));
inputField.setPartialName("inputField_" + pageIndex + "_" + fieldIndex);
inputField.setValue("Data Entry " + pageIndex + "-" + fieldIndex);
// Attach field to the document form
pdfFile.getForm().add(inputField, pageIndex);
}
}
// Save document to disk
pdfFile.save("UserForm.pdf");
// Free resources
pdfFile.close();
}
}

此代码演示了如何使用Java从PDF表单中提取数据。您可以访问表单中的各种属性,例如字段的备用名称、映射名称、内容、部分名称、活动状态、选中状态名称、页面索引等。要仅访问选定字段,请使用字段索引,例如formFields[1].getValue()来访问第一个字段的值。

在本文中,我们处理了PDF文件中的表单。要从PDF文件中提取字体,请参阅使用Java从PDF中提取字体一文。

 简体中文