ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:287.93KB ,
资源ID:26405861      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/26405861.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java 创建填充PDF表单域.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java 创建填充PDF表单域.docx

1、Java 创建填充PDF表单域Java 创建、填充PDF表单域表单域,可以按用途分为多种不同的类型,常见的有文本框、多行文本框、密码框、隐藏域、复选框、单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据。下面的示例中,将分享通过Java编程在PDF中添加以及填充表单域的方法。包括:文本框、复选框、单选按钮、列表框、组合框、签名域、按钮等。这里填充表单域可分为2种情况,一种是在创建表单域时填充,一种是加载已经创建好表单域的文档进行填充。此外,对于已经创建表单域并填写好的文档,也可以设置只读,防止修改、编辑。要点概括:1.创建表单域2.填充表单域3.设置表单域只读工具:Free Spire

2、.PDF for Java v2.0.0(免费版)Jar文件导入步骤1:在Java程序中新建一个文件夹可命名为Lib。并将产品包中的2个jar文件复制到新建的文件夹下。步骤2:复制文件后,添加到引用类库:选中这两个jar文件,点击鼠标右键,选择“Build Path” “Add to Build Path”。完成引用。Java代码示例(供参考)1.创建并填充PDF表单域import java.awt.*;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import com.spire.pdf.FileFormat;im

3、port 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.getPa

4、ges().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,

5、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.

6、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.geo

7、m.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.Recta

8、ngle2D.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.Fl

9、oat(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 listBoxFie

10、ld = 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); listB

11、oxField.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);/创建单选按钮对象 PdfRadioButto

12、nListItem 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);/创建第二个单选按钮

13、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);/选择填

14、充第一个单选按钮 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 Pd

15、fComboBoxField(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

16、(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 PdfSign

17、atureField(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,

18、 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().ad

19、d(buttonField);/添加按钮到PDF /保存文档 doc.saveToFile(result.pdf, FileFormat.PDF); 创建(填充)效果:2.加载并填充已有的表单域文档测试文档如下: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

20、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.getC

21、ount(); i+) PdfField field = formWidgetCollection.get(i); if (field instanceof PdfTextBoxFieldWidget) PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget) field; textBoxField.setText(吴 敏); if (field instanceof PdfCheckBoxWidgetFieldWidget) PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfChe

22、ckBoxWidgetFieldWidget) field; switch(checkBoxField.getName() case CheckBox1: checkBoxField.setChecked(true); break; case CheckBox2: checkBoxField.setChecked(true); break; if (field instanceof PdfRadioButtonListFieldWidget) PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFiel

23、dWidget) field; radioButtonListField.setSelectedIndex(1); if (field instanceof PdfListBoxWidgetFieldWidget) PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget) field; listBox.setSelectedIndex(1); if (field instanceof PdfComboBoxWidgetFieldWidget) PdfComboBoxWidgetFieldWidget comboBox

24、Field = (PdfComboBoxWidgetFieldWidget) field; comboBoxField.setSelectedIndex(1); /保存文档 doc.saveToFile(FillFormFields.pdf, FileFormat.PDF); 填充效果:3.限制表单域编辑(只读) 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); 生成的文档中,表单域将不可编辑,为只读状态。(本文完)

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

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