ketbra ketbra - 1 year ago 306
Java Question

Scaling a PDF page using PDFBox

I'm trying to convert PDF pages to

instances using the Apache PDFBox library. Why is the following code not working?

try {
PDDocument doc = PDDocument.load(pdfFile); // pdfFile is file path to pdf
List<PDPage> pgeLst = document.getDocumentCatalog().getAllPages();

for(PDPage pge : pgeLst) {
final PDRectangle mediaBox = pge.getMediaBox();
mediaBox.setUpperRightX(mediaBox.getUpperRightX() * scale);
mediaBox.setUpperRightY(mediaBox.getUpperRightY() * scale);

BufferedImage img = page.convertToImage(); // Image should now be scaled,
// but isn't.

} catch(Exception e) { e.printStackTrace(); }

Maybe the solution is very easy, though I searched the web and the JavaDoc and didn't find any solution.

I'm using PDFBox version 1.8.8, precisely
Thank you Tilman Hausherr for pointing that out :)

Thanks in advance :)

Answer Source


BufferedImage img = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300);

in PDFBox 1.8.*, this will render at 300dpi. The default (which you used) is 144dpi. So use a dpi parameter that is larger or smaller than 144 depending on what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download