How can I crop the pages of an existing PDF document?

I have an existing document of which the pages are too big. How can I crop the pages? Posted on StackOverflow on Apr 12, 2014 by choudhary You need to get the /MediaBox and /CropBox value fr

I have an existing document of which the pages are too big. How can I crop the pages?

Posted on StackOverflow on Apr 12, 2014 by choudhary

You need to get the /MediaBox and /CropBox value from the page dictionary. In iText 7 this can be done in the following way:

PdfPage page = pdfDoc.getPage(pageIndex);
Rectangle cropBox = page.getCropBox();
Rectangle mediaBox = page.getMediaBox();

In many cases, cropbox will be null in which case you can safely ignore it and use the mediabox value instead.

The cropbox value (or if null, mediabox) is a Rectangle object with x and y coordinates and width and height values. If you want to crop a page, you need to change these coordinates and either replace the existing cropbox value (if one already exists) or add a new cropbox value (if there is none):

page.setCropBox(new Rectangle(newX, newY, newWidth, newHeight));

Click this link if you want to see how to answer this question in iText 5.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now