How to generate fillable PDF form fields using an HTML to PDF converter
The Syncfusion® HTML-to-PDF converter is a robust .NET library designed to convert HTML, SVG, MHTML, and web pages into high-quality PDF documents using C#. Leveraging the Blink rendering engine (the same engine behind Google Chrome), it delivers precise rendering of text, images, fonts, and layouts.
This article explains how to convert HTML forms into fillable PDF documents using Syncfusion’s .NET PDF library. You’ll learn how to define interactive form fields in HTML, preserve their functionality during conversion, and customize the layout and appearance of the resulting PDF. This approach gives you full control over the form design and ensures a seamless user experience when filling out PDFs.
Steps to generate fillable PDF form fields from HTML using a converter
- Create a new project: Start a new Console application in .NET Core to facilitate the HTML-to-PDF conversion process.
- Install required packages: Add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package from Nuget.org.
- Set up the environment: In the
Program.csfile, include these namespaces.
C#
using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
- Generate fillable PDF form fields: Follow these steps to convert HTML forms into interactive, fillable PDF documents using an HTML to PDF converter.
C#
// Initialize the HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Enable form conversion
BlinkConverterSettings settings = new BlinkConverterSettings
{
EnableForm = true
};
htmlConverter.ConverterSettings = settings;
// Convert the HTML file to a PDF document
using (PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html")))
{
// Set default appearance for form fields
document.Form.SetDefaultAppearance(false);
// Save the PDF document to a memory stream
using (MemoryStream stream = new MemoryStream())
{
// Load the saved PDF document
document.Save(stream);
// Load the PDF document containing form fields
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
// Fill the form fields
PdfLoadedForm form = loadedDocument.Form;
// Fill the "name" field
if (form.Fields["name"] is PdfLoadedTextBoxField nameField)
{
nameField.Text = "XYZ";
}
// Fill the "email" field
if (form.Fields["email"] is PdfLoadedTextBoxField emailField)
{
emailField.Text = "xyz@example.com";
}
// Select "Male" in the "gender" dropdown
if (form.Fields["gender"] is PdfLoadedComboBoxField genderField)
{
genderField.SelectedValue = "Male";
}
// Fill the "signature" field
if (form.Fields["signature"] is PdfLoadedTextBoxField signatureTextBox)
{
// Get the original field's position and page
RectangleF bounds = signatureTextBox.Bounds;
PdfPageBase page = signatureTextBox.Page;
// Remove the original textbox field
form.Fields.Remove(signatureTextBox);
// Create a new signature field at the same location
PdfSignatureField signatureField = new PdfSignatureField(page, "ClientSignature")
{
Bounds = bounds
};
// Add the new signature field to the form
form.Fields.Add(signatureField);
}
// Save the PDF document
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
}
}
A complete working sample can be downloaded from GitHub.
By executing the program, you will generate the following PDF document.
Conclusion
I hope you enjoyed learning how to convert HTML forms into fillable PDF documents using an HTML to PDF converter.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!