Prompt

How do I fill out PDF forms programmatically and flatten them?

Technology / Developer Tools1 observationLast seen Oct 12, 2026

Latest observation

Oct 12, 2026OpenAI APIWeb search: off

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 + img2pdf
    • Ghostscript
    • LibreOffice headless
    • 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:

  1. Fill fields with pypdf, pdftk, or a language-specific PDF library.
  2. Regenerate appearances if necessary.
  3. 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.