如何填寫和編輯 PDF 表單
IronPDF提供直觀的工具集來編輯PDF文件中的現有表單,包括文字區域、文字輸入、複選框、下拉選單和選擇按鈕。
開始使用 IronPDF
立即在您的專案中使用IronPDF,並享受免費試用。
如何填寫和編輯 PDF 表單
- 下載用於填寫和編輯 PDF 表單的 C# 程式庫
- 使用以下方式匯入目標 PDF 文件
從文件
方法 - 找到要修改的表單對象,使用
查找表單欄位
方法 - 修改 價值 設定所需訊息的屬性
- 匯出編輯後的 PDF 文件
編輯表單
IronPDF 輕鬆編輯 PDF 文件中各種類型的現有表單字段。
文字區域和輸入表單
要編輯文本區域和輸入表單,請將值屬性指定給所需的資訊。 以下程式碼首先使用表單名稱,通過 FindFormField
方法找到表單對象。 然後,它訪問並分配對象的值屬性。
: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");
Imports Microsoft.VisualBasic
Imports IronPdf
Private pdf As PdfDocument = 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" & vbCrLf & "205 N. Michigan Ave."
pdf.SaveAs("textAreaAndInputFormEdited.pdf")
輸出 PDF 文件
複選框和下拉式選單表單
首先通過其名稱查找表單字段,然後編輯現有的復選框和下拉框表單。 將Value屬性設為 'Yes' 以勾選核取方塊表單。 將所需選項分配給其值屬性,以在組合框中選擇任何可用選項。 為了方便起見,通過訪問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");
Imports IronPdf
Imports System
Private pdf As PdfDocument = PdfDocument.FromFile("checkboxAndComboboxForm.pdf")
Private checkboxForm = pdf.Form.FindFormField("taskCompleted")
' Check the checkbox form
checkboxForm.Value = "Yes"
Dim comboboxForm = pdf.Form.FindFormField("priority")
' Set the combobox value
comboboxForm.Value = "Low"
' Print out all the available choices
For Each choice In comboboxForm.Choices
Console.WriteLine(choice)
Next 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");
Imports IronPdf
Imports System
Private pdf As PdfDocument = PdfDocument.FromFile("radioButtomForm.pdf")
Private radioForm = pdf.Form.FindFormField("traveltype")
' Set the radio button value
radioForm.Value = "Airplane"
' Print out all the available choices
For Each annotation In radioForm.Annotations
Console.WriteLine(annotation.OnAppearance)
Next annotation
pdf.SaveAs("radioButtomFormEdited.pdf")
此外,使用 Clear
方法來取消選擇單選按鈕。 此方法僅可在對象類型為RadioFormField時訪問。 從 PDF 存取 radio 表單物件後,可以將其轉換為 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");
Imports IronPdf
Imports IronSoftware.Forms
Private pdf As PdfDocument = PdfDocument.FromFile("textAreaAndInputForm.pdf")
' Remove Form
Private targetForm As IFormField = pdf.Form.FindFormField("firstname")
pdf.Form.Remove(targetForm)
pdf.SaveAs("removedForm.pdf")
輸出 PDF 文件
了解如何在以下文章中以程式化方式建立 PDF 表單:如何建立 PDF 表單.