Resize PDF page using Java and Aspose.PDF | Reduce the page dimension in PDF file

Hi,

Our organization uses aspose licensed version. One issue we are facing right now related to pdf file dimension .we want to reduce dimension of pdf file in java code. I have tried with used code but not worked. I would like to reduce pdf file dimension to A4 size. Can you please help? pdf attached.

String pdfOut = pdfIn.substring(0, pdfIn.length() - 4) + "_resize.pdf";


	// load the source PDF document
	Document document = new Document(pdfIn);

	// get page to trim white space
	com.aspose.pdf.Page pdfPage = ((IDocument) document).getPages().get_Item(1);

	// get the content boundaries
	com.aspose.pdf.Rectangle contentBBox = pdfPage.calculateContentBBox();

	// set Page CropBox and MedioBos as per content boundries to tirm white space
	pdfPage.setCropBox(contentBBox);
	pdfPage.setMediaBox(contentBBox);

	// save the resultant PDF
	document.save(pdfOut); 
    return pdfOut;

Pdf dimension:- 8.50 * 11 in

<a class="attachment" href="/uploads/default/50359">sample.pdf</a> (237.3 KB)

sample.pdf (237.3 KB)

@DIN001

Please use the below code snippet in order to resize PDF Page using Java and Aspose.PDF:

Document pdfDocument = new Document(dataDir + "sample.pdf");
com.aspose.pdf.facades.PdfFileEditor.ContentsResizeParameters parameters = new com.aspose.pdf.facades.PdfFileEditor.ContentsResizeParameters(
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(0),
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(PageSize.getA4().getWidth()),
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(0),
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(0),
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(PageSize.getA4().getHeight()),
 com.aspose.pdf.facades.PdfFileEditor.ContentsResizeValue.units(0)
);
com.aspose.pdf.facades.PdfFileEditor editor = new com.aspose.pdf.facades.PdfFileEditor();
editor.resizeContents(pdfDocument, parameters);

// Save output as PDF format
pdfDocument.save(dataDir + "output.pdf");

output.pdf (228.9 KB)

Thanks asad for the code. However i am facing issue , it reduces the size of first 4 pages of the pdf doc ,but it did not reduces the size of further pages. (Resize pdf attached).
Can you please suggest?1620135466972_resize.pdf (273.3 KB)

@DIN001

It seems like you are using trial version of the API and due to its restriction, you cannot process more than 4 elements of any collection (Page, Annotation, etc.). Please try using the valid license while using the API and if you do not have one, you can apply for free 30-days temporary license and use it. In case you face some issue, please feel free to let us know.
output.pdf (274.7 KB)

Thanks Asad for the reply. Yes we are using trial version as of now.