Page-Tree Grafting and Object Renumbering
Merging PDFs is not byte concatenation. Pages hang from a /Pages tree whose /Kids arrays carry /Parent links, and every indirect object is numbered so the trailer's /Root can reach it. The merger parses each source, copies each page's object graph into one document, renumbers objects, then writes a new xref table and trailer. Raw concatenation usually fails because the second file's offsets and object numbers collide.
Page order comes from /Kids, not from file order, and /Count must match reachable leaves. Imported pages get a rewritten /Parent, while inheritable /Resources, /MediaBox and /Rotate are materialized when the old parent disappears. Destinations and page labels that referenced old indices are remapped. Linearized files and incremental updates are rebuilt, discarding the original byte layout.