mergeDocument method
Imports the existing objects into the new PDF document.
Implementation
@override
void mergeDocument(PdfDocument pdfDocument) {
_ensureXrefParsed();
final trailer = _trailerInfo ??
PdfParserXref.readTrailerInfoFromReader(reader, xrefOffset);
if (trailer.rootObj == null) {
pdfDocument.catalog = PdfCatalog(pdfDocument, PdfPageList(pdfDocument));
return;
}
final rootObj = _getObject(trailer.rootObj!);
if (rootObj == null || rootObj.value is! PdfDictToken) {
pdfDocument.catalog = PdfCatalog(pdfDocument, PdfPageList(pdfDocument));
return;
}
final rootDict = rootObj.value as PdfDictToken;
final pagesRef =
PdfParserObjects.asRef(rootDict.values[PdfNameTokens.pages]);
final pageList = PdfPageList(
pdfDocument,
objser: pagesRef?.obj,
objgen: pagesRef?.gen ?? 0,
);
pdfDocument.catalog = PdfCatalog(
pdfDocument,
pageList,
objser: rootObj.objId,
objgen: rootObj.gen,
);
PdfParserObjects.mergeDictIntoPdfDict(
pdfDocument.catalog.params,
rootDict,
ignoreKeys: const {PdfNameTokens.pages, PdfNameTokens.type},
);
if (pagesRef != null) {
// The `PdfPage` constructor already registers the page in
// `pdfDocument.pdfPageList`. Appending the result again duplicated
// every page: a two-page document came out with
// `/Kids [3 0 R 22 0 R 3 0 R 22 0 R] /Count 4`.
_loadPages(pagesRef, pdfDocument);
}
}