Java 创建、填充、读取PDF表单域

发布时间:2025-05-24 16:22:45 作者:益华网络 来源:undefined 浏览量(1) 点赞(1)
摘要:概述 表单域,可以按用途分为多种不同的类型,常见的有文本框、多行文本框、密码框、隐藏域、复选框、单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据。 下面的示例中,将分享通过Java编程在PDF中创建、填充以及读取PDF表单域

概述

表单域,可以按用途分为多种不同的类型,常见的有文本框、多行文本框、密码框、隐藏域、复选框、单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据。

下面的示例中,将分享通过Java编程在PDF中创建、填充以及读取PDF表单域的方法。 创建表单域包括文本框、复选框、单选按钮、列表框、组合框、签名域、按钮等。 填充表单域时可分为2种情况,一种是在创建表单域时填充,一种是加载已经创建好表单域的文档进行填充;对于已经创建表单域并填写好的文档,也可以设置只读,防止修改、编辑等; 读取表单域时,可获取指定(可通过索引值或表单域名称)表单域的值,或者获取文档中所有表单域的值。

示例要点概括:

1.创建表单域

2.填充表单域

3.设置表单域只读

4.读取表单域值

工具

Free Spire.PDF for Java(免费版)

Jar文件获取及导入:

方法1: 通过官网 下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入到java程序。

方法2: 通过 maven 仓库安装导入

Java代码示例(供参考)

【示例1】 创建并填充PDF表单域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;
public class AddFormFieldsToPdf {
public static void main(String[] args) throws Exception {
//创建PdfDocument对象,并添加页面
PdfDocument doc = new PdfDocument();        
PdfPageBase page = doc.getPages().add();
//初始化位置变量
float baseX = 100;
float baseY = 0;
//创建画刷对象
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));
//创建TrueType字体
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,10),true); 
//添加文本框
String text = "姓名:";//添加文本
page.getCanvas().drawString(text, font, brush1, new Point2D.Float(0, baseY));//在PDF中绘制文字
Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY , 150, 15);//创建Rectangle2D对象
PdfTextBoxField textBox = new PdfTextBoxField(page, "TextBox");//创建文本框对象
textBox.setBounds(tbxBounds);//设置文本框的Bounds
textBox.setText("刘兴");//填充文本框
textBox.setFont(font);//应用文本框的字体
doc.getForm().getFields().add(textBox);//添加文本框到PDF域的集合
baseY +=25;
//添加复选框
page.getCanvas().drawString("所在院系:", font, brush1, new Point2D.Float(0, baseY));
java.awt.geom.Rectangle2D.Float rec1 = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 15, 15);
PdfCheckBoxField checkBoxField = new PdfCheckBoxField(page, "CheckBox1");//创建第一个复选框对象
checkBoxField.setBounds(rec1);
checkBoxField.setChecked(false);//填充复选框
page.getCanvas().drawString("经管系", font, brush2, new Point2D.Float(baseX + 20, baseY));
java.awt.geom.Rectangle2D.Float rec2 = new java.awt.geom.Rectangle2D.Float(baseX + 70, baseY, 15, 15);
PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "CheckBox2");//创建第二个复选框对象
checkBoxField1.setBounds(rec2);
checkBoxField1.setChecked(true);//填充复选框
page.getCanvas().drawString("创新班", font,  brush2, new Point2D.Float(baseX+90, baseY));      
doc.getForm().getFields().add(checkBoxField);//添加复选框到PDF
baseY += 25;
//添加列表框
page.getCanvas().drawString("录取批次:", font, brush1, new Point2D.Float(0, baseY));
java.awt.geom.Rectangle2D.Float rec = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 150, 50);
PdfListBoxField listBoxField = new PdfListBoxField(page, "ListBox");//创建列表框对象
listBoxField.getItems().add(new PdfListFieldItem("第一批次", "item1"));
listBoxField.getItems().add(new PdfListFieldItem("第二批次", "item2"));
listBoxField.getItems().add(new PdfListFieldItem("第三批次", "item3"));
listBoxField.setBounds(rec);
listBoxField.setFont(font);
listBoxField.setSelectedIndex(0);//填充列表框
doc.getForm().getFields().add(listBoxField);//添加列表框到PDF
baseY += 60;
//添加单选按钮
page.getCanvas().drawString("招收方式:", font, brush1, new Point2D.Float(0, baseY));
PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "Radio");//创建单选按钮对象
PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("Item1");//创建第一个单选按钮
radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
page.getCanvas().drawString("全日制", font, brush2, new Point2D.Float(baseX + 20, baseY));
PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("Item2");//创建第二个单选按钮
radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
page.getCanvas().drawString("成人教育", font, brush2, new Point2D.Float(baseX + 90, baseY));
radioButtonListField.getItems().add(radioItem1);
radioButtonListField.getItems().add(radioItem2);
radioButtonListField.setSelectedIndex(0);//选择填充第一个单选按钮
doc.getForm().getFields().add(radioButtonListField);//添加单选按钮到PDF
baseY += 25;
//添加组合框
page.getCanvas().drawString("最高学历:", font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);//创建cmbBounds对象
PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "ComboBox");//创建comboBoxField对象
comboBoxField.setBounds(cmbBounds);
comboBoxField.getItems().add(new PdfListFieldItem("博士", "item1"));
comboBoxField.getItems().add(new PdfListFieldItem("硕士", "itme2"));
comboBoxField.getItems().add(new PdfListFieldItem("本科", "item3"));
comboBoxField.getItems().add(new PdfListFieldItem("大专", "item4"));
comboBoxField.setSelectedIndex(0);      
comboBoxField.setFont(font);
doc.getForm().getFields().add(comboBoxField);//添加组合框到PDF
baseY += 25;
//添加签名域
page.getCanvas().drawString("本人签字确认\n以上信息属实:", font, brush1, new Point2D.Float(0, baseY));
PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");//创建sgnField对象
Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);//创建sgnBounds对象
sgnField.setBounds(sgnBounds);          
doc.getForm().getFields().add(sgnField);//添加sgnField到PDF
baseY += 90;
//添加按钮
page.getCanvas().drawString("", font, brush1, new Point2D.Float(0, baseY));
Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);//创建btnBounds对象
PdfButtonField buttonField = new PdfButtonField(page, "Button");//创建buttonField对象
buttonField.setBounds(btnBounds);
buttonField.setText("提交");//设置按钮显示文本
buttonField.setFont(font);
doc.getForm().getFields().add(buttonField);//添加按钮到PDF  
//保存文档
doc.saveToFile("result.pdf", FileFormat.PDF);              
}
}

