Free Support Forum - aspose.com

Maintain the original Media/Crop/Bleed/Art boxes while concatenating PDFs using Aspose.PDF

I am concatenating multiple PDF files together. The dimensions of each input PDF file is out of my control.

Code examples are a little tricky as I am actually working in Clojure but I can give something close.

So right now imagine I am creating an inital page in a blank document. I then iterate over additional documents and begin adding their pages into the new document. This appears to work fine when pages are all A4 but I have an input which has a custom MediaBox.

Document base = new Document();
// For this demo I'm not going to use this
Page initialPage = base.getPages().add();

// Now I want to add an unusual landscape page
Page landscape = base.getPages().add();
// And give it a large landscape media box
Rectangle mediaBox = new Rectangle(0d, 0d, 1200d, 500d);
landscape.setMediaBox(mediaBox);

// once saved the media box changes and
// 2 portrait pages remain with media boxes
// of (0,0) (595, 842)
base.save("path/to/output.pdf");

How can I maintain the original Media/Crop/Bleed/Art boxes?

edit: I am using version 20.10 (java)

@dcoxall

You may please check following article(s) in the API documentation which demonstrate different methods to change page dimension, orientation and content size.

In case you are still unable to achieve your requirements, please share your sample source PDF file(s) with us along with an expected output PDF document. We will test the scenario in our environment and address it accordingly.

Okay so this is resolved by me taking the Crop/Bleed/Trim/Art Boxes and the Page Rectangle from the original page and setting it on the page after adding it to the new Document. The documentation around concatenating documents lacks any mention that the page sizes are modified which feels rather counter intuitive.

@dcoxall

It is good to know that your issue has been resolved. Furthermore, the documentation article(s) for PDF concatenation only display the information which covers the merging process generally. In a scenario where some custom operations or modifications are required during/after the concatenation, you can refer to other related articles in the documentation according to your needs. Please feel free to let us know in case you need further assistance.