PDF Forms
This code example demonstrates how developers can use IronPDF to create and edit PDF documents with fillable forms.
To create PDF forms, simply create the form as desired using HTML markup, and convert it into PDF using one of IronPDF's available static rendering methods. Use PdfDocument.renderHtmlAsPdf
to convert an HTML form marked up in a string of HTML (as shown in the example above), and use PdfDocument.renderHtmlFileAsPdf
to convert an HTML file containing a form located on a local file path. Both methods will produce PDF forms that users can open and fill in using their preferred PDF reading applications.
By default, IronPDF will make any form within HTML markup editable. Developers can toggle this behavior on and off as needed by supplying a ChromePdfRenderOptions
object along with the HTML content to a PDF rendering method. Set the createPdfFormsFromHtml
property to true or false with the setCreatePdfFormsFromHtml
method to enable or disable editable forms.
Reading and writing values to a PDF form's fields is achieved through its PDfDocument
's FormManager
. Reference it using the below line of code:
FormManager pdfForm = document.getForm();
As shown on lines 26 and 29 of the code example, the developer can set values on form text fields directly by calling a FormManager
's setFieldValue
with the name of the text field (as specified in the associated <input>
element name attribute of the original HTML markup), followed by the value with which it should be filled.
To read a form field's value, the developer must first get a direct reference to the desired form field through a FormManager
's indexed list of FormField
objects. The code below illustrates how this should be done:
FormManager pdfForm = document.getForm();
List<FormField> fields = pdfForm.getFields().getAllFields();
FormField firstNameField = fields.get(0);
System.out.println("First Name: " + firstNameField.getValue());