You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Mark Gibson <ma...@staff.bluematrix.com> on 2019/11/13 12:09:09 UTC

What is the best way to the page contents of one PDF in to an existing page in another PDF

Hi,

I've been trawling t'internet for suggestions on how to merge two PDF files.  Specifially, take a page from PDF A, and write the contents over an existing page in PDF B.

I found org.apace.pdfbox.multipdf.LayerUtility, and had initially started using that.  However, we had a situation where both PDFs were landscape, but PDF A had properties (width vs height) which where in portrait, with an added 90 degrees rotation.  When using LayerUtility, it ended up messing the aspect ratio.  The rotation was correct, but was squidged in to portrait width, instead of landscape width.

I'm a complete novice working at this level with PDFs, and am on a steep learning curve here :)  So, grabbing at straws, I'm wondering if the scaling in LayerUtility is to blame:

        case 90:
            at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
            at.translate(0, viewBox.getWidth());
            at.rotate(-Math.PI / 2.0);
            break;

As part of my journey, I then also found org.apache.pdfbox.multipdf.Overlay.  This seems to be exactly what I need.  And testing in isolation, seems to do what we need.  It would, however, require reworking by me (a rewrite to a new class, rather than wanting to change what's there) because we need to target specific pages of PDF A to specific pages in PDF B.

Can anyone comment please on logically which implementation they would prefer for our usecase, and explain why?

Thanks
Mark