原創(chuàng)|使用教程|編輯:李顯亮|2019-09-05 11:22:18.143|閱讀 1038 次
概述:Adobe Acrobat Form (AcroForm) 是表單域的集合,用來以交互方式從用戶那里收集信息。Spire.PDF支持創(chuàng)建多種交互式表單域,本文將介紹如何創(chuàng)建表單域并設(shè)置屬性。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.PDF是一個(gè)專業(yè)的PDF組件,能夠獨(dú)立地創(chuàng)建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設(shè)置(包括數(shù)字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數(shù)據(jù)更新、章節(jié)和段落優(yōu)化、圖形/圖像描繪和插入、表格創(chuàng)建和處理、數(shù)據(jù)導(dǎo)入等等。>>下載Spire.PDF最新試用版
Adobe Acrobat Form (AcroForm) 是表單域的集合,用來以交互方式從用戶那里收集信息。Spire.PDF支持創(chuàng)建多種交互式表單域,例如:文本域、單選按鈕、復(fù)選框、列表框、組合框,并在特定的表單域添加提示文本(Tooltip),方便用戶輸入正確信息。
創(chuàng)建表單域
//創(chuàng)建PDF文檔并添加一頁
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//設(shè)置font, brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f,FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
//定義一些坐標(biāo)變量并賦初值
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);
//添加復(fù)選框
text = "復(fù)選框: ";
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++)
{
//添加項(xiàng)目到列表框
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++)
{
//添加項(xiàng)目到下拉列表框
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");
添加提示文本(Tooltip)
//創(chuàng)建PDF文檔并添加一頁
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//設(shè)置font, brush
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
//定義一些坐標(biāo)變量并賦初值
float x = 10;
float y = 50;
float tempX = 0;
float tempY = 0;
//添加文本框
string text = "滿意指數(shù): ";
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 = "請(qǐng)輸入0-5之間的數(shù)字";
//添加文本域到fields collection并保存文檔
pdf.Form.Fields.Add(textbox);
pdf.SaveToFile("添加文本信息.pdf");
當(dāng)我們把PDF表單域填寫完成后,可以將這些域設(shè)置為只讀來阻止用戶修改或刪除表單域的內(nèi)容。Spire.PDF組件支持以下兩種方式將PDF表單域設(shè)置為只讀:
將表單域扁平化
可以使用PdfForm類的IsFlatten屬性來將PDF文檔中的所有表單域扁平化。代碼示例如下:
//加載PDF文檔
PdfDocument document = new PdfDocument();
document.LoadFromFile("Form.pdf");
//獲取文檔中的現(xiàn)有表單域
PdfForm loadedForm = document.Form;
//扁平化所有表單域
loadedForm.IsFlatten = true;
//保存文檔
document.SaveToFile("Flatten1.pdf");此外,我們還可以通過PdfField類的Flatten屬性來扁平化指定表單域:
//加載PDF文檔
PdfDocument document = new PdfDocument();
document.LoadFromFile("Form.pdf");
//獲取文檔中的現(xiàn)有表單域
PdfFormWidget form = document.Form as PdfFormWidget;
//扁平化指定表單域
PdfField field = form.FieldsWidget.List[0] as PdfField;
field.Flatten = true;
//保存文檔
document.SaveToFile("Flatten2.pdf");將表單域設(shè)置為只讀
將PDF文檔中的所有表單域設(shè)置為只讀,我們可以使用PdfForm類的ReadOnly屬性:
//加載PDF文檔
PdfDocument document = new PdfDocument();
document.LoadFromFile("Form.pdf");
//獲取文檔中的現(xiàn)有表單域
PdfForm loadedForm = document.Form;
//將所有表單域設(shè)置為只讀
loadedForm.ReadOnly = true;
//保存文檔
document.SaveToFile("ReadOnly1.pdf");將PDF文檔中的指定表單域設(shè)置為只讀,我們可以使用PdfField類的ReadOnly屬性:
//加載PDF文檔
PdfDocument document = new PdfDocument();
document.LoadFromFile("Form.pdf");
//獲取文檔中的現(xiàn)有表單域
PdfFormWidget form = document.Form as PdfFormWidget;
//將指定表單域設(shè)置為只讀
PdfField field = form.FieldsWidget.List[0] as PdfField;
field.ReadOnly = true;
//保存文檔
document.SaveToFile("ReadOnly2.pdf");如果你有任何問題或意見,可在下方評(píng)論區(qū)留言,點(diǎn)擊資源列表查看更多教程資源~
*想要購買正版授權(quán)的朋友可以哦~
掃描關(guān)注“慧聚IT”微信公眾號(hào),及時(shí)獲取更多產(chǎn)品最新動(dòng)態(tài)及最新資訊

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn