溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java創(chuàng)建和填充PDF表單域方法

發(fā)布時(shí)間:2020-09-03 07:13:24 來源:腳本之家 閱讀:156 作者:laozhang 欄目:編程語言

表單域,可以按用途分為多種不同的類型,常見的有文本框、多行文本框、密碼框、隱藏域、復(fù)選框、單選框和下拉選擇框等,目的是用于采集用戶的輸入或選擇的數(shù)據(jù)。下面的示例中,將分享通過Java編程在PDF中添加以及填充表單域的方法。這里填充表單域可分為2種情況,一種是在創(chuàng)建表單域時(shí)填充,一種是加載已經(jīng)創(chuàng)建好表單域的文檔進(jìn)行填充。此外,對(duì)于已經(jīng)創(chuàng)建表單域并填寫好的文檔,也可以設(shè)置只讀,防止修改、編輯。

要點(diǎn)概括:

1.創(chuàng)建表單域

2.填充表單域

3.設(shè)置表單域只讀

工具:Free Spire.PDF for Java v2.0.0(免費(fèi)版)

Jar文件導(dǎo)入

步驟1:在Java程序中新建一個(gè)文件夾可命名為Lib。并將產(chǎn)品包中的2個(gè)jar文件復(fù)制到新建的文件夾下。

Java創(chuàng)建和填充PDF表單域方法

步驟2:復(fù)制文件后,添加到引用類庫:選中這兩個(gè)jar文件,點(diǎn)擊鼠標(biāo)右鍵,選擇“Build Path” – “Add to Build Path”。完成引用。

Java創(chuàng)建和填充PDF表單域方法

Java代碼示例(供參考)

1.創(chuàng)建并填充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 {
   
  //創(chuàng)建PdfDocument對(duì)象,并添加頁面
  PdfDocument doc = new PdfDocument();  
  PdfPageBase page = doc.getPages().add();
 
  //初始化位置變量
  float baseX = 100;
  float baseY = 0;
 
  //創(chuàng)建畫刷對(duì)象
  PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
  PdfSolidBrush brush3 = new PdfSolidBrush(new PdfRGBColor(Color.black));
   
  //創(chuàng)建TrueType字體
  PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,10),true); 
 
  //添加文本框
  String text = "姓名:";//添加文本
  page.getCanvas().drawString(text, font, brush2, new Point2D.Float(0, baseY));//在PDF中繪制文字
  Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY , 150, 15);//創(chuàng)建Rectangle2D對(duì)象
  PdfTextBoxField textBox = new PdfTextBoxField(page, "TextBox");//創(chuàng)建文本框?qū)ο?  textBox.setBounds(tbxBounds);//設(shè)置文本框的Bounds
  textBox.setText("劉興");//填充文本框
  textBox.setFont(font);//應(yīng)用文本框的字體
  doc.getForm().getFields().add(textBox);//添加文本框到PDF域的集合
  baseY +=25;
 
  //添加復(fù)選框
  page.getCanvas().drawString("所在院系:", font, brush2, 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");//創(chuàng)建第一個(gè)復(fù)選框?qū)ο?  checkBoxField.setBounds(rec1);
  checkBoxField.setChecked(false);//填充復(fù)選框
  page.getCanvas().drawString("經(jīng)管系", font, brush3, 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");//創(chuàng)建第二個(gè)復(fù)選框?qū)ο?  checkBoxField1.setBounds(rec2);
  checkBoxField1.setChecked(true);//填充復(fù)選框
  page.getCanvas().drawString("創(chuàng)新班", font, brush3, new Point2D.Float(baseX+90, baseY));  
  doc.getForm().getFields().add(checkBoxField);//添加復(fù)選框到PDF
  baseY += 25;
 
  //添加列表框
  page.getCanvas().drawString("錄取批次:", font, brush2, 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");//創(chuàng)建列表框?qū)ο?  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, brush2, new Point2D.Float(0, baseY));
  PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "Radio");//創(chuàng)建單選按鈕對(duì)象
  PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("Item1");//創(chuàng)建第一個(gè)單選按鈕
  radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
  page.getCanvas().drawString("全日制", font, brush3, new Point2D.Float(baseX + 20, baseY));
  PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("Item2");//創(chuàng)建第二個(gè)單選按鈕
  radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
  page.getCanvas().drawString("成人教育", font, brush3, new Point2D.Float(baseX + 90, baseY));
  radioButtonListField.getItems().add(radioItem1);
  radioButtonListField.getItems().add(radioItem2);
  radioButtonListField.setSelectedIndex(0);//選擇填充第一個(gè)單選按鈕
  doc.getForm().getFields().add(radioButtonListField);//添加單選按鈕到PDF
  baseY += 25;
 
  //添加組合框
  page.getCanvas().drawString("最高學(xué)歷:", font, brush2, new Point2D.Float(0, baseY));
  Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);//創(chuàng)建cmbBounds對(duì)象
  PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "ComboBox");//創(chuàng)建comboBoxField對(duì)象
  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("本人簽字確認(rèn)\n以上信息屬實(shí):", font, brush2, new Point2D.Float(0, baseY));
  PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");//創(chuàng)建sgnField對(duì)象
  Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);//創(chuàng)建sgnBounds對(duì)象
  sgnField.setBounds(sgnBounds);   
  doc.getForm().getFields().add(sgnField);//添加sgnField到PDF
  baseY += 90;
 
  //添加按鈕
  page.getCanvas().drawString("", font, brush2, new Point2D.Float(0, baseY));
  Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);//創(chuàng)建btnBounds對(duì)象
  PdfButtonField buttonField = new PdfButtonField(page, "Button");//創(chuàng)建buttonField對(duì)象
  buttonField.setBounds(btnBounds);
  buttonField.setText("提交");//設(shè)置按鈕顯示文本
  buttonField.setFont(font);
  doc.getForm().getFields().add(buttonField);//添加按鈕到PDF 
   
  //保存文檔
  doc.saveToFile("result.pdf", FileFormat.PDF);    
 }
}

創(chuàng)建(填充)效果:

Java創(chuàng)建和填充PDF表單域方法

2.加載并填充已有的表單域文檔

測試文檔如下:

Java創(chuàng)建和填充PDF表單域方法

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){
   
  //創(chuàng)建PdfDocument對(duì)象,并加載PDF文檔
  PdfDocument doc = new PdfDocument();
  doc.loadFromFile("output.pdf");
 
  //獲取文檔中的域
  PdfFormWidget form = (PdfFormWidget) doc.getForm();  
  //獲取域控件集合
  PdfFormFieldWidgetCollection formWidgetCollection = form.getFieldsWidget();
 
  //遍歷域控件并填充數(shù)據(jù)
  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);
 }
}

填充效果:

Java創(chuàng)建和填充PDF表單域方法

3.限制表單域編輯(只讀)

import com.spire.pdf.PdfDocument;
 
public class FieldReadonly_PDF {
 public static void main(String[] args) throws Exception {
 {
 //創(chuàng)建PdfDocument對(duì)象,并加載包含表單域的PDF文檔
 PdfDocument pdf = new PdfDocument();
 pdf.loadFromFile("test.pdf");
  
  //將文檔中的所有表單域設(shè)置為只讀
  pdf.getForm().setReadOnly(true);
  
  //保存文檔
  pdf.saveToFile("result.pdf"); 
  }
 }

生成的文檔中,表單域?qū)⒉豢删庉?,為只讀狀態(tài)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI