Prompt
How do I fill out PDF forms programmatically and flatten them?
Latest observation
There are a few common ways to do this, depending on your language and whether you want a fully open-source solution.
What “fill and flatten” means
- Fill PDF form fields: write values into AcroForm fields like text boxes, checkboxes, dropdowns.
- Flatten the PDF: render the filled values into the page content so the result is no longer editable as form fields.
Recommended approaches
1) Python with pypdf + a PDF renderer if needed
Good for simple AcroForms.
Fill fields
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
writer.update_page_form_field_values(
writer.pages[0], # sometimes form fields are updated via pages
{"Name": "Alice", "Date": "2026-10-12"}
)
with open("filled.pdf", "wb") as f:
writer.write(f)
Flattening
pypdf does not reliably flatten in all cases by itself. Common options:
- Use a library that supports flattening directly
- Or fill the form and then print/render it to a new PDF using tools like:
pdftoppm+img2pdfGhostscriptLibreOfficeheadless- commercial libraries such as iText / PDFTron
Example using Ghostscript to flatten after filling:
gs -o flattened.pdf -sDEVICE=pdfwrite -dNOPAUSE -dBATCH filled.pdf
This often removes form interactivity and merges appearances into page content.
2) Java with iText
Very solid for forms and flattening.
PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"),
new PdfWriter("output.pdf"));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("Name").setValue("Alice");
fields.get("Date").setValue("2026-10-12");
form.flattenFields();
pdfDoc.close();
This is one of the cleanest ways if you’re okay with the license terms.
3) C# / .NET with iText or PDF libraries
Similar workflow:
- open PDF
- set field values
- flatten form fields
- save
Commercial libraries like:
- iText 7
- PDFium-based wrappers
- Telerik / Syncfusion / DevExpress PDF tools
4) Command-line tools
If you already have a filled PDF and just want to flatten:
- Ghostscript
- pdftk (older, but still used)
- MuPDF tools
- qpdf (not a flattener by itself, but useful in pipelines)
Example with pdftk:
pdftk input.pdf fill_form data.fdf output filled.pdf flatten
If your data is in XFDF/FDF format, some tools can import it directly.
Important caveats
Not all PDFs are the same
- AcroForms: fillable forms, generally supported by tools above.
- XFA forms: more complex; many open-source tools handle these poorly.
- Adobe/enterprise libraries often work better.
- You may need to convert or render instead of true field filling.
Flattening can depend on appearance streams
Sometimes fields are filled but not visible unless the PDF has appearance streams generated. To avoid issues:
- Ensure “appearance” is regenerated
- Use a library that supports this
- Or flatten after filling
Best practical recipe
If you want a reliable open-source-ish pipeline:
- Fill fields with
pypdf,pdftk, or a language-specific PDF library. - Regenerate appearances if necessary.
- Flatten using Ghostscript or a PDF library that supports flattening.
Example pipeline in Python
from pypdf import PdfReader, PdfWriter
reader = PdfReader("form.pdf")
writer = PdfWriter()
writer.clone_document_from_reader(reader)
writer.update_page_form_field_values(
writer.pages[0],
{
"first_name": "Alice",
"last_name": "Smith",
}
)
with open("filled.pdf", "wb") as f:
writer.write(f)
Then flatten:
gs -o flattened.pdf -sDEVICE=pdfwrite -dNOPAUSE -dBATCH filled.pdf
If you tell me your language/environment (Python, Node.js, Java, C#, Go, CLI, etc.) and whether the PDF is AcroForm or XFA, I can give you a working example tailored to it.