I have code like this
Document pdfDocument = new Document(“input.pdf”);
for (Page page : pdfDocument.getPages()) {
double originalWidth = page.getPageRect(false).getWidth();
double originalHeight = page.getPageRect(false).getHeight();
// Skip scaling and just crop if either dimension is 0.
if (originalWidth == 0 || originalHeight == 0) {
continue;
}
double targetWidth = PageSize.getA4().getWidth();
double targetHeight = PageSize.getA4().getHeight();
// Only scale as much as necessary. Maintain aspect ratio.
double widthScale = (originalWidth<originalHeight? targetWidth: targetHeight) / originalWidth;
double heightScale = (originalWidth<originalHeight? targetHeight: targetWidth) / originalHeight;
double scale = 1.0d;
if (widthScale <= heightScale) {
scale = widthScale;
} else {
scale = heightScale;
}
//nicht vergrößern
if (scale > 1) continue;
//Minimale Abweichungen zulassen
if (scale > 0.90) continue;
double destWidth = originalWidth * scale;
double destHeight = originalHeight * scale;
PdfFileEditor.ContentsResizeParameters par = PdfFileEditor.ContentsResizeParameters.pageResize(destWidth, destHeight);
com.aspose.pdf.facades.PdfFileEditor pfe = new com.aspose.pdf.facades.PdfFileEditor();
pfe.resizeContents(pdfDocument, new int[] {page.getNumber()}, par);
}
// Save output as PDF format
pdfDocument.save("output.pdf");
It works as expected for most files. But with one pdf svg_00808043_S_1_B-Plan_GolfparkWeiherhof_Erweiterung.pdf (1.6 MB) the Content is shifted outside the pdf page. Could you please give me a hint, what is going wrong. I think it has to do with the CropBox of the input.pdf.
Thanks