//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/create-and-fill-form.javaimport com.ironsoftware.ironpdf.PdfDocument;import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;import java.io.IOException;import java.nio.file.Paths;public class App { public static void main(String[] args) throws IOException { // Define an HTML form with two text input fields String formHTML = "<html>" + "<body>" + "<h2>Editable PDF Form</h2>" + "<form>" + "First name: <br><input type='text' name='firstname' value=''><br>" + "Last name: <br><input type='text' name='lastname' value=''>" + "</form>" + "</body>" + "</html>"; // Enable HTML-to-form-field conversion during rendering ChromePdfRenderOptions renderOptions = newChromePdfRenderOptions(); renderOptions.setCreatePdfFormsFromHtml(true); // Render HTML to PDF and save the blank templatePdfDocument.renderHtmlAsPdf(formHTML, renderOptions) .saveAs(Paths.get("assets/BasicForm.pdf")); // Load the template and fill in field values PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm.pdf")); form.getForm().setFieldValue("firstname", "Minnie"); form.getForm().setFieldValue("lastname", "Mouse"); // Save the completed form to a new file form.saveAs(Paths.get("assets/BasicForm_Filled.pdf")); }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/create-and-fill-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Define an HTML form with two text input fields
String formHTML = "<html>"
+ "<body>"
+ "<h2>Editable PDF Form</h2>"
+ "<form>"
+ "First name: <br><input type='text' name='firstname' value=''><br>"
+ "Last name: <br><input type='text' name='lastname' value=''>"
+ "</form>"
+ "</body>"
+ "</html>";
// Enable HTML-to-form-field conversion during rendering
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setCreatePdfFormsFromHtml(true);
// Render HTML to PDF and save the blank template
PdfDocument.renderHtmlAsPdf(formHTML, renderOptions)
.saveAs(Paths.get("assets/BasicForm.pdf"));
// Load the template and fill in field values
PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm.pdf"));
form.getForm().setFieldValue("firstname", "Minnie");
form.getForm().setFieldValue("lastname", "Mouse");
// Save the completed form to a new file
form.saveAs(Paths.get("assets/BasicForm_Filled.pdf"));
}
}
Java
最初のブロックは HTML の <input> 要素を静的テキストではなくライブ PDF フォームフィールドとして扱うように setCreatePdfFormsFromHtml(true) と ChromePdfRenderOptions を使用してレンダリングエンジンに指示します。 レンダリングされたPDFは再利用可能なテンプレートとして保存されます。 2番目のブロックは、そのテンプレートをロードし、各フィールドに名前で getForm().setFieldValue() を呼び出し、結果を書き込むための別の出力ファイルに書き込み、将来の使用のために元のテンプレートをそのまま残します。
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-existing-form.javaimport com.ironsoftware.ironpdf.PdfDocument;import java.io.IOException;import java.nio.file.Paths;public class FillExistingForm { public static void main(String[] args) throws IOException { // Load a third-party fillable PDF PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf")); // Set text field values by field name form.getForm().setFieldValue("applicant_name", "Jane Smith"); form.getForm().setFieldValue("date_of_birth", "1985-06-14"); form.getForm().setFieldValue("reference_number", "REF-2024-00421"); // Save the completed application form.saveAs(Paths.get("output/application_filled.pdf")); }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-existing-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Paths;
public class FillExistingForm {
public static void main(String[] args) throws IOException {
// Load a third-party fillable PDF
PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));
// Set text field values by field name
form.getForm().setFieldValue("applicant_name", "Jane Smith");
form.getForm().setFieldValue("date_of_birth", "1985-06-14");
form.getForm().setFieldValue("reference_number", "REF-2024-00421");
// Save the completed application
form.saveAs(Paths.get("output/application_filled.pdf"));
}
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/list-form-fields.javaimport com.ironsoftware.ironpdf.PdfDocument;import com.ironsoftware.ironpdf.form.FormField;import java.nio.file.Paths;public class ListFormFields { public static void main(String[] args) throws Exception { PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf")); // Print the name and type of every form field for (FormField field : form.getForm().getFields()) {System.out.println("Field: " + field.getName() + " | Type: " + field.getType()); } }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/list-form-fields.java
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.form.FormField;
import java.nio.file.Paths;
public class ListFormFields {
public static void main(String[] args) throws Exception {
PdfDocument form = PdfDocument.fromFile(Paths.get("templates/application.pdf"));
// Print the name and type of every form field
for (FormField field : form.getForm().getFields()) {
System.out.println("Field: " + field.getName()
+ " | Type: " + field.getType());
}
}
}
Java
データを書く前にフィールド名を印刷することは、setFieldValue() が一致しない名前で黙って失敗するのを防ぎます。 PDF ドキュメント内のフィールド名は大文字小文字を区別するため、"firstname" と "FirstName" は異なるフィールドとして扱われます。
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-checkboxes-radio.javaimport com.ironsoftware.ironpdf.PdfDocument;import java.nio.file.Paths;public class FillCheckboxesAndRadio { public static void main(String[] args) throws Exception { PdfDocument form = PdfDocument.fromFile(Paths.get("templates/survey.pdf")); // Check a checkbox by setting its exported value (often "Yes" or "On") form.getForm().setFieldValue("agree_terms", "Yes"); // Select a radio button option by its exported value form.getForm().setFieldValue("preferred_contact", "email"); form.saveAs(Paths.get("output/survey_filled.pdf")); }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-checkboxes-radio.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;
public class FillCheckboxesAndRadio {
public static void main(String[] args) throws Exception {
PdfDocument form = PdfDocument.fromFile(Paths.get("templates/survey.pdf"));
// Check a checkbox by setting its exported value (often "Yes" or "On")
form.getForm().setFieldValue("agree_terms", "Yes");
// Select a radio button option by its exported value
form.getForm().setFieldValue("preferred_contact", "email");
form.saveAs(Paths.get("output/survey_filled.pdf"));
}
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-dropdown.javaimport com.ironsoftware.ironpdf.PdfDocument;import java.nio.file.Paths;public class FillDropdown { public static void main(String[] args) throws Exception { PdfDocument form = PdfDocument.fromFile(Paths.get("templates/registration.pdf")); // Set a dropdown/combo box field to a specific option form.getForm().setFieldValue("country", "United States"); // Set a list box field (multi-select may require comma-separated values) form.getForm().setFieldValue("subscription_tier", "Professional"); form.saveAs(Paths.get("output/registration_filled.pdf")); }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/fill-dropdown.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;
public class FillDropdown {
public static void main(String[] args) throws Exception {
PdfDocument form = PdfDocument.fromFile(Paths.get("templates/registration.pdf"));
// Set a dropdown/combo box field to a specific option
form.getForm().setFieldValue("country", "United States");
// Set a list box field (multi-select may require comma-separated values)
form.getForm().setFieldValue("subscription_tier", "Professional");
form.saveAs(Paths.get("output/registration_filled.pdf"));
}
}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/flatten-form.javaimport com.ironsoftware.ironpdf.PdfDocument;import java.nio.file.Paths;public class FlattenForm { public static void main(String[] args) throws Exception { PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm_Filled.pdf")); // Flatten all form fields -- values become static text form.getForm().flatten(); form.saveAs(Paths.get("output/BasicForm_Archived.pdf")); }}
//:path=/static-assets/pdf/content-code-examples/how-to/java-fill-pdf-form-tutorial/flatten-form.java
import com.ironsoftware.ironpdf.PdfDocument;
import java.nio.file.Paths;
public class FlattenForm {
public static void main(String[] args) throws Exception {
PdfDocument form = PdfDocument.fromFile(Paths.get("assets/BasicForm_Filled.pdf"));
// Flatten all form fields -- values become static text
form.getForm().flatten();
form.saveAs(Paths.get("output/BasicForm_Archived.pdf"));
}
}
How can I fill dropdown lists in a PDF form using IronPDF?
Use setFieldValue() to assign the dropdown option's export value. The exact export value should match the option's defined value in the PDF form, with FormComboBoxField.getOptions() providing the list of valid values.
How do I flatten a filled PDF form using IronPDF?
Flattening a filled PDF form with IronPDF can be done by calling form.getForm().flatten() on the PdfDocument, which converts form fields into static text, preventing further editing.
What are some additional features of IronPDF for Java?
Beyond form filling, IronPDF features include rendering HTML to PDF, manipulating PDF security settings, applying digital signatures, and allowing programmatic access to various PDF document elements.
How can I find more resources to further explore IronPDF capabilities?
Explore the [IronPDF Java API reference](https://ironpdf.com/java/object-reference/api/) for detailed documentation, and additional guides on creating forms from HTML, HTML to PDF conversion, and PDF security and digital signing.