pdf表单域,也称交互域,主要用于收集用户信息。常见的表单域包括文本框、单选按钮、复选框、列表框和组合框等。文本将介绍如何使用spire.pdf for java在java程序中创建pdf表单域。
import java.awt.*;
import java.awt.geom.point2d;
import java.awt.geom.rectangle2d;
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,12),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("选项1", 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(false);
page.getcanvas().drawstring("选项2", font, brush2, new point2d.float(basex 90, basey));
doc.getform().getfields().add(checkboxfield);
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("项目1", "item1"));
listboxfield.getitems().add(new pdflistfielditem("项目2", "item2"));
listboxfield.getitems().add(new pdflistfielditem("项目3", "item3"));;
listboxfield.setbounds(rec);
listboxfield.setfont(font);
listboxfield.setselectedindex(0);
doc.getform().getfields().add(listboxfield);
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("选项1", 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("选项2", font, brush2, new point2d.float(basex 90, basey));
radiobuttonlistfield.getitems().add(radioitem1);
radiobuttonlistfield.getitems().add(radioitem2);
radiobuttonlistfield.setselectedindex(0);
doc.getform().getfields().add(radiobuttonlistfield);
basey = 25;
//添加组合框
page.getcanvas().drawstring("组合框:", font, brush1, new point2d.float(0, basey));
rectangle2d.float cmbbounds = new rectangle2d.float(basex, basey, 150, 15);
pdfcomboboxfield comboboxfield = new pdfcomboboxfield(page, "combobox");
comboboxfield.setbounds(cmbbounds);
comboboxfield.getitems().add(new pdflistfielditem("项目1", "item1"));
comboboxfield.getitems().add(new pdflistfielditem("项目2", "itme2"));
comboboxfield.getitems().add(new pdflistfielditem("项目3", "item3"));
comboboxfield.getitems().add(new pdflistfielditem("项目4", "item4"));
comboboxfield.setselectedindex(0);
comboboxfield.setfont(font);
doc.getform().getfields().add(comboboxfield);
basey = 25;
//添加签名域
page.getcanvas().drawstring("签名域:", font, brush1, new point2d.float(0, basey));
pdfsignaturefield sgnfield= new pdfsignaturefield(page,"sgnfield");
rectangle2d.float sgnbounds = new rectangle2d.float(basex, basey, 150, 80);
sgnfield.setbounds(sgnbounds);
doc.getform().getfields().add(sgnfield);
basey = 90;
//添加按钮
page.getcanvas().drawstring("提交按钮:", font, brush1, new point2d.float(0, basey));
rectangle2d.float btnbounds = new rectangle2d.float(basex, basey, 50, 15);
pdfbuttonfield buttonfield = new pdfbuttonfield(page, "button");
buttonfield.setbounds(btnbounds);
buttonfield.settext("提交");
buttonfield.setfont(font);
doc.getform().getfields().add(buttonfield);
//保存文档
doc.savetofile("addformfield.pdf", fileformat.pdf);
}
}