Create, Fill, and Edit Fillable PDF Forms Using C# - A Complete Guide | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Create Fill and Edit Fillable PDF Forms Using C A Complete Guide

Create, Fill, and Edit Fillable PDF Forms Using C# – A Complete Guide

PDF forms allow users to input and submit data in a structured format. These forms are popular because they are easy to use and can be filled out electronically, reducing the need for paper and manual data entry. They can also be saved and shared electronically, making them convenient for individuals and organizations.

The Syncfusion .NET PDF Library provides comprehensive support for creating, customizing, and managing form fields in PDF documents.

This article will explore several use cases of PDF forms and how they can be implemented using the Syncfusion .NET PDF Library.

Agenda

Transform your PDF files effortlessly in C# with just five lines of code using Syncfusion's comprehensive PDF Library!

Create a new, fillable PDF form

Syncfusion’s .NET PDF Library allows users to easily create forms (Acroforms) in PDF documents with various form fields. You can include text boxes, combo boxes, radio buttons, list boxes, checkboxes, signature fields, buttons, and more in the PDF form.

Let’s create a simple form with a text box and radio button fields in a PDF document:

  1. First, create a new PDF document using the PdfDocument class.
  2. Add a new blank page using the PdfPage class.
  3. Create text box fields to collect the name, email address, and phone number using the PdfTextBoxField class.
  4. Then, create the radio button fields for gender selection using the PdfRadioButtonListField class and add the list items using the PdfRadioButtonListItem class.
  5. Finally, save the PDF document using the Save method.

The following code example shows how to create a simple form in a PDF document using C#.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a new page to the PDF document.
PdfPage page = document.Pages.Add();
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
//Draw the string.      
page.Graphics.DrawString("Job Application", font, PdfBrushes.Black, new PointF(250, 0));
font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
page.Graphics.DrawString("Name", font, PdfBrushes.Black, new PointF(10, 20));

//Create a text box field for the name.
PdfTextBoxField textBoxField1 = new PdfTextBoxField(page, "Name");
textBoxField1.Bounds = new RectangleF(10, 40, 200, 20);
textBoxField1.ToolTip = "Name";
document.Form.Fields.Add(textBoxField1);

page.Graphics.DrawString("Email address", font, PdfBrushes.Black, new PointF(10, 80));
//Create a text box field for the email address.
PdfTextBoxField textBoxField3 = new PdfTextBoxField(page, "Email address");
textBoxField3.Bounds = new RectangleF(10, 100, 200, 20);
textBoxField3.ToolTip = "Email address";
document.Form.Fields.Add(textBoxField3);

page.Graphics.DrawString("Phone", font, PdfBrushes.Black, new PointF(10, 140));
//Create a text box field for the phone number.
PdfTextBoxField textBoxField4 = new PdfTextBoxField(page, "Phone");
textBoxField4.Bounds = new RectangleF(10, 160, 200, 20);
textBoxField4.ToolTip = "Phone";
document.Form.Fields.Add(textBoxField4);

page.Graphics.DrawString("Gender", font, PdfBrushes.Black, new PointF(10, 200));
//Create a radio button for gender.
PdfRadioButtonListField employeesRadioList = new PdfRadioButtonListField(page, "Gender");
document.Form.Fields.Add(employeesRadioList);
page.Graphics.DrawString("Male", font, PdfBrushes.Black, new PointF(40, 220));
PdfRadioButtonListItem radioButtonItem1 = new PdfRadioButtonListItem("Male");
radioButtonItem1.Bounds = new RectangleF(10, 220, 20, 20);
page.Graphics.DrawString("Female", font, PdfBrushes.Black, new PointF(140, 220));
PdfRadioButtonListItem radioButtonItem2 = new PdfRadioButtonListItem("Female");
radioButtonItem2.Bounds = new RectangleF(110, 220, 20, 20);
employeesRadioList.Items.Add(radioButtonItem1);
employeesRadioList.Items.Add(radioButtonItem2);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    document.Save(outputFileStream);
}
//Close the document.
document.Close(true);

Running this code example will generate a PDF that closely resembles the image displayed below.

Creating a PDF form using Syncfusion .NET PDF Library
Creating a PDF form using Syncfusion .NET PDF Library

Say goodbye to tedious PDF tasks and hello to effortless document processing with Syncfusion's PDF Library.

Fill form fields in a PDF

We can use the TryGetField method to get a form field from an existing document using the field name. This method helps us determine the field’s availability within the form by returning a Boolean value.

Follow these steps to fill in the form fields in a PDF document:

  1. Use the PdfLoadedDocument class to load the created PDF document.
  2. Get the form from the PDF using the PdfLoadedForm class.
  3. Now, get the form field collections using the PdfLoadedFormFieldCollection class.
  4. Use the TryGetField method to obtain the form fields, such as text boxes and checkboxes, and then fill them with the necessary information.
  5. Finally, save the filled PDF document using the Save method.

The following code example shows how to fill form fields in a PDF document.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Load the form from the loaded PDF document.
PdfLoadedForm form = loadedDocument.Form;
//Load the form field collections from the form.
PdfLoadedFormFieldCollection fieldCollection = form.Fields as PdfLoadedFormFieldCollection;
PdfLoadedField loadedField = null;

//Get and fill the field using TryGetField Methodmethod.
if (fieldCollection.TryGetField("Name", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "Simons";
}
if (fieldCollection.TryGetField("Email address", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
}
if (fieldCollection.TryGetField("Phone", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "31 12 34 56";
}
if (fieldCollection.TryGetField("Gender", out loadedField))
{
    (loadedField as PdfLoadedRadioButtonListField).SelectedIndex = 0;
}

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

This code generates a pre-filled PDF with your designated form field values.

Filling the form fields in a PDF document
Filling the form fields in a PDF document

Note: The specific layout and design of the filled PDF document may vary based on the original PDF document and the values set for the form fields.

Modifying a form field in a PDF 

You can also modify an existing form field and its properties like bounds, text, color, and border. To do so, please follow these steps:

  1. Use the PdfLoadedDocument class to load an existing PDF document.
  2. Get the existing form in a PDF using the PdfLoadedForm class.
  3. Get the required form fields from the PdfFormFieldCollection.
  4. Modify the text box form field properties.
  5. Finally, save the PDF document using the Save method.

The following code example illustrates how to modify an existing form field in a PDF document.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Get the loaded form.
PdfLoadedForm loadedForm = loadedDocument.Form;

//Get the loaded form field and modify the properties.
PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[0] as PdfLoadedTextBoxField;
RectangleF newBounds = new RectangleF(200, 60, 300, 30);
loadedTextBoxField.Bounds = newBounds;
loadedTextBoxField.Text = "John";
loadedTextBoxField.BackColor = Color.LightYellow;
loadedTextBoxField.BorderColor = Color.Red;

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

By executing this code example, we will get a PDF like the following image.

Modifying a form field in a PDF document
Modifying a form field in a PDF document

Removing form fields from a PDF

You can easily remove form fields from a PDF document by following these steps:

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Get the form using the PdfLoadedForm class.
  3. Now, remove the required form field from the index using the RemoveAt method or specify the field name in the Remove method of the PdfFieldCollection class.
  4. Then, save the PDF document using the Save method.

The following code illustrates how to remove form fields from a PDF document.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Get the loaded form.
PdfLoadedForm loadedForm = loadedDocument.Form;

//Load the text box field.
PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[2] as PdfLoadedTextBoxField;
//Remove the field.
loadedForm.Fields.Remove(loadedTextBoxField);
//Remove the field at index 1.
loadedForm.Fields.RemoveAt(1);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

By executing this code example, we will get the PDF in the following image.

Removing forms fields from a PDF document
Removing forms fields from a PDF document

Explore the wide array of rich features in Syncfusion's PDF Library through step-by-step instructions and best practices.

Create a signature field and sign the PDF document

The Syncfusion .NET PDF Library helps create signature fields for signing PDF documents with ease.

Adding a signature field to a PDF 

Follow these steps to create a signature field in a PDF document using Syncfusion’s .NET PDF Library:

  1. Create a new PDF document using the PdfDocument class.
  2. Add a new blank page using the PdfPage class.
  3. Create a new signature field using the PdfSignatureField class.
  4. Save the PDF document using the Save method.

The following code example illustrates creating a signature field in a new PDF document.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a new page to the PDF document.
PdfPage page = document.Pages.Add();

//Create a PDF sSignature field.
PdfSignatureField signatureField = new PdfSignatureField(page, "Signature");
//Set properties to the signature field.
signatureField.Bounds = new RectangleF(0, 100, 90, 20);
signatureField.ToolTip = "Signature";
//Add the form field to the document.
document.Form.Fields.Add(signatureField);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    document.Save(outputFileStream);
}
//Close the document.
document.Close(true);

By executing this code example, you will get a PDF like in the following image.

Adding signature field in a PDF
Adding a signature field in a PDF

Digitally sign the PDF document

For how to digitally sign a PDF and verify signatures, refer to this blog.

Restrict editing capabilities of form fields

PDF forms are an excellent way to gather information from users. However, you may want to restrict the editing capability of all or certain form fields to prevent unintended modifications. This can be done by flattening the PDF document or marking the form or field as read-only.

Flattening form fields

The Syncfusion .NET PDF Library supports flattening a specific form field or entire form by removing the existing form field and replacing it with graphical objects that resemble the form field but cannot be edited.

Here are the steps to flatten the form fields in a PDF document:

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Get the form using the PdfLoadedForm class.
  3. Get the form field collections using the PdfLoadedFormFieldCollection class.
  4. Now, flatten the whole form using the Flatten property.
  5. Finally, save the PDF document using the Save method.

The following code example illustrates how to flatten the form fields in a PDF document.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Get the loaded form.
PdfLoadedForm loadedForm = loadedDocument.Form;
PdfLoadedFormFieldCollection fields = loadedForm.Fields;
//Flatten the whole form.
loadedForm.Flatten = true;

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

Marking the PDF form read-only

We can restrict the editing of a PDF form by marking it as read-only:

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Get the form using the PdfLoadedForm class.
  3. Set the form to read-only using the ReadOnly property.
  4. Then, save the PDF document using the Save method.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Get the loaded form.
PdfLoadedForm loadedForm = loadedDocument.Form;
//Set the form as read-only.
loadedForm.ReadOnly = true;

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

By executing this code example, you will get a PDF like in the following image.

Restricting editing capabilities of a PDF form
Restricting editing capabilities of a PDF form

Witness the advanced capabilities of Syncfusion's PDF Library with feature showcases.

Import and export PDF form data

PDF forms are commonly used to collect data from users. Once the data is collected, it may need to be exported or imported for further analysis or processing. The Syncfusion .NET PDF Library supports importing and exporting PDF form data in various formats such as FDF, XFDF, JSON, and XML. This makes it easy to integrate form data with other apps and systems.

Import form field data to PDF

The following steps illustrate how to import form field data to a PDF document:  

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Get the form in the PDF using the PdfLoadedForm class
  3. Import the form field data from an FDF file to the PDF using the ImportDataFDF method.
  4. Finally, save the modified PDF document using the Save method.

Refer to the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Load the existing form.
PdfLoadedForm loadedForm = loadedDocument.Form;
//Load the FDF file.
FileStream fileStream = new FileStream("Input.fdf", FileMode.Open, FileAccess.Read);
//Import the FDF stream.
loadedForm.ImportDataFDF(fileStream, true);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

By executing this code example, you will get a PDF like in the following image.

Importing form fields from FDF file to PDF form
Importing from fields from the FDF file to the PDF form

Export form field data from PDF

The following steps illustrate how to export PDF form field data to an XML file:

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Get the form from the PDF using the PdfLoadedForm class.
  3. Export the PDF form field data to an XML file using the ExportData You can specify the format of the exported data using the DataFormat enum.
  4. Save the modified PDF document using the Save method.

Refer to the following code example.

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Load an existing form. 
PdfLoadedForm loadedForm = loadedDocument.Form;
//Create memory stream.
MemoryStream ms = new MemoryStream();
//Create the XML file. 
FileStream stream = new FileStream("Export.xml", FileMode.Create, FileAccess.ReadWrite);
//Export the existing PDF form fields data to the XML file. 
loadedForm.ExportData(stream, DataFormat.Xml, "AcroForm1");
//Close the document.
loadedDocument.Close(true);

By executing this code example, you will get XML like in the following image.

Exporting PDF form field data to an XML file
Exporting PDF form field data to an XML file

Retain extended rights of PDF forms

Extended features in a PDF form refer to additional capabilities that are not part of the standard PDF specification. These features allow users to do more with the form than just filling out fields and submitting data.

The Syncfusion .NET PDF Library supports preserving extended rights in PDF forms when performing actions like filling the form fields.

You can fill the reader-extended PDF document by following these steps:

  1. Use the PdfLoadedDocument class to load the extended featured PDF document.
  2. Get the form from the PDF using the PdfLoadedForm class.
  3. Get the form field collections using the PdfLoadedFormFieldCollection class.
  4. Use the TryGetField method to obtain the First Name, Last Name, and Email address form fields and then fill them with the necessary information.
  5. Finally, save the PDF document (Extended rights preserved) using the Save method.

Refer to the following code example.

//Load a PDF.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
//Load the form from the loaded document.
PdfLoadedForm loadedForm = loadedDocument.Form;
loadedForm.SetDefaultAppearance(false);
//Load the form field collections from the form.
PdfLoadedFormFieldCollection fieldCollection = loadedForm.Fields as PdfLoadedFormFieldCollection;
PdfLoadedField loadedField = null;

//Get the field using TryGetField method.
if (fieldCollection.TryGetField("First Name", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "Simons";
}
if (fieldCollection.TryGetField("Last Name", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "Bistro";
}
if (fieldCollection.TryGetField("Email Address", out loadedField))
{
    (loadedField as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com";
}

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    loadedDocument.Save(outputFileStream);
}
//Close the document.
loadedDocument.Close(true);

By executing this code example, you will get a PDF like in the following image.

Retaining extended rights in a PDF form
Retaining extended rights in a PDF form

See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.

Utilizing JavaScript actions in PDF forms

The Syncfusion .NET PDF Library also supports adding JavaScript actions to the form fields using the PdfJavaScriptAction class:

  1. Create a new PDF document using the PdfDocument class.
  2. Then, add a new blank page in it using the PdfPage class.
  3. Use the PdfButtonField class to create a new button field.
  4. Set the JavaScript action for the submit button using the PdfJavaScriptAction class.
  5. Finally, save the PDF document using the Save method.

Refer to the following code example.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Create a new page.
PdfPage page = document.Pages.Add();

//Create a new PdfButtonField.
PdfButtonField submitButton = new PdfButtonField(page, "submitButton");
submitButton.Bounds = new RectangleF(25, 160, 100, 20);
submitButton.Text = "Apply";
submitButton.BackColor = new PdfColor(181, 191, 203);

//Create a new PdfJavaScriptAction.
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"You are looking at form fields action of PDF document\")");
//Set the scriptAction to submitButton.
submitButton.Actions.MouseDown = scriptAction;

//Add the submit button to the new document.
document.Form.Fields.Add(submitButton);

//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
    //Save the PDF document to file stream.
    document.Save(outputFileStream);
}
//Close the document.
document.Close(true);

By executing this code example, you will get a PDF like in the following screenshot.

Adding JavaScript actions to PDF form
Adding JavaScript actions to PDF form

GitHub reference

For more details, refer to the create, fill, and edit fillable PDF forms using the C# demo on GitHub.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

Conclusion

Thanks for reading! In this blog, we’ve seen how to create, fill, and edit forms in PDF documents using the Syncfusion .NET PDF Library. Try out the steps in this blog post and leave your feedback in the comments section below.

Take a moment to look at the PDF forms documentation, where you can find other options and features, all with accompanying code examples.

For questions, you can contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related Blog

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed