PDF Forms
You can create editable PDF documents with IronPDF as easily as a normal document. The PdfForm
class is a collection of user-editable form fields within a PDF document. It can be implemented into your PDF render to make it a form or an editable document.
This example shows you how to create editable PDF forms in IronPDF.
PDFs with editable forms can be created from HTML simply by adding <form>
, <input>
, and <textarea>
tags to the document parts.
The PdfDocument.Form.FindFormField
method can be used to read and write the value of any form field. The field's name will be the same as the 'name' attribute given to that field in your HTML.
The PdfDocument.Form
object can be used in two ways:
- Populating Default Values: This can be used to set a default value for form fields that will be displayed in PDF viewers like Adobe Reader.
- Reading User Input: After the user fills in the form, the form fields can be accessed and the data read back into your application.
using IronPdf; // Import the IronPdf library
// Function to create and manipulate a PDF document with form fields
public void CreateEditablePdfDocument()
{
// HTML string defining a simple form with input fields
var htmlContent = @"
<html>
<body>
<form>
<input type='text' name='username' placeholder='Enter your username' />
<textarea name='comments' placeholder='Enter your comments'></textarea>
</form>
</body>
</html>";
// Render the HTML to a PDF document
var renderer = new HtmlToPdf();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Access the form within the PDF
var pdfForm = pdfDocument.Form;
// Set default values for the form fields
pdfForm["username"].Value = "DefaultUser"; // Populate the 'username' field with a default value
pdfForm["comments"].Value = "Default comment."; // Populate the 'comments' field with a default text
// Save the PDF to a file
pdfDocument.SaveAs("EditableForm.pdf"); // Save the PDF document with a specified filename
// The document has now been created and saved with editable fields.
}
using IronPdf; // Import the IronPdf library
// Function to create and manipulate a PDF document with form fields
public void CreateEditablePdfDocument()
{
// HTML string defining a simple form with input fields
var htmlContent = @"
<html>
<body>
<form>
<input type='text' name='username' placeholder='Enter your username' />
<textarea name='comments' placeholder='Enter your comments'></textarea>
</form>
</body>
</html>";
// Render the HTML to a PDF document
var renderer = new HtmlToPdf();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Access the form within the PDF
var pdfForm = pdfDocument.Form;
// Set default values for the form fields
pdfForm["username"].Value = "DefaultUser"; // Populate the 'username' field with a default value
pdfForm["comments"].Value = "Default comment."; // Populate the 'comments' field with a default text
// Save the PDF to a file
pdfDocument.SaveAs("EditableForm.pdf"); // Save the PDF document with a specified filename
// The document has now been created and saved with editable fields.
}
Imports IronPdf ' Import the IronPdf library
' Function to create and manipulate a PDF document with form fields
Public Sub CreateEditablePdfDocument()
' HTML string defining a simple form with input fields
Dim htmlContent = "
<html>
<body>
<form>
<input type='text' name='username' placeholder='Enter your username' />
<textarea name='comments' placeholder='Enter your comments'></textarea>
</form>
</body>
</html>"
' Render the HTML to a PDF document
Dim renderer = New HtmlToPdf()
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Access the form within the PDF
Dim pdfForm = pdfDocument.Form
' Set default values for the form fields
pdfForm("username").Value = "DefaultUser" ' Populate the 'username' field with a default value
pdfForm("comments").Value = "Default comment." ' Populate the 'comments' field with a default text
' Save the PDF to a file
pdfDocument.SaveAs("EditableForm.pdf") ' Save the PDF document with a specified filename
' The document has now been created and saved with editable fields.
End Sub
In the example above, we first import the IronPdf library and define a method CreateEditablePdfDocument
. This method contains the HTML structure of a simple form with input fields for username and comments. Using the HtmlToPdf
renderer, we convert this HTML content into a PDF document.
The pdfDocument.Form
is then used to access and manipulate the form fields. We set default values that will appear when the document is opened in a PDF viewer. Finally, the document is saved with the name "EditableForm.pdf", allowing it to be stored or shared with embedded editable fields.