How to Create PDFs from HTML and Add Signature Fields in C#?
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. Using this library, you can easily convert HTML content into a PDF and add signature fields to specific areas of the document.
Steps to add a signature field in a PDF converted from HTML programmatically
- Create a new console application project.
- Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference in your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
- Use the following code sample in Program.cs to add a signature field in a PDF converted from HTML.
C#
// Initialize the HTML to PDF converter using the Blink rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Configure the converter to preserve form fields in the PDF
BlinkConverterSettings settings = new BlinkConverterSettings
{
EnableForm = true // Ensures form elements like <input>, <textarea> are converted to PDF fields
};
htmlConverter.ConverterSettings = settings;
// Convert the HTML file to a PDF document
PdfDocument document = htmlConverter.Convert("Test.html");
// Optional: Remove default appearances for form fields to match the page style
document.Form.SetDefaultAppearance(false);
// Save the PDF to a memory stream for further processing
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream); // Save converted PDF to memory
stream.Position = 0; // Reset stream position
document.Close(true); // Close the original document
// Replace the "signature" textarea with an actual signature field
AddPdfSignatureField(stream);
}
/// <summary>
/// Finds the "signature" field in the form, removes it, and replaces it with a true PDF signature field.
/// </summary>
/// <param name="stream">MemoryStream containing the PDF document</param>
static void AddPdfSignatureField(MemoryStream stream)
{
// Load the PDF document from memory stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream))
{
PdfLoadedForm loadedForm = loadedDocument.Form;
// Check for a field named "signature"
if (loadedForm.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
loadedForm.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
loadedForm.Fields.Add(signatureField);
}
// Save the modified document
loadedDocument.Save("Output.pdf");
// Close the document and release resources
loadedDocument.Close(true);
}
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation , where you will find features for converting HTML pages into PDF documents.
Conclusion
I hope you enjoyed learning on how to add a signature field in a PDF converted from HTML.
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!