adobe acrobat form (acroform) 是表单域的集合,用来以交互方式从用户那里收集信息。spire.pdf支持创建多种交互式表单域,例如:文本域、单选按钮、复选框、列表框、组合框,并在特定的表单域添加提示文本(tooltip),方便用户输入正确信息。
创建表单域
c#
//创建pdf文档并添加一页
pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();
//设置font, brush
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 10f,fontstyle.regular), true);
pdfbrush brush = pdfbrushes.black;
//定义一些坐标变量并赋初值
float x = 10;
float y = 10;
float tempx = 0;
float tempy = 0;
//添加文本框
string text = "文本框: ";
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdftextboxfield textbox = new pdftextboxfield(page, "textbox");
textbox.bounds = new rectanglef(tempx, y, tempx * 2, 15);
textbox.borderwidth = 0.75f;
textbox.borderstyle = pdfborderstyle.solid;
pdf.form.fields.add(textbox);
//添加复选框
text = "复选框: ";
y = tempy;
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdfcheckboxfield checkbox = new pdfcheckboxfield(page, "checkbox");
checkbox.bounds = new rectanglef(tempx, y, 15, 15);
checkbox.borderwidth = 0.75f;
checkbox.style = pdfcheckboxstyle.cross;
pdf.form.fields.add(checkbox);
//添加列表框
text = "列表框: ";
y = tempy;
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdflistboxfield listbox = new pdflistboxfield(page, "listbox");
listbox.bounds = new rectanglef(tempx, y, tempx * 2, tempy * 2);
listbox.borderwidth = 0.75f;
for (int i = 0; i < 3; i )
{
//添加项目到列表框
string temptext = string.format("text {0}", i);
string tempvalue = string.format("value {0}", i);
listbox.items.add(new pdflistfielditem(temptext, tempvalue));
}
pdf.form.fields.add(listbox);
//添加单选按钮
text = "单选按钮: ";
y = tempy * 2 15;
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdfradiobuttonlistfield radiobutton = new pdfradiobuttonlistfield(page, "radiobutton");
for (int i = 0; i < 3; i )
{
pdfradiobuttonlistitem item = new pdfradiobuttonlistitem(string.format("rb{0}", i));
item.borderwidth = 0.75f;
item.bounds = new rectanglef(tempx i * 20, y, 15, 15);
radiobutton.items.add(item);
}
pdf.form.fields.add(radiobutton);
//添加组合框
text = "下拉列表框: ";
y = tempy;
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdfcomboboxfield combobox = new pdfcomboboxfield(page, "combobox");
combobox.bounds = new rectanglef(tempx, y, tempx, 15);
combobox.borderwidth = 0.75f;
for (int i = 0; i < 3; i )
{
//添加项目到下拉列表框
string temptext = string.format("text {0}", i);
string tempvalue = string.format("value {0}", i);
combobox.items.add(new pdflistfielditem(temptext, tempvalue));
}
pdf.form.fields.add(combobox);
//保存文档
pdf.savetofile("pdf表单域.pdf");
vb.net
'创建pdf文档并添加一页
dim pdf as new pdfdocument()
dim page as pdfpagebase = pdf.pages.add()
'设置font, brush
dim font as new pdftruetypefont(new font("arial unicode ms", 10f, fontstyle.regular), true)
dim brush as pdfbrush = pdfbrushes.black
'定义一些坐标变量并赋初值
dim x as single = 10
dim y as single = 10
dim tempx as single = 0
dim tempy as single = 0
'添加文本框
dim text as string = "文本框: "
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim textbox as new pdftextboxfield(page, "textbox")
textbox.bounds = new rectanglef(tempx, y, tempx * 2, 15)
textbox.borderwidth = 0.75f
textbox.borderstyle = pdfborderstyle.solid
pdf.form.fields.add(textbox)
'添加复选框
text = "复选框: "
y = tempy
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim checkbox as new pdfcheckboxfield(page, "checkbox")
checkbox.bounds = new rectanglef(tempx, y, 15, 15)
checkbox.borderwidth = 0.75f
checkbox.style = pdfcheckboxstyle.cross
pdf.form.fields.add(checkbox)
'添加列表框
text = "列表框: "
y = tempy
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim listbox as new pdflistboxfield(page, "listbox")
listbox.bounds = new rectanglef(tempx, y, tempx * 2, tempy * 2)
listbox.borderwidth = 0.75f
for i as integer = 0 to 2
'添加项目到列表框
dim temptext as string = string.format("text {0}", i)
dim tempvalue as string = string.format("value {0}", i)
listbox.items.add(new pdflistfielditem(temptext, tempvalue))
next
pdf.form.fields.add(listbox)
'添加单选按钮
text = "单选按钮: "
y = tempy * 2 15
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim radiobutton as new pdfradiobuttonlistfield(page, "radiobutton")
for i as integer = 0 to 2
dim item as new pdfradiobuttonlistitem(string.format("rb{0}", i))
item.borderwidth = 0.75f
item.bounds = new rectanglef(tempx i * 20, y, 15, 15)
radiobutton.items.add(item)
next
pdf.form.fields.add(radiobutton)
'添加组合框
text = "下拉列表框: "
y = tempy
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim combobox as new pdfcomboboxfield(page, "combobox")
combobox.bounds = new rectanglef(tempx, y, tempx, 15)
combobox.borderwidth = 0.75f
for i as integer = 0 to 2
'添加项目到下拉列表框
dim temptext as string = string.format("text {0}", i)
dim tempvalue as string = string.format("value {0}", i)
combobox.items.add(new pdflistfielditem(temptext, tempvalue))
next
pdf.form.fields.add(combobox)
'保存文档
pdf.savetofile("pdf表单域.pdf")
添加提示文本(tooltip)
c#
//创建pdf文档并添加一页
pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();
//设置font, brush
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 10f, fontstyle.regular), true);
pdfbrush brush = pdfbrushes.black;
//定义一些坐标变量并赋初值
float x = 10;
float y = 50;
float tempx = 0;
float tempy = 0;
//添加文本框
string text = "满意指数: ";
page.canvas.drawstring(text, font, brush, x, y);
tempx = font.measurestring(text).width 15;
tempy = font.measurestring(text).height 15;
pdftextboxfield textbox = new pdftextboxfield(page, "textbox");
textbox.bounds = new rectanglef(tempx, y, tempx * 2, 15);
textbox.borderwidth = 0.75f;
textbox.borderstyle = pdfborderstyle.solid;
//添加文本提示信息
textbox.tooltip = "请输入0-5之间的数字";
//添加文本域到fields collection并保存文档
pdf.form.fields.add(textbox);
pdf.savetofile("添加文本信息.pdf");
vb.net
'创建pdf文档并添加一页
dim pdf as new pdfdocument()
dim page as pdfpagebase = pdf.pages.add()
'设置font, brush
dim font as new pdftruetypefont(new font("arial unicode ms", 10f, fontstyle.regular), true)
dim brush as pdfbrush = pdfbrushes.black
'定义一些坐标变量并赋初值
dim x as single = 10
dim y as single = 50
dim tempx as single = 0
dim tempy as single = 0
'添加文本框
dim text as string = "满意指数: "
page.canvas.drawstring(text, font, brush, x, y)
tempx = font.measurestring(text).width 15
tempy = font.measurestring(text).height 15
dim textbox as new pdftextboxfield(page, "textbox")
textbox.bounds = new rectanglef(tempx, y, tempx * 2, 15)
textbox.borderwidth = 0.75f
textbox.borderstyle = pdfborderstyle.solid
'添加文本提示信息
textbox.tooltip = "请输入0-5之间的数字"
'添加文本域到fields collection并保存文档
pdf.form.fields.add(textbox)
pdf.savetofile("添加文本信息.pdf")