PDFBox LayerUtility - Import layers into an existing PDF file

I use pdfbox to manage pdf content. I have a large PDF file (say, 500 pages). I also have several other one-page PDF files containing only one image of about 8-15 kilobytes in size per max file. What I need to do is import these one-page PDF files as overlay on certain pages of a large PDF file.

I tried LayerUtility pdfbox where I succeeded, but it creates a very large file size as output. The pdf source is about 1 MB before processing, and when added with smaller files in PDF format, the size increases to 64 MB. And sometimes I need to include two small PDF files into a larger one.

Is there a better way to do this, or am I just doing it wrong? The code post below tries to add two layers to one page:

... ... .. overlayDoc[pCounter] = PDDocument.load("data\\" + overlay + ".pdf"); outputPage[pCounter] = (PDPage) overlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0); LayerUtility lu = new LayerUtility( overlayDoc[pCounter] ); form[pCounter] = lu.importPageAsForm( bigPDFDoc, Integer.parseInt(pageNo)-1); lu.appendFormAsLayer( outputPage[pCounter], form[pCounter], aTrans, "OVERLAY_"+pCounter ); outputDoc.addPage(outputPage[pCounter]); mOverlayDoc[pCounter] = PDDocument.load("data\\" + overlay2 + ".pdf"); mOutputPage[pCounter] = (PDPage) mOverlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0); LayerUtility lu2 = new LayerUtility( mOverlayDoc[pCounter] ); mForm[pCounter] = lu2.importPageAsForm(outputDoc, outputDoc.getNumberOfPages()-1); lu.appendFormAsLayer( mOutputPage[pCounter], mForm[pCounter], aTrans, "OVERLAY_2"+pCounter ); outputDoc.removePage(outputPage[pCounter]); outputDoc.addPage(mOutputPage[pCounter]); ... ... 
+4
source share
1 answer

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.)

+3
source

All Articles