With code like the one below, I see no irreversible size increase:
PDDocument bigDocument = PDDocument.load(BIG_SOURCE_FILE); LayerUtility layerUtility = new LayerUtility(bigDocument); List bigPages = bigDocument.getDocumentCatalog().getAllPages(); // import each page to superimpose only once PDDocument firstSuperDocument = PDDocument.load(FIRST_SUPER_FILE); PDXObjectForm firstForm = layerUtility.importPageAsForm(firstSuperDocument, 0); PDDocument secondSuperDocument = PDDocument.load(SECOND_SUPER_FILE); PDXObjectForm secondForm = layerUtility.importPageAsForm(secondSuperDocument, 0); // These things can easily be done in a loop, too AffineTransform affineTransform = new AffineTransform(); // Identity... your requirements may differ layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "Superimposed0"); layerUtility.appendFormAsLayer((PDPage) bigPages.get(1), secondForm, affineTransform, "Superimposed1"); layerUtility.appendFormAsLayer((PDPage) bigPages.get(2), firstForm, affineTransform, "Superimposed2"); bigDocument.save(BIG_TARGET_FILE);
As you can see, I overlaid the first page of FIRST_SUPER_FILE on two pages of the target file, but I only imported the page once. Thus, the resources of the imported page are imported only once.
This approach is also open to loops, but does not import the same page multiple times ! Instead, import all the necessary template pages before they are presented as forms, and in a later cycle, these forms again and again.
(I hope this solves your problem. If not, put more code and sample PDF files to reproduce your problem.)
source share