Flatten
Basic Flatten
Open a filled form PDF and flatten all fields into static content.
doc, _ := gpdf.Open(filledFormPDF)
if err := doc.FlattenForms(); err != nil {
log.Fatal(err)
}
result, _ := doc.Save()
Flatten + Watermark
Flatten form fields and add a "FINAL" watermark.
doc, _ := gpdf.Open(filledFormPDF)
// Flatten forms
doc.FlattenForms()
// Add watermark
doc.EachPage(func(_ int, p *template.PageBuilder) {
p.Absolute(document.Mm(50), document.Mm(140), func(c *template.ColBuilder) {
c.Text("FINAL",
template.FontSize(72),
template.TextColor(pdf.Gray(0.9)),
)
})
})
result, _ := doc.Save()
Flatten + Page Numbers
Flatten form fields and add page numbers to every page.
doc, _ := gpdf.Open(filledFormPDF)
// Flatten forms
doc.FlattenForms()
// Add page numbers
count, _ := doc.PageCount()
doc.EachPage(func(i int, p *template.PageBuilder) {
p.Absolute(document.Mm(170), document.Mm(285), func(c *template.ColBuilder) {
c.Text(fmt.Sprintf("%d / %d", i+1, count),
template.FontSize(10),
template.AlignRight(),
)
}, template.AbsoluteWidth(document.Mm(20)))
})
result, _ := doc.Save()
Safe Flatten (No Forms)
FlattenForms() is safe to call on any PDF — it's a no-op if no form fields exist.
// Works on any PDF, with or without forms
doc, _ := gpdf.Open(anyPDF)
doc.FlattenForms() // no-op if no AcroForm
result, _ := doc.Save()