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("宋体", 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("宋体", 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("宋体", 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("宋体", 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")