Guide
Filling a PDF form in bulk: not a mail merge
Updated
This distinction sounds pedantic until somebody spends a day building a merge template to reproduce a form that already exists and must not be redesigned.
The difference
A mail merge generates a document. The layout is yours, the content comes from data, and the output is whatever your template says.
Filling a form takes a PDF that already exists, with named fields in it, and writes values into those fields. The layout is fixed and not yours to change, which is usually the point: official forms have to look the way they look.
Fields or no fields
If the PDF has real form fields, the work is mapping your data to the field names, and every PDF library can write them.
If it is a flat PDF with lines printed on it, there are no fields to fill. You either add form fields first, which is a one-off piece of work on the source document, or you overlay text at fixed coordinates, which works until the issuing body reissues the form and moves everything by four millimetres.
Field names are the work
Look at them before writing anything. They are frequently strings assigned by whatever tool built the form: topmostSubform[0].Page1[0].f1_07[0] and similar.
Building the map from your data to those names is most of the job, and it is worth doing once into a named configuration rather than scattering the strings through code.
Flatten or leave editable
A filled form that still has live fields can be altered by anybody who opens it. Flattening converts the values to page content and removes that.
Which you want depends on whether the document is a record or a draft, and it is a decision worth making explicitly rather than accepting whichever your library does by default.