C# code tutorial

PDF Processing & Editing PDF Creating & Inserting PDF to Image PDF Digital Signature PDF Extracting PDF Protecting PDF Chart PDF Barcode

Popular .NET SDK


It is a very convenient way to distribute text and data using document in a PDF form. However, you may discover that editing PDF is a lot more trouble than editing a Microsoft Word document. That's because the PDF format was never designed to be edited at all, especially for its form fields.

Editing PDF form fields can be a serious pain if you don't have the right tools. Here's what you need, XsPDF PDF Basic Library for .NET. It allows you to easily get the value of PDF form field and do more manipulations, like filling, updating, and deleting. Almost all daily use AcroForm types, including TextField, SignatureField, ButtonField, ListBoxField, CheckBoxField, ChoiceField, PushButtonField, RadioButtonField, and ComboBoxField.

On this guide page, we will illustrate how to read and get PDF form fields value and do more further editing. Please directly see the following C# sample codes.

Read the Value of PDF Form Fields in C#

Here, we take TextField and CheckBoxField as example to show you how to get and read the value of PDF form fields.

PdfDocument document = new PdfDocument("sample.pdf");

// Get Form fields in pdf document
var form = document.AcroForm;
var fields = form.Fields;

// Get all the fields names
var names = new List<string>();
names.AddRange(fields.Names);
names.AddRange(fields.DescendantNames);

for (int i = 0; i < names.Count; i++)
{
    // Obtain field by name
    string fName = names[i];
    var field = fields[fName];

    // Show value in fields
    PdfTextField txtField;
    PdfCheckBoxField chkField;

    if ((txtField = field as PdfTextField) != null)
    {
        Console.WriteLine(String.Format("text field '{0}': '{1}'", txtField.Name, txtField.Text));
    }
    else if ((chkField = field as PdfCheckBoxField) != null)
    {
        Console.WriteLine(String.Format("checkbox '{0}': '{1}'", chkField.Name, chkField.Checked));
    }
}
Fill and Update the Value of PDF Form Fields in C#

Please refer to the following C# sample code to see how to fill and update the value of PDF form fields. Please note, you should set "EnableUpdate" to true before you populate PDF form fields if you do not want PDF to "hide" the values on the form while opening in a PDF viewer/reader.

PdfDocument document = new PdfDocument("sample.pdf");

// If you are attempting to populate PDF form fields, you need to set the EnableUpdate to true.
// Otherwise the PDF will "hide" the values on the form.
document.AcroForm.EnableUpdate = true;

// Update the value of text field
PdfTextField txtField = (PdfTextField)(document.AcroForm.Fields[document.AcroForm.Fields.Names[0]]);
txtField.Value = new PdfString("update by xspdf");

// Update the value of checkbox field
PdfCheckBoxField chkField = (PdfCheckBoxField)(document.AcroForm.Fields[document.AcroForm.Fields.DescendantNames[2]]);
chkField.Checked = true;

document.Save("form-fill.pdf");

More Excel tutorial