This is a sample Java program showing how to crop a page in Java using Qoppa’s PDF library jPDFProcess.
This small snippet shows how to open a PDF, get the first page, set a new crop box and save the cropped PDF to a new file name.
// Open PDF file PDFDocument pdfDoc = new PDFDocument("C:\\test\\mydocument.pdf", null); // Get first page PDFPage page = pdfDoc.getPage(0); // Define the new crop box, which is a rectangle that is used to crop content // before displaying or printing the page. This rectangle is in PDF native // coordinates starting at the bottom left and increasing up to the right. // The dimensions are given in PostScript points. // 1 inch = 72 points, 1cm = 28.3465 points, 1mm = 2.8346 points // width in points double crop_width = 200; // height in points double crop_height = 300; // bottom left corner coordinates in points double x_1 = 20; double y_1 = 20; Rectangle2D.Double area = new Rectangle2D.Double(x_1, y_1, crop_width, crop_height); // set the new crop box page.setCropBox(area); // save the cropped document pdfDoc.saveDocument("C:\\test\\mydocument_cropped.pdf"); |
When cropping a PDF, the actual content is still present but hidden. Only the display or print area is changed.