PDF管理控件Spire.PDF使用教程:创建 PDF 表单域并设置属性

C# 创建 PDF 表单域

Adobe Acrobat Form (AcroForm) 是表单域的集合,用来以交互方式从用户那里收集信息。Spire.PDF支持创建多种交互式表单域,例如:文本域、单选按钮、复选框、列表框、组合框,并在特定的表单域添加提示文本(Tooltip),方便用户输入正确信息。

创建表单域

//创建PDF文档并添加一页PdfDocument pdf = new PdfDocument();PdfPageBase page = pdf.Pages.Add();//设置font, brushPdfTrueTypeFont 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");

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年8月3日
下一篇 2019年8月3日

相关推荐

发表回复

登录后才能评论