The Interaction Form in PDF using C# and .NET

Interactive forms in PDF, also known as AcroForms, allow users to enter data directly into the document. This is particularly useful for creating surveys, questionnaires, and other documents that require user input. In this article, we will explore how to fill in interactive PDF forms using C# and .NET with the help of the SautinSoft PDF.Net library.

Filling out forms it is designed to automate the process of entering data into interactive PDF documents. This is especially useful in the following cases:

  • Automation of business processes: Filling out forms can be automated to speed up the processing of documents such as questionnaires, surveys, applications and contracts.
  • Data Collection: It is easy to collect and process data from users, which simplifies the analysis and storage of information.
  • Improved user experience: Users can fill out forms electronically, which is more convenient and faster than manually filling out paper forms.
  • Error reduction: Automatic form filling reduces the likelihood of errors related to manual data entry.
  • Integration with other systems: Completed forms can be easily integrated with other systems and databases for further processing and analysis.
  • Data unification: Ensures uniformity and standardization of data, which is important for reporting and analysis.

Step-by-step Guide:

  1. Add SautinSoft.PDF from NuGet.
  2. Load the PDF document with forms.
  3. Read the form fields.
  4. Fill the form fields.
  5. Save the document.

Output result:

Полный код

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Fill in PDF interactive forms.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/fill-in-pdf-interactive-forms.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(@"..\..\..\Form.pdf");

            //Load PDF Document with forms.
            using (var document = PdfDocument.Load(pdfFile))
            {
                // Fill the form fields.
                document.Form.Fields["FullName"].Value = "Jane Doe";
                document.Form.Fields["ID"].Value = "0123456789";
                document.Form.Fields["Gender"].Value = "Female";
                document.Form.Fields["Married"].Value = "Yes";
                document.Form.Fields["City"].Value = "Berlin";
                document.Form.Fields["Language"].Value = new string[] { "German", "Italian" };
                document.Form.Fields["Notes"].Value = "Notes first line\rNotes second line\rNotes third line";

                // Save PDF Document.
                document.Save("FormFilled.pdf");
            }

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("FormFilled.pdf") { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Fill in PDF interactive forms.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/fill-in-pdf-interactive-forms.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("..\..\..\Form.pdf")

			'Load PDF Document with forms.
			Using document = PdfDocument.Load(pdfFile)
				' Fill the form fields.
				document.Form.Fields("FullName").Value = "Jane Doe"
				document.Form.Fields("ID").Value = "0123456789"
				document.Form.Fields("Gender").Value = "Female"
				document.Form.Fields("Married").Value = "Yes"
				document.Form.Fields("City").Value = "Berlin"
				document.Form.Fields("Language").Value = New String() { "German", "Italian" }
				document.Form.Fields("Notes").Value = "Notes first line" & vbCr & "Notes second line" & vbCr & "Notes third line"

				' Save PDF Document.
				document.Save("FormFilled.pdf")
			End Using

			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("FormFilled.pdf") With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


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



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

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