We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Optimize PDF and resize content to fit A4

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) {

	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

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.



Would you kindly share an expected output PDF with us. We will test the scenario in our environment and address it accordingly.