在.NET平臺(tái)使用C#為PDF添加各種類型的表單域的方法
引言
在日常辦公系統(tǒng)開發(fā)中,涉及 PDF 處理相關(guān)的開發(fā)時(shí),生成可填寫的 PDF 表單是一種常見需求,例如員工信息登記表、用戶注冊(cè)表、問卷調(diào)查或協(xié)議確認(rèn)頁等。與靜態(tài) PDF 不同,帶有**表單域(Form Field)**的文檔支持用戶直接在 PDF 內(nèi)部輸入、勾選、選擇等交互操作,極大提升了表單使用體驗(yàn)。
本文將介紹如何使用 C# 為 PDF 添加各種類型的表單域,包括文本框、下拉框、復(fù)選框、單選框、列表框和按鈕,并通過完整示例演示如何將這些域組合成一個(gè)實(shí)際可用的表單頁。
本文所使用的方法需要用到Free Spire.PDF for .NET,NuGet安裝:PM> Install-Package FreeSpire.PDF。
使用 PdfTextBoxField 添加文本輸入域
PdfTextBoxField 表示文本輸入域,適用于姓名、地址、日期等自由輸入內(nèi)容。
PdfTextBoxField textBox = new PdfTextBoxField(page, "textBox"); textBox.Bounds = new RectangleF(100, 50, 150, 20); textBox.Text = "Enter your name"; textBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f); doc.Form.Fields.Add(textBox);
使用 PdfComboBoxField 添加下拉選擇域
PdfComboBoxField 是用于顯示可選列表的下拉框,適合性別、部門、國籍等字段。
PdfComboBoxField comboBox = new PdfComboBoxField(page, "comboBox");
comboBox.Bounds = new RectangleF(100, 110, 150, 20);
comboBox.Items.Add(new PdfListFieldItem("Option A", "A"));
comboBox.Items.Add(new PdfListFieldItem("Option B", "B"));
comboBox.Items.Add(new PdfListFieldItem("Option C", "C"));
comboBox.SelectedIndex = 0;
comboBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f);
doc.Form.Fields.Add(comboBox);
使用 PdfCheckBoxField 添加復(fù)選框域
PdfCheckBoxField 表示復(fù)選框,適用于“是否同意”、“是否接收通知”等二元布爾選項(xiàng)。
PdfCheckBoxField checkBox = new PdfCheckBoxField(page, "checkBox"); checkBox.Bounds = new RectangleF(100, 80, 15, 15); checkBox.Checked = false; doc.Form.Fields.Add(checkBox);
綜合示例:包含所有類型表單域的 PDF 表單
以下代碼創(chuàng)建了一個(gè)“用戶信息登記表”,整合了所有常見表單域類型,包括文本框、下拉框、復(fù)選框、列表框、單選按鈕和按鈕。
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;
class Program
{
static void Main(string[] args)
{
// 創(chuàng)建文檔和頁面
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
// 坐標(biāo)和樣式初始化
float baseX = 100;
float baseY = 30;
PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush labelBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black));
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
// 文本框
page.Canvas.DrawString("TextBox:", font, titleBrush, new PointF(10, baseY));
RectangleF textBoxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
textBox.Bounds = textBoxBounds;
textBox.Text = "Hello World";
textBox.Font = font;
doc.Form.Fields.Add(textBox);
baseY += 25;
// 復(fù)選框
page.Canvas.DrawString("CheckBox:", font, titleBrush, new PointF(10, baseY));
RectangleF checkBox1Bounds = new RectangleF(baseX, baseY, 15, 15);
PdfCheckBoxField checkBox1 = new PdfCheckBoxField(page, "checkbox1");
checkBox1.Bounds = checkBox1Bounds;
checkBox1.Checked = false;
page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));
RectangleF checkBox2Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
PdfCheckBoxField checkBox2 = new PdfCheckBoxField(page, "checkbox2");
checkBox2.Bounds = checkBox2Bounds;
checkBox2.Checked = false;
page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));
doc.Form.Fields.Add(checkBox1);
doc.Form.Fields.Add(checkBox2);
baseY += 25;
// 下拉列表框
page.Canvas.DrawString("ComboBox:", font, titleBrush, new PointF(10, baseY));
RectangleF comboBoxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfComboBoxField comboBox = new PdfComboBoxField(page, "combobox");
comboBox.Bounds = comboBoxBounds;
comboBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
comboBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
comboBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
comboBox.SelectedIndex = 0;
comboBox.Font = font;
doc.Form.Fields.Add(comboBox);
baseY += 25;
// 列表框
page.Canvas.DrawString("ListBox:", font, titleBrush, new PointF(10, baseY));
RectangleF listBoxBounds = new RectangleF(baseX, baseY, 150, 50);
PdfListBoxField listBox = new PdfListBoxField(page, "listbox");
listBox.Bounds = listBoxBounds;
listBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
listBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
listBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
listBox.SelectedIndex = 0;
listBox.Font = font;
doc.Form.Fields.Add(listBox);
baseY += 60;
// 單選按鈕
page.Canvas.DrawString("RadioButton:", font, titleBrush, new PointF(10, baseY));
PdfRadioButtonListField radioGroup = new PdfRadioButtonListField(page, "radioGroup");
PdfRadioButtonListItem radio1 = new PdfRadioButtonListItem("Option1");
radio1.Bounds = new RectangleF(baseX, baseY, 15, 15);
page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));
PdfRadioButtonListItem radio2 = new PdfRadioButtonListItem("Option2");
radio2.Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));
radioGroup.Items.Add(radio1);
radioGroup.Items.Add(radio2);
radioGroup.SelectedIndex = 0;
doc.Form.Fields.Add(radioGroup);
baseY += 25;
// 簽名域
page.Canvas.DrawString("Signature Field:", font, titleBrush, new PointF(10, baseY));
RectangleF signatureBounds = new RectangleF(baseX, baseY, 150, 80);
PdfSignatureField signatureField = new PdfSignatureField(page, "signatureField");
signatureField.Bounds = signatureBounds;
doc.Form.Fields.Add(signatureField);
baseY += 90;
// 按鈕
page.Canvas.DrawString("Button:", font, titleBrush, new PointF(10, baseY));
RectangleF buttonBounds = new RectangleF(baseX, baseY, 50, 15);
PdfButtonField button = new PdfButtonField(page, "submitButton");
button.Bounds = buttonBounds;
button.Text = "Submit";
button.Font = font;
PdfSubmitAction submitAction = new PdfSubmitAction("https://www.google.com/");
submitAction.DataFormat = SubmitDataFormat.Html;
button.Actions.MouseDown = submitAction;
doc.Form.Fields.Add(button);
// 保存文檔
doc.SaveToFile("FillableForm.pdf", FileFormat.PDF);
doc.Close();
}
}
創(chuàng)建結(jié)果

表單域類型一覽
| 表單域類型 | 說明 |
|---|---|
PdfTextBoxField | 文本輸入域,用戶可鍵入任意內(nèi)容 |
PdfCheckBoxField | 勾選框,可用于二選一邏輯判斷 |
PdfComboBoxField | 下拉選擇域,提供固定選項(xiàng) |
PdfListBoxField | 多項(xiàng)列表,可啟用多選模式 |
PdfRadioButtonListField | 單選按鈕組,用戶僅能選一項(xiàng) |
PdfButtonField | 按鈕,可設(shè)定執(zhí)行特定操作 |
通過以上方式,開發(fā)者可以快速構(gòu)建結(jié)構(gòu)清晰、功能完備的 PDF 表單,實(shí)現(xiàn)用戶信息采集、文檔自動(dòng)化交互等多種應(yīng)用場(chǎng)景。
到此這篇關(guān)于在.NET平臺(tái)使用C#為PDF添加各種類型的表單域的方法的文章就介紹到這了,更多相關(guān)C#為PDF添加表單域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問題解決
本文主要介紹了WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
C#使用TCP協(xié)議實(shí)現(xiàn)數(shù)據(jù)發(fā)送和接受的方法
這篇文章主要介紹了c#使用TCP協(xié)議實(shí)現(xiàn)數(shù)據(jù)發(fā)送和接受,使用TCP協(xié)議實(shí)現(xiàn)數(shù)據(jù)的發(fā)送和接受包括客戶端和服務(wù)端兩個(gè)部分,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
Unity實(shí)現(xiàn)物體左右移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體左右移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
C#實(shí)現(xiàn)給Word每一頁設(shè)置不同文字水印的方法詳解
Word中設(shè)置水印時(shí),可使用預(yù)設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時(shí),會(huì)對(duì)所有頁面都設(shè)置成統(tǒng)一效果。本文以C#?代碼為例,對(duì)Word每一頁設(shè)置不同的文字水印效果作詳細(xì)介紹,感興趣的可以了解一下2022-07-07

