---
title: "Fill and flatten a PDF form in Go"
url: https://recursiveai.org/onepdf/go/cookbook/fill-pdf-form-in-go/
description: "Fill named AcroForm fields in Go, then flatten the PDF so the answers appear as page content. Run the example on US Courts form AO 440."
version: "1.0.1"
---

# Fill and flatten a PDF form in Go

**onepdf-go** is a free, open-source PDF library for Go from Recursive AI that reads, edits, signs, redacts and lays out PDFs. MIT or Apache-2.0, no third-party dependencies.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onepdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-go) · [Docs](https://recursiveai.org/onepdf/go/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/go/cookbook/fill-pdf-form-in-go/fill-pdf-form-in-go.zip)

Use `Form().Fill` on the sample AcroForm (a standard fillable PDF), then `Flatten` to make the answers part of the page. This won’t fill XFA-only forms.

```shell
go get recursiveai.org/go/onepdf
go get recursiveai.org/go/onepdf/fonts
```

Go 1.24+ required (download: Go 1.26.8).

Use `ao440.pdf` from the zip, or download [US Courts form AO 440](https://www.uscourts.gov/sites/default/files/ao440.pdf) and save it as `ao440.pdf` next to `main.go`. The form has “Plaintiff(s)” and “Defendant(s)” boxes.

```go
package main

import (
	"fmt"
	"log"

	"recursiveai.org/go/onepdf"
	"recursiveai.org/go/onepdf/fonts"
)

func main() {
	fonts.Register()
	edit := onepdf.WithEditing()
	doc, err := onepdf.OpenFile("ao440.pdf", edit)
	if err != nil {
		log.Fatal(err)
	}
	defer doc.Close()
	values := map[string]any{
		"Plaintiff": "Example Plaintiff",
		"Defendant": "Example Defendant",
	}
	if err := doc.Form().Fill(values); err != nil {
		log.Fatal(err)
	}
	if _, err := doc.Flatten(); err != nil {
		log.Fatal(err)
	}
	if err := doc.SaveFile("flattened.pdf"); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Wrote flattened.pdf")
}
```

Paste the code above into main.go, run `go mod init example` and `go get recursiveai.org/go/onepdf`, then `go run .`; or unzip [the example](https://recursiveai.org/onepdf/go/cookbook/fill-pdf-form-in-go/fill-pdf-form-in-go.zip) and `go run .`.

If you paste the code into a new module, also run `go get recursiveai.org/go/onepdf/fonts` before `go run .`.

`go run .` prints:

```text
Wrote flattened.pdf
```

Open `flattened.pdf`: the first page shows “Example Plaintiff” and “Example Defendant” as page content, not editable boxes. The remaining blank form fields are flattened too. `Fill` can apply some values before returning an error, so the example stops on any fill error before saving.

## Variations

**Find the field names for another PDF.** Replace everything from `values :=` through the final `Println` with this loop. It prints the fully qualified names of top-level fields and their descendants:

```go
var names func([]*onepdf.Field)
names = func(fields []*onepdf.Field) {
    for _, field := range fields {
        fmt.Println(field.FullName())
        names(field.Children())
    }
}
if form := doc.Form(); form != nil {
    names(form.Fields())
}
```

Two lines from the output for `ao440.pdf`:

```text
Plaintiff
Defendant
```

**Keep the form editable.** Omit the `Flatten` call and save to a separate file instead:

```go
if err := doc.SaveFile("filled.pdf"); err != nil {
    log.Fatal(err)
}
fmt.Println("Wrote filled.pdf")
```

## Related recipes

- [Create a PDF in Go](https://recursiveai.org/onepdf/go/cookbook/create-pdf-in-go/)
- [Merge PDF files in Go](https://recursiveai.org/onepdf/go/cookbook/merge-pdf-files-in-go/)

## The short answer

Open a fillable PDF in Go with `onepdf.WithEditing()`, put the field names and answers in a `map[string]any`, and pass it to `doc.Form().Fill`. Call `doc.Flatten()` before saving if the answers should appear on the page without editable form fields. Keep the original blank form if you’ll fill it again.

## How do I auto-populate a PDF form in Go?

Match your input data to the PDF’s internal names, not the printed captions beside the fields. The AO 440 sample says “Plaintiff(s)” on the page, but the key you need is `Plaintiff`. In a nested group, use the full name, including the parent names, as printed by `FullName()`. Once you have that map, you can reuse the same code for every input record. If the template changes, list the names again before assuming the old keys still work.

`Fill` accepts strings for text values, Booleans for checked states, slices of strings for multiple values, and `nil` to clear a value. A typo or a refused field can produce an error while other entries succeed. Check the call’s error before writing an output file rather than assuming that every field was accepted together.

## How do I flatten a fillable PDF form?

Fill it first, then call `Flatten` before you save. Flattening draws the field appearances into the page and removes the ordinary widgets from the form; signed signature fields are kept. The example registers bundled substitute fonts so the library can use them when a font isn’t embedded. Check that the resulting page shows the answers where you expect them. If a recipient needs to add or correct answers later, send the filled but unflattened version instead.

## Can I fill an XFA PDF form this way?

The example uses an ordinary AcroForm. `HasXFA()` detects an XFA entry, but the library doesn’t render or run XFA data. Some files have both XFA data and standard fields; the example’s approach applies only to the AcroForm fields. If you have an XFA-only template, or a flat scan with no fields at all, get a fillable AcroForm version rather than guessing names for `Fill`.
