Interacting with PDF Form Fields using C# and .NET

An interactive form in Pdf.Net is represented by a PdfInteractiveForm class and can be accessed via the Form property of the SautinSoft.Pdf class. The base class of all PDF fields is a PdfField and can be accessed via the Fields property of the PdfInteractiveForm class.

PDF forms are a powerful way to collect and manage data. Using PDF.Net from the SautinSoft library, developers can create, read, fill out PDF forms and manipulate them programmatically using C# and .NET. This article will guide you through the essential tasks involved in working with PDF form data.

The example below shows how all PDF interactive form fields in a PDF document can be read for their type, name, value, and other properties. To read data from a PDF form, you can use the following code snippet:

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF Document.
  3. Group fields by name.
  4. Write the field type, field name, and field value just once for a field group.
  5. Output in the console.

Input file:

Output result:

Полный код

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using SautinSoft.Pdf.Forms;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Read PDF interactive form fields.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/read-interactive-form.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string pdfFile = Path.GetFullPath(@"..\..\..\FormFilled.pdf");

            var writer = new StringWriter(CultureInfo.InvariantCulture);
            string format = "{0,-16}|{1,20}|{2,-20}|{3,-20}|{4,-20}", separator = new string('-', 100);

            // Write the header.
            writer.WriteLine("Document contains the following form fields:");
            writer.WriteLine();
            writer.WriteLine(format,
                "Type",
                '"' + "Name" + '"',
                "Value",
                "ExportValue/Choice",
                "Checked/Selected");
            writer.WriteLine(separator);

            PdfFieldType? fieldType;
            string fieldName, fieldExportValueOrChoice;
            object fieldValue;
            bool? fieldCheckedOrSelected;

            using (var document = PdfDocument.Load(pdfFile))
                // Group fields by name because all fields with the same name are actually different representations (widget annotations) of the same field.
                // Radio button fields are usually grouped. Other field types are rarely grouped.
                foreach (var fieldGroup in document.Form.Fields.GroupBy(field => field.Name))
                {
                    var field = fieldGroup.First();

                    fieldType = field.FieldType;
                    fieldName = '"' + field.Name + '"';
                    fieldValue = field.Value;

                    foreach (var widgetField in fieldGroup)
                    {
                        switch (widgetField.FieldType)
                        {
                            case PdfFieldType.CheckBox:
                            case PdfFieldType.RadioButton:
                                // Check box and radio button are toggle button fields.
                                var toggleField = (PdfToggleButtonField)widgetField;

                                fieldExportValueOrChoice = toggleField.FieldType == PdfFieldType.CheckBox ?
                                        ((PdfCheckBoxField)toggleField).ExportValue :
                                        ((PdfRadioButtonField)toggleField).Choice;
                                fieldCheckedOrSelected = toggleField.Checked;

                                writer.WriteLine(format,
                                    fieldType,
                                    fieldName,
                                    fieldValue,
                                    fieldExportValueOrChoice,
                                    fieldCheckedOrSelected);
                                break;

                            case PdfFieldType.ListBox:
                            case PdfFieldType.Dropdown:
                                // List box and drop-down are choice fields.
                                var choiceField = (PdfChoiceField)widgetField;

                                // List box can have multiple values if multiple selection is enabled.
                                if (fieldValue is string[] fieldValues)
                                    fieldValue = string.Join(", ", fieldValues);

                                for (int itemIndex = 0; itemIndex < choiceField.Items.Count; ++itemIndex)
                                {
                                    fieldExportValueOrChoice = choiceField.Items[itemIndex].ExportValue ?? choiceField.Items[itemIndex].Value;
                                    fieldCheckedOrSelected = choiceField.FieldType == PdfFieldType.ListBox ?
                                            ((PdfListBoxField)choiceField).SelectedIndices.Contains(itemIndex) :
                                            ((PdfDropdownField)choiceField).SelectedIndex == itemIndex;

                                    writer.WriteLine(format,
                                    fieldType,
                                    fieldName,
                                    fieldValue,
                                    fieldExportValueOrChoice,
                                    fieldCheckedOrSelected);

                                    // Write the field type, field name, and field value just once for a field group.
                                    fieldType = null;
                                    fieldName = null;
                                    fieldValue = null;
                                }
                                break;

                            default:
                                // Text field may contain multiple lines of text, if enabled.
                                if (widgetField.FieldType == PdfFieldType.Text && ((PdfTextField)widgetField).MultiLine && fieldValue != null)
                                    fieldValue = ((string)fieldValue).Replace("\r", "\\r");

                                fieldExportValueOrChoice = null;
                                fieldCheckedOrSelected = null;

                                writer.WriteLine(format,
                                    fieldType,
                                    fieldName,
                                    fieldValue,
                                    fieldExportValueOrChoice,
                                    fieldCheckedOrSelected);
                                break;
                        }

                        // Write the field type, field name, and field value just once for a field group.
                        fieldType = null;
                        fieldName = null;
                        fieldValue = null;
                    }

                    writer.WriteLine(separator);
                }

            Console.Write(writer.ToString());
        }
    }
    }

Download

Option Infer On

Imports Microsoft.VisualBasic
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Imports SautinSoft.Pdf.Forms

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Read PDF interactive form fields.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/read-interactive-form.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free 100-day trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			Dim pdfFile As String = Path.GetFullPath("..\..\..\FormFilled.pdf")

			Dim writer = New StringWriter(CultureInfo.InvariantCulture)
			Dim format As String = "{0,-16}|{1,20}|{2,-20}|{3,-20}|{4,-20}", separator As New String("-"c, 100)

			' Write the header.
			writer.WriteLine("Document contains the following form fields:")
			writer.WriteLine()
			writer.WriteLine(format, "Type", """"c & "Name" & """"c, "Value", "ExportValue/Choice", "Checked/Selected")
			writer.WriteLine(separator)

			Dim fieldType? As PdfFieldType
			Dim fieldName, fieldExportValueOrChoice As String
			Dim fieldValue As Object
			Dim fieldCheckedOrSelected? As Boolean

			Using document = PdfDocument.Load(pdfFile)
				' Group fields by name because all fields with the same name are actually different representations (widget annotations) of the same field.
				' Radio button fields are usually grouped. Other field types are rarely grouped.
				For Each fieldGroup In document.Form.Fields.GroupBy(Function(field) field.Name)
					Dim field = fieldGroup.First()

					fieldType = field.FieldType
					fieldName = """"c & field.Name & """"c
					fieldValue = field.Value

					For Each widgetField In fieldGroup
						Select Case widgetField.FieldType
							Case PdfFieldType.CheckBox, PdfFieldType.RadioButton
								' Check box and radio button are toggle button fields.
								Dim toggleField = CType(widgetField, PdfToggleButtonField)

								fieldExportValueOrChoice = If(toggleField.FieldType = PdfFieldType.CheckBox, CType(toggleField, PdfCheckBoxField).ExportValue, CType(toggleField, PdfRadioButtonField).Choice)
								fieldCheckedOrSelected = toggleField.Checked

								writer.WriteLine(format, fieldType, fieldName, fieldValue, fieldExportValueOrChoice, fieldCheckedOrSelected)

							Case PdfFieldType.ListBox, PdfFieldType.Dropdown
								' List box and drop-down are choice fields.
								Dim choiceField = CType(widgetField, PdfChoiceField)

								' List box can have multiple values if multiple selection is enabled.
								Dim tempVar As Boolean = TypeOf fieldValue Is String()
								Dim fieldValues() As String = If(tempVar, DirectCast(fieldValue, String()), Nothing)
								If tempVar Then
									fieldValue = String.Join(", ", fieldValues)
								End If

								For itemIndex As Integer = 0 To choiceField.Items.Count - 1
									fieldExportValueOrChoice = If(choiceField.Items(itemIndex).ExportValue, choiceField.Items(itemIndex).Value)
									fieldCheckedOrSelected = If(choiceField.FieldType = PdfFieldType.ListBox, CType(choiceField, PdfListBoxField).SelectedIndices.Contains(itemIndex), CType(choiceField, PdfDropdownField).SelectedIndex = itemIndex)

									writer.WriteLine(format, fieldType, fieldName, fieldValue, fieldExportValueOrChoice, fieldCheckedOrSelected)

									' Write the field type, field name, and field value just once for a field group.
									fieldType = Nothing
									fieldName = Nothing
									fieldValue = Nothing
								Next itemIndex

							Case Else
								' Text field may contain multiple lines of text, if enabled.
								If widgetField.FieldType = PdfFieldType.Text AndAlso CType(widgetField, PdfTextField).MultiLine AndAlso fieldValue IsNot Nothing Then
									fieldValue = DirectCast(fieldValue, String).Replace(vbCr, "\r")
								End If

								fieldExportValueOrChoice = Nothing
								fieldCheckedOrSelected = Nothing

								writer.WriteLine(format, fieldType, fieldName, fieldValue, fieldExportValueOrChoice, fieldCheckedOrSelected)
						End Select

						' Write the field type, field name, and field value just once for a field group.
						fieldType = Nothing
						fieldName = Nothing
						fieldValue = Nothing
					Next widgetField

					writer.WriteLine(separator)
				Next fieldGroup
			End Using

			Console.Write(writer.ToString())
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.