如何填寫和編輯 PDF 表單

This article was translated from English: Does it need improvement?
Translated
View the article in English

恰克尼思

IronPDF提供直觀的工具集來編輯PDF文件中的現有表單,包括文字區域、文字輸入、複選框、下拉選單和選擇按鈕。

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

編輯表單

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");
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")
VB   C#

輸出 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");
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")
VB   C#

輸出 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")
VB   C#

此外,使用 Clear 方法取消選擇單選按鈕。該方法僅在對象類型為 RadioFormField 時才可訪問。從 PDF 中訪問單選表單對象後,可以將其轉換為 RadioFormField 類型。

輸出 PDF 文件

了解如何在以下文章中以程式化方式建立 PDF 表單:如何建立 PDF 表單.

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。