PDF Forms
HTML with form fields can also be rendered to PDF with the form field information and functionality intact. Creating an editable PDF form from HTML content involves defining an HTML form with various input fields like text fields, radio buttons, and checkboxes. These form elements allow users to input data and make selections.
To achieve this, set the createPdfFormsFromHtml
option to true
. This option specifies that the HTML form should be converted into an editable PDF form.
Next, render the HTML content into a PDF document using the PdfDocument.fromHtml
method. Pass the specified rendering options as an object parameter.
Finally, export the editable form field PDF as a new PDF file named "formField.pdf" using the saveAs
method.
To learn more about generating PDF forms from HTML content, visit the IronPDF Product Page for detailed information and examples.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Form Example</title>
</head>
<body>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label>Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<label>Interests:</label><br>
<input type="checkbox" id="coding" name="interest" value="coding">
<label for="coding">Coding</label><br>
<input type="checkbox" id="music" name="interest" value="music">
<label for="music">Music</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Form Example</title>
</head>
<body>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label>Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<label>Interests:</label><br>
<input type="checkbox" id="coding" name="interest" value="coding">
<label for="coding">Coding</label><br>
<input type="checkbox" id="music" name="interest" value="music">
<label for="music">Music</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
// Import the necessary namespaces
using IronPdf;
class PdfFormGenerator
{
static void Main()
{
// Define the HTML content to be converted into a PDF
string htmlContent = @"
<html>
<body>
<form>
<input type='text' name='username'>
<input type='password' name='password'>
</form>
</body>
</html>";
// Instantiate a new HtmlToPdf object
var renderer = new HtmlToPdf();
// Set the PdfFormsFromHtml property to true
var options = new PdfPrintOptions()
{
CreatePdfFormsFromHtml = true
};
// Render the HTML to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent, options);
// Save the document with the editable form fields as "formField.pdf"
pdf.SaveAs("formField.pdf");
}
}
// Import the necessary namespaces
using IronPdf;
class PdfFormGenerator
{
static void Main()
{
// Define the HTML content to be converted into a PDF
string htmlContent = @"
<html>
<body>
<form>
<input type='text' name='username'>
<input type='password' name='password'>
</form>
</body>
</html>";
// Instantiate a new HtmlToPdf object
var renderer = new HtmlToPdf();
// Set the PdfFormsFromHtml property to true
var options = new PdfPrintOptions()
{
CreatePdfFormsFromHtml = true
};
// Render the HTML to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent, options);
// Save the document with the editable form fields as "formField.pdf"
pdf.SaveAs("formField.pdf");
}
}
' Import the necessary namespaces
Imports IronPdf
Friend Class PdfFormGenerator
Shared Sub Main()
' Define the HTML content to be converted into a PDF
Dim htmlContent As String = "
<html>
<body>
<form>
<input type='text' name='username'>
<input type='password' name='password'>
</form>
</body>
</html>"
' Instantiate a new HtmlToPdf object
Dim renderer = New HtmlToPdf()
' Set the PdfFormsFromHtml property to true
Dim options = New PdfPrintOptions() With {.CreatePdfFormsFromHtml = True}
' Render the HTML to a PDF document
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent, options)
' Save the document with the editable form fields as "formField.pdf"
pdf.SaveAs("formField.pdf")
End Sub
End Class