How to merge multiple PDF documents
pdfutil.MergePdf concatenates a list of files into one: every page of the first
input, then every page of the second, and so on.
import "github.com/unidoc/unipdf/v5/pdfutil"
err := pdfutil.MergePdf([]string{"input1.pdf", "input2.pdf"}, "output.pdf", true)The third argument is isMergingForms. Set it to true when any input has an
AcroForm; passing false there leaves the widget annotations on the merged pages
but drops the form that ties them together, so the fields stop working. The
godoc’s advice is to set it to true if you are unsure.
If the inputs are already in memory, MergePdfStreams(inputs []io.ReaderAt, output io.Writer, isMergingForms bool) does the same thing without touching
disk. Note the io.ReaderAt: a bytes.Buffer does not qualify, so wrap bytes
in bytes.NewReader.
Merging carries pages and, optionally, the AcroForm. Nothing else. Outlines, document info, XMP metadata, page labels and named destinations belong to the document rather than to a page, and the merge builds a fresh writer that never receives them. Bookmarks vanishing from a merged file is the usual first surprise.
Form merging also renames fields. The first document’s fields keep their names;
each later document’s fields are re-parented under doc1, doc2 and so on, so
a field named name in the second input becomes doc1.name in the output.
No optimizer runs by default, so a font embedded in twenty inputs is written
twenty times. MergePdfWithOptions takes a MergePdfOptions with an
Optimizer field for that case.
Full treatment, including the stream variants and the memory cost, in Merge PDFs.