欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在.NET平臺(tái)使用C#為PDF添加各種類型的表單域的方法

 更新時(shí)間:2025年05月03日 09:18:42   作者:Eiceblue  
在日常辦公系統(tǒng)開(kāi)發(fā)中,涉及 PDF 處理相關(guān)的開(kāi)發(fā)時(shí),生成可填寫(xiě)的 PDF 表單是一種常見(jiàn)需求,與靜態(tài) PDF 不同,帶有**表單域的文檔支持用戶直接在 PDF 內(nèi)部輸入、勾選、選擇等交互操作,本文將介紹如何使用 C# 為 PDF 添加各種類型的表單域,需要的朋友可以參考下

引言

在日常辦公系統(tǒng)開(kāi)發(fā)中,涉及 PDF 處理相關(guān)的開(kāi)發(fā)時(shí),生成可填寫(xiě)的 PDF 表單是一種常見(jiàn)需求,例如員工信息登記表、用戶注冊(cè)表、問(wèn)卷調(diào)查或協(xié)議確認(rèn)頁(yè)等。與靜態(tài) PDF 不同,帶有**表單域(Form Field)**的文檔支持用戶直接在 PDF 內(nèi)部輸入、勾選、選擇等交互操作,極大提升了表單使用體驗(yàn)。

本文將介紹如何使用 C# 為 PDF 添加各種類型的表單域,包括文本框、下拉框、復(fù)選框、單選框、列表框和按鈕,并通過(guò)完整示例演示如何將這些域組合成一個(gè)實(shí)際可用的表單頁(yè)。

本文所使用的方法需要用到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 是用于顯示可選列表的下拉框,適合性別、部門(mén)、國(guó)籍等字段。

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è)“用戶信息登記表”,整合了所有常見(jiàn)表單域類型,包括文本框、下拉框、復(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)建文檔和頁(yè)面
        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é)果

表單域類型一覽

表單域類型說(shuō)明
PdfTextBoxField文本輸入域,用戶可鍵入任意內(nèi)容
PdfCheckBoxField勾選框,可用于二選一邏輯判斷
PdfComboBoxField下拉選擇域,提供固定選項(xiàng)
PdfListBoxField多項(xiàng)列表,可啟用多選模式
PdfRadioButtonListField單選按鈕組,用戶僅能選一項(xiàng)
PdfButtonField按鈕,可設(shè)定執(zhí)行特定操作

通過(guò)以上方式,開(kāi)發(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)文章

  • C# Task取消暫停的實(shí)現(xiàn)

    C# Task取消暫停的實(shí)現(xiàn)

    本文主要介紹了C# Task取消暫停的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • c#處理3種json數(shù)據(jù)的實(shí)例

    c#處理3種json數(shù)據(jù)的實(shí)例

    這篇文章主要介紹了c#處理包含數(shù)組、對(duì)象的復(fù)雜json數(shù)據(jù)的方法,,需要的朋友可以參考下
    2014-03-03
  • WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決

    WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決

    本文主要介紹了WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • c#中判斷字符串是不是數(shù)字或字母的方法

    c#中判斷字符串是不是數(shù)字或字母的方法

    這篇文章介紹了C#判斷字符串是否數(shù)字或字母的實(shí)例,有需要的朋友可以參考一下
    2013-06-06
  • C# 格式化JSON的兩種實(shí)現(xiàn)方式

    C# 格式化JSON的兩種實(shí)現(xiàn)方式

    本文主要介紹了C# 格式化JSON的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C#使用TCP協(xié)議實(shí)現(xiàn)數(shù)據(jù)發(fā)送和接受的方法

    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è)部分,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • C#帶你玩掃雷(附源碼)

    C#帶你玩掃雷(附源碼)

    這篇文章主要介紹了C#帶你玩掃雷(附源碼),詳細(xì)的介紹實(shí)現(xiàn)掃雷的方法,具體一定的參考價(jià)值,有興趣的可以了解一下
    2017-10-10
  • Unity實(shí)現(xiàn)物體左右移動(dòng)效果

    Unity實(shí)現(xiàn)物體左右移動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體左右移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 基于C# 中可以new一個(gè)接口?的問(wèn)題分析

    基于C# 中可以new一個(gè)接口?的問(wèn)題分析

    本篇文章是對(duì)C#中可以new一個(gè)接口?的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#實(shí)現(xiàn)給Word每一頁(yè)設(shè)置不同文字水印的方法詳解

    C#實(shí)現(xiàn)給Word每一頁(yè)設(shè)置不同文字水印的方法詳解

    Word中設(shè)置水印時(shí),可使用預(yù)設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時(shí),會(huì)對(duì)所有頁(yè)面都設(shè)置成統(tǒng)一效果。本文以C#?代碼為例,對(duì)Word每一頁(yè)設(shè)置不同的文字水印效果作詳細(xì)介紹,感興趣的可以了解一下
    2022-07-07

最新評(píng)論