表单域,能够按用处分为多种差别的范例,罕见的有文本框、多行文本框、暗码框、隐蔽域、复选框、单选框和下拉挑选框等,目标是用于收集用户的输入或挑选的数据。下面的示例中,将分享经由过程Java编程在PDF中增加以及添补表单域的要领。这里添补表单域可分为2种状况,一种是在建立表单域时添补,一种是加载已建立好表单域的文档举行添补。另外,关于已建立表单域并填写好的文档,也能够设置只读,防备修正、编辑。
要点归纳综合:
1.建立表单域
2.添补表单域
3.设置表单域只读
东西:Free Spire.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; 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.加载并添补已有的表单域文档
测试文档以下:
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.限定表单域编辑(只读)
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"); } }
生成的文档中,表单域将不可编辑,为只读状况
以上就是Java怎样建立和添补PDF表单域(代码示例)的细致内容,更多请关注ki4网别的相干文章!