如何填寫和編輯 PDF 表單

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

恰克尼思

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

開始使用 IronPDF

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

第一步:
green arrow pointer


編輯表單

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

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

輸出 PDF 文件

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

Chaknith related to 輸出 PDF 文件

查克尼思·賓

軟體工程師

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