Java創(chuàng)建和填充PDF表單域方法
表單域,可以按用途分為多種不同的類(lèi)型,常見(jiàn)的有文本框、多行文本框、密碼框、隱藏域、復(fù)選框、單選框和下拉選擇框等,目的是用于采集用戶的輸入或選擇的數(shù)據(jù)。下面的示例中,將分享通過(guò)Java編程在PDF中添加以及填充表單域的方法。這里填充表單域可分為2種情況,一種是在創(chuàng)建表單域時(shí)填充,一種是加載已經(jīng)創(chuàng)建好表單域的文檔進(jìn)行填充。此外,對(duì)于已經(jīng)創(chuàng)建表單域并填寫(xiě)好的文檔,也可以設(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è)文件夾可命名為L(zhǎng)ib。并將產(chǎn)品包中的2個(gè)jar文件復(fù)制到新建的文件夾下。

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

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ì)象,并添加頁(yè)面
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.getPages().add();
//初始化位置變量
float baseX = 100;
float baseY = 0;
//創(chuàng)建畫(huà)刷對(duì)象
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = 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, brush1, 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, 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");//創(chuàng)建第一個(gè)復(fù)選框?qū)ο?
checkBoxField.setBounds(rec1);
checkBoxField.setChecked(false);//填充復(fù)選框
page.getCanvas().drawString("經(jīng)管系", 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");//創(chuàng)建第二個(gè)復(fù)選框?qū)ο?
checkBoxField1.setBounds(rec2);
checkBoxField1.setChecked(true);//填充復(fù)選框
page.getCanvas().drawString("創(chuàng)新班", font, brush2, new Point2D.Float(baseX+90, baseY));
doc.getForm().getFields().add(checkBoxField);//添加復(fù)選框到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");//創(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, brush1, 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, brush2, 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, brush2, 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, brush1, 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, brush1, 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, brush1, 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)建(填充)效果:

2.加載并填充已有的表單域文檔
測(cè)試文檔如下:

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);
}
}
填充效果:

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)
相關(guān)文章
舉例講解Java設(shè)計(jì)模式編程中模板方法模式的運(yùn)用實(shí)例
這篇文章主要介紹了Java設(shè)計(jì)模式編程中模板方法模式的運(yùn)用實(shí)例,模板方法模式強(qiáng)調(diào)基于繼承的代碼復(fù)用,需要的朋友可以參考下2016-05-05
Gauva使用ListenableFuture介紹說(shuō)明
并發(fā)是一個(gè)困難問(wèn)題,但是通過(guò)強(qiáng)大和強(qiáng)大的抽象能夠顯著的簡(jiǎn)化工作。為了簡(jiǎn)化問(wèn)題,Gauva使用ListenableFuture擴(kuò)展了JDK的Future接口,這篇文章主要介紹了Gauva使用ListenableFuture2023-01-01
SpringBoot詳解如果通過(guò)@Value注解給靜態(tài)變量注入值
這篇文章主要介紹了springboot如何通過(guò)@Value給靜態(tài)變量注入值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解
這篇文章主要介紹了springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決
這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增)
本文主要介紹了MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢時(shí)攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10

