Java 创建、填充PDF表单域
表單域,可以按用途分為多種不同的類型,常見的有文本框、多行文本框、密碼框、隱藏域、復選框、單選框和下拉選擇框等,目的是用于采集用戶的輸入或選擇的數據。下面的示例中,將分享通過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);//設置文本框的BoundstextBox.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);//添加復選框到PDFbaseY += 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);//添加列表框到PDFbaseY += 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);//添加單選按鈕到PDFbaseY += 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);//添加組合框到PDFbaseY += 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到PDFbaseY += 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工程師朋友們加入Java群: 741514154 群內提供免費的Java架構學習資料(里面有高可用、高并發、高性能及分布式、Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用自己每一分每一秒的時間來學習提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代!
轉載于:https://juejin.im/post/5c63cc4fe51d450159027304
總結
以上是生活随笔為你收集整理的Java 创建、填充PDF表单域的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 笔记: 环境 - Postgre从安装到
- 下一篇: 209. 单点登录(SSO)