创建(填充)效果:

【示例2】 加载并填充已有的表单域文档

测试文档如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.*;
public class FillFormField_PDF{
public static void main(String[] args){
//创建PdfDocument对象,并加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("output.pdf");
//获取文档中的域
PdfFormWidget form = (PdfFormWidget) doc.getForm();        
//获取域控件集合
PdfFormFieldWidgetCollection formWidgetCollection = form.getFieldsWidget();
//遍历域控件并填充数据
for (int i = 0; i < formWidgetCollection.getCount(); i++) {
PdfField field = formWidgetCollection.get(i);         
if (field instanceof PdfTextBoxFieldWidget) {
PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget) field;
textBoxField.setText("吴 敏");
}  
if (field instanceof PdfCheckBoxWidgetFieldWidget) {
PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget) field;
switch(checkBoxField.getName()){
case "CheckBox1":
checkBoxField.setChecked(true);
break;
case "CheckBox2":
checkBoxField.setChecked(true);
break;
}
}
if (field instanceof PdfRadioButtonListFieldWidget) {
PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFieldWidget) field;
radioButtonListField.setSelectedIndex(1);
}
if (field instanceof PdfListBoxWidgetFieldWidget) {
PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget) field;
listBox.setSelectedIndex(1);
}
if (field instanceof PdfComboBoxWidgetFieldWidget) {
PdfComboBoxWidgetFieldWidget comboBoxField = (PdfComboBoxWidgetFieldWidget) field;
comboBoxField.setSelectedIndex(1);
}
}
//保存文档
doc.saveToFile("FillFormFields.pdf", FileFormat.PDF);
}
}

填充效果:

【示例3】限制表单域编辑(只读)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import com.spire.pdf.PdfDocument;
public class FieldReadonly_PDF {
public static void main(String[] args) throws Exception {
{
//创建PdfDocument对象,并加载包含表单域的PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("test.pdf");
//将文档中的所有表单域设置为只读
pdf.getForm().setReadOnly(true);
//保存文档
pdf.saveToFile("result.pdf");   
}
}

生成的文档中,表单域将不可编辑,为只读状态。

【示例4】获取表单域值

     1. 获取指定表单域值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import com.spire.pdf.PdfDocument;
import com.spire.pdf.widget.PdfFormWidget;
import com.spire.pdf.widget.PdfTextBoxFieldWidget;
import java.io.FileWriter;
import java.io.IOException;
public class GetSpecificFormfieldValue {
public static void main(String[] args) {
//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("test.pdf");
//获取表单域
PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
//通过索引值来获取指定表单域中的值
PdfTextBoxFieldWidget textbox = ( PdfTextBoxFieldWidget)formWidget.getFieldsWidget().get(0);
//PdfTextBoxFieldWidget textbox = ( PdfTextBoxFieldWidget)formWidget.getFieldsWidget().get("TextBox");//通过表单域名称来获取值
//将获取的值写入txt文档
String text = textbox.getText();
try {
//将文本写入 .txt文件
FileWriter writer = new FileWriter("GetSpecificFormfieldValue.txt");
writer.write(text);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
pdf.close();
}
}

指定表单域值获取结果:

    2. 获取全部表单域值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.*;
import java.io.FileWriter;
import java.io.IOException;
public class GetAllFormfieldValue {
public static void main(String[] args) {
//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("测试.pdf");
//获取表单域
PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
StringBuilder sb = new StringBuilder();
//遍历表单域控件集合并提取所有表单的值
for (int i = 0; i < formWidget.getFieldsWidget().getCount(); i++)
{
PdfField field = (PdfField)formWidget.getFieldsWidget().getList().get(i);
//获取文本框的值
if (field instanceof PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field ;
String text = textBoxField.getText();
sb.append("文本框内容: " + text + "\r\n");
}
//获取列表框的值
if (field instanceof PdfListBoxWidgetFieldWidget)
{
PdfListBoxWidgetFieldWidget listBoxField = (PdfListBoxWidgetFieldWidget)field;
//获取列表框中选中的值
String selectedValue = listBoxField.getSelectedValue();
sb.append("列表框选中的内容: " + selectedValue + "\r\n");
//获取列表框中的所有选项值
sb.append("列表框内容: \r\n");
PdfListWidgetItemCollection items = listBoxField.getValues();
for (PdfListWidgetItem item : (Iterable<PdfListWidgetItem>) items)
{
sb.append(item.getValue() + "\r\n");
}
}
//获取组合框的值
if (field instanceof PdfComboBoxWidgetFieldWidget)
{
PdfComboBoxWidgetFieldWidget comBoxField = (PdfComboBoxWidgetFieldWidget)field ;
//获取组合框中选中的值
String selectedValue = comBoxField.getSelectedValue();
sb.append("组合框选中的内容: " + selectedValue + "\r\n");
//获取组合框中所有选项值
sb.append("组合框内容: \r\n");
PdfListWidgetItemCollection items = comBoxField.getValues();
for (PdfListWidgetItem item : (Iterable<PdfListWidgetItem>) items)
{
sb.append(item.getValue() + "\r\n");
}
}
//获取单选按钮值
if (field instanceof PdfRadioButtonListFieldWidget)
{
PdfRadioButtonListFieldWidget radioBtnField = (PdfRadioButtonListFieldWidget)field;
String Value = radioBtnField.getValue();
sb.append("单选按钮内容: " + Value + "\r\n");
}
//获取复选框值
if (field instanceof PdfCheckBoxWidgetFieldWidget)
{
PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget)field;
//获取复选框的选中状态
boolean state = checkBoxField.getChecked();
sb.append("复选框是否被选中? " + state + "\r\n");
}
}
try {
//将文本写入 .txt文件
FileWriter writer = new FileWriter("GetAllFormfieldValues.txt");
writer.write(sb.toString());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
pdf.close();
}
}

表单域读取结果:

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!