如何填寫和編輯 PDF 表單
IronPDF提供直觀的工具集來編輯PDF文件中的現有表單,包括文字區域、文字輸入、複選框、下拉選單和選擇按鈕。
開始使用 IronPDF
立即在您的專案中使用IronPDF,並享受免費試用。
如何填寫和編輯 PDF 表單
- 下載用於填寫和編輯 PDF 表單的 C# 庫
- 使用
FromFile
方法匯入目標 PDF 文件 - 使用
FindFormField
方法查找要修改的表单对象 - 修改Value屬性以設置所需的信息
- 匯出編輯後的 PDF 文件
編輯表單
IronPDF 輕鬆編輯 PDF 文件中各種類型的現有表單字段。
文字區域和輸入表單
要編輯文本區域和輸入表單,將 Value 屬性指定為所需的信息。 下面的代碼首先使用表單名稱的FindFormField
方法查找表單對象。 然後,它訪問並賦值該物件的Value屬性。
:path=/static-assets/pdf/content-code-examples/how-to/edit-forms-input-textarea.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");
// Set text input form values
pdf.Form.FindFormField("firstname").Value = "John";
pdf.Form.FindFormField("lastname").Value = "Smith";
// Set text area form values
pdf.Form.FindFormField("address").Value = "Iron Software LLC\r\n205 N. Michigan Ave.";
pdf.SaveAs("textAreaAndInputFormEdited.pdf");
輸出 PDF 文件
複選框和下拉式選單表單
首先通過其名稱查找表單字段,然後編輯現有的復選框和下拉框表單。 將 Value 屬性賦值為「Yes」以勾選複選框表單。 通過將所需的選擇指派給其Value屬性來選擇下拉式選單中的任何可用選項。 為了方便起見,通過訪問Choices屬性來檢索所有選擇的值。
:path=/static-assets/pdf/content-code-examples/how-to/edit-forms-checkbox-combobox.cs
using IronPdf;
using System;
PdfDocument pdf = PdfDocument.FromFile("checkboxAndComboboxForm.pdf");
var checkboxForm = pdf.Form.FindFormField("taskCompleted");
// Check the checkbox form
checkboxForm.Value = "Yes";
var comboboxForm = pdf.Form.FindFormField("priority");
// Set the combobox value
comboboxForm.Value = "Low";
// Print out all the available choices
foreach (var choice in comboboxForm.Choices)
{
Console.WriteLine(choice);
}
pdf.SaveAs("checkboxAndComboboxFormEdited.pdf");
輸出 PDF 文件
单选按钮表单
在 IronPDF 中處理單選按鈕表單時,同一組的單選按鈕被包含在一個表單對象中。 要編輯單選按鈕的值,只需將表單物件的Value屬性分配給其中一個可用選項。 使用Annotations屬性檢索所有可用選項。 以下程式碼展示了如何編輯單選按鈕的值。
:path=/static-assets/pdf/content-code-examples/how-to/edit-forms-radiobutton.cs
using IronPdf;
using System;
PdfDocument pdf = PdfDocument.FromFile("radioButtomForm.pdf");
var radioForm = pdf.Form.FindFormField("traveltype");
// Set the radio button value
radioForm.Value = "Airplane";
// Print out all the available choices
foreach(var annotation in radioForm.Annotations)
{
Console.WriteLine(annotation.OnAppearance);
}
pdf.SaveAs("radioButtomFormEdited.pdf");
此外,使用Clear
方法取消選取單選按鈕。 此方法只能在物件為RadioFormField類型時才能存取。 訪問 PDF 中的單選框表單物件後,可以將其轉換為 RadioFormField 類型。
輸出 PDF 文件
移除表單
要從 PDF 中移除表單,首先使用 FindFormField
方法選擇目標表單。 將表單物件傳遞給 Form.Remove
方法,該方法可從 PdfDocument 物件訪問。 讓我們在textAreaAndInputForm.pdf檔案上嘗試這個方法。
:path=/static-assets/pdf/content-code-examples/how-to/edit-forms-remove-form.cs
using IronPdf;
using IronSoftware.Forms;
PdfDocument pdf = PdfDocument.FromFile("textAreaAndInputForm.pdf");
// Remove Form
IFormField targetForm = pdf.Form.FindFormField("firstname");
pdf.Form.Remove(targetForm);
pdf.SaveAs("removedForm.pdf");
輸出 PDF 文件
了解如何透過程式設計方式建立 PDF 表單,詳情請參閱以下文章:"如何建立 PDF 表單"。