Java 创建填充PDF表单域.docx

上传人:b****3 文档编号:26405861 上传时间:2023-06-19 格式:DOCX 页数:13 大小:287.93KB
下载 相关 举报
Java 创建填充PDF表单域.docx_第1页
第1页 / 共13页
Java 创建填充PDF表单域.docx_第2页
第2页 / 共13页
Java 创建填充PDF表单域.docx_第3页
第3页 / 共13页
Java 创建填充PDF表单域.docx_第4页
第4页 / 共13页
Java 创建填充PDF表单域.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

Java 创建填充PDF表单域.docx

《Java 创建填充PDF表单域.docx》由会员分享,可在线阅读,更多相关《Java 创建填充PDF表单域.docx(13页珍藏版)》请在冰豆网上搜索。

Java 创建填充PDF表单域.docx

Java创建填充PDF表单域

Java创建、填充PDF表单域

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

下面的示例中,将分享通过Java编程在PDF中添加以及填充表单域的方法。

包括:

文本框、复选框、单选按钮、列表框、组合框、签名域、按钮等。

这里填充表单域可分为2种情况,一种是在创建表单域时填充,一种是加载已经创建好表单域的文档进行填充。

此外,对于已经创建表单域并填写好的文档,也可以设置只读,防止修改、编辑。

要点概括:

1.创建表单域

2.填充表单域

3.设置表单域只读

工具:

FreeSpire.PDFforJavav2.0.0(免费版)

Jar文件导入

步骤1:

在Java程序中新建一个文件夹可命名为Lib。

并将产品包中的2个jar文件复制到新建的文件夹下。

步骤2:

复制文件后,添加到引用类库:

选中这两个jar文件,点击鼠标右键,选择“BuildPath”–“AddtoBuildPath”。

完成引用。

Java代码示例(供参考)

1.创建并填充PDF表单域

importjava.awt.*;

importjava.awt.geom.Point2D;

importjava.awt.geom.Rectangle2D;

importcom.spire.pdf.FileFormat;

importcom.spire.pdf.PdfDocument;

importcom.spire.pdf.PdfPageBase;

importcom.spire.pdf.fields.*;

importcom.spire.pdf.graphics.*;

publicclassAddFormFieldsToPdf{

publicstaticvoidmain(String[]args)throwsException{

//创建PdfDocument对象,并添加页面

PdfDocumentdoc=newPdfDocument();

PdfPageBasepage=doc.getPages().add();

//初始化位置变量

floatbaseX=100;

floatbaseY=0;

//创建画刷对象

PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));

PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.black));

//创建TrueType字体

PdfTrueTypeFontfont=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);

//添加文本框

Stringtext="姓名:

";//添加文本

page.getCanvas().drawString(text,font,brush1,newPoint2D.Float(0,baseY));//在PDF中绘制文字

Rectangle2D.FloattbxBounds=newRectangle2D.Float(baseX,baseY,150,15);//创建Rectangle2D对象

PdfTextBoxFieldtextBox=newPdfTextBoxField(page,"TextBox");//创建文本框对象

textBox.setBounds(tbxBounds);//设置文本框的Bounds

textBox.setText("刘兴");//填充文本框

textBox.setFont(font);//应用文本框的字体

doc.getForm().getFields().add(textBox);//添加文本框到PDF域的集合

baseY+=25;

//添加复选框

page.getCanvas().drawString("所在院系:

",font,brush1,newPoint2D.Float(0,baseY));

java.awt.geom.Rectangle2D.Floatrec1=newjava.awt.geom.Rectangle2D.Float(baseX,baseY,15,15);

PdfCheckBoxFieldcheckBoxField=newPdfCheckBoxField(page,"CheckBox1");//创建第一个复选框对象

checkBoxField.setBounds(rec1);

checkBoxField.setChecked(false);//填充复选框

page.getCanvas().drawString("经管系",font,brush2,newPoint2D.Float(baseX+20,baseY));

java.awt.geom.Rectangle2D.Floatrec2=newjava.awt.geom.Rectangle2D.Float(baseX+70,baseY,15,15);

PdfCheckBoxFieldcheckBoxField1=newPdfCheckBoxField(page,"CheckBox2");//创建第二个复选框对象

checkBoxField1.setBounds(rec2);

checkBoxField1.setChecked(true);//填充复选框

page.getCanvas().drawString("创新班",font,brush2,newPoint2D.Float(baseX+90,baseY));

doc.getForm().getFields().add(checkBoxField);//添加复选框到PDF

baseY+=25;

//添加列表框

page.getCanvas().drawString("录取批次:

",font,brush1,newPoint2D.Float(0,baseY));

java.awt.geom.Rectangle2D.Floatrec=newjava.awt.geom.Rectangle2D.Float(baseX,baseY,150,50);

PdfListBoxFieldlistBoxField=newPdfListBoxField(page,"ListBox");//创建列表框对象

listBoxField.getItems().add(newPdfListFieldItem("第一批次","item1"));

listBoxField.getItems().add(newPdfListFieldItem("第二批次","item2"));

listBoxField.getItems().add(newPdfListFieldItem("第三批次","item3"));;

listBoxField.setBounds(rec);

listBoxField.setFont(font);

listBoxField.setSelectedIndex(0);//填充列表框

doc.getForm().getFields().add(listBoxField);//添加列表框到PDF

baseY+=60;

//添加单选按钮

page.getCanvas().drawString("招收方式:

",font,brush1,newPoint2D.Float(0,baseY));

PdfRadioButtonListFieldradioButtonListField=newPdfRadioButtonListField(page,"Radio");//创建单选按钮对象

PdfRadioButtonListItemradioItem1=newPdfRadioButtonListItem("Item1");//创建第一个单选按钮

radioItem1.setBounds(newRectangle2D.Float(baseX,baseY,15,15));

page.getCanvas().drawString("全日制",font,brush2,newPoint2D.Float(baseX+20,baseY));

PdfRadioButtonListItemradioItem2=newPdfRadioButtonListItem("Item2");//创建第二个单选按钮

radioItem2.setBounds(newRectangle2D.Float(baseX+70,baseY,15,15));

page.getCanvas().drawString("成人教育",font,brush2,newPoint2D.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,newPoint2D.Float(0,baseY));

Rectangle2D.FloatcmbBounds=newRectangle2D.Float(baseX,baseY,150,15);//创建cmbBounds对象

PdfComboBoxFieldcomboBoxField=newPdfComboBoxField(page,"ComboBox");//创建comboBoxField对象

comboBoxField.setBounds(cmbBounds);

comboBoxField.getItems().add(newPdfListFieldItem("博士","item1"));

comboBoxField.getItems().add(newPdfListFieldItem("硕士","itme2"));

comboBoxField.getItems().add(newPdfListFieldItem("本科","item3"));

comboBoxField.getItems().add(newPdfListFieldItem("大专","item4"));

comboBoxField.setSelectedIndex(0);

comboBoxField.setFont(font);

doc.getForm().getFields().add(comboBoxField);//添加组合框到PDF

baseY+=25;

//添加签名域

page.getCanvas().drawString("本人签字确认\n以上信息属实:

",font,brush1,newPoint2D.Float(0,baseY));

PdfSignatureFieldsgnField=newPdfSignatureField(page,"sgnField");//创建sgnField对象

Rectangle2D.FloatsgnBounds=newRectangle2D.Float(baseX,baseY,150,80);//创建sgnBounds对象

sgnField.setBounds(sgnBounds);

doc.getForm().getFields().add(sgnField);//添加sgnField到PDF

baseY+=90;

//添加按钮

page.getCanvas().drawString("",font,brush1,newPoint2D.Float(0,baseY));

Rectangle2D.FloatbtnBounds=newRectangle2D.Float(baseX,baseY,50,15);//创建btnBounds对象

PdfButtonFieldbuttonField=newPdfButtonField(page,"Button");//创建buttonField对象

buttonField.setBounds(btnBounds);

buttonField.setText("提交");//设置按钮显示文本

buttonField.setFont(font);

doc.getForm().getFields().add(buttonField);//添加按钮到PDF

//保存文档

doc.saveToFile("result.pdf",FileFormat.PDF);

}

}

创建(填充)效果:

2.加载并填充已有的表单域文档

测试文档如下:

importcom.spire.pdf.FileFormat;

importcom.spire.pdf.PdfDocument;

importcom.spire.pdf.fields.PdfField;

importcom.spire.pdf.widget.*;

publicclassFillFormField_PDF{

publicstaticvoidmain(String[]args){

//创建PdfDocument对象,并加载PDF文档

PdfDocumentdoc=newPdfDocument();

doc.loadFromFile("output.pdf");

//获取文档中的域

PdfFormWidgetform=(PdfFormWidget)doc.getForm();

//获取域控件集合

PdfFormFieldWidgetCollectionformWidgetCollection=form.getFieldsWidget();

//遍历域控件并填充数据

for(inti=0;i

PdfFieldfield=formWidgetCollection.get(i);

if(fieldinstanceofPdfTextBoxFieldWidget){

PdfTextBoxFieldWidgettextBoxField=(PdfTextBoxFieldWidget)field;

textBoxField.setText("吴敏");

}

if(fieldinstanceofPdfCheckBoxWidgetFieldWidget){

PdfCheckBoxWidgetFieldWidgetcheckBoxField=(PdfCheckBoxWidgetFieldWidget)field;

switch(checkBoxField.getName()){

case"CheckBox1":

checkBoxField.setChecked(true);

break;

case"CheckBox2":

checkBoxField.setChecked(true);

break;

}

}

if(fieldinstanceofPdfRadioButtonListFieldWidget){

PdfRadioButtonListFieldWidgetradioButtonListField=(PdfRadioButtonListFieldWidget)field;

radioButtonListField.setSelectedIndex

(1);

}

if(fieldinstanceofPdfListBoxWidgetFieldWidget){

PdfListBoxWidgetFieldWidgetlistBox=(PdfListBoxWidgetFieldWidget)field;

listBox.setSelectedIndex

(1);

}

if(fieldinstanceofPdfComboBoxWidgetFieldWidget){

PdfComboBoxWidgetFieldWidgetcomboBoxField=(PdfComboBoxWidgetFieldWidget)field;

comboBoxField.setSelectedIndex

(1);

}

}

//保存文档

doc.saveToFile("FillFormFields.pdf",FileFormat.PDF);

}

}

填充效果:

3.限制表单域编辑(只读)

importcom.spire.pdf.PdfDocument;

publicclassFieldReadonly_PDF{

publicstaticvoidmain(String[]args)throwsException{

{

//创建PdfDocument对象,并加载包含表单域的PDF文档

PdfDocumentpdf=newPdfDocument();

pdf.loadFromFile("test.pdf");

//将文档中的所有表单域设置为只读

pdf.getForm().setReadOnly(true);

//保存文档

pdf.saveToFile("result.pdf");

}

}

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

(本文完)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 远程网络教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1