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 How can I crop the pages of an existing PDF document? if you want to see how to answer this question in iText 5.