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

Free Support Forum - aspose.com

PDF to PDF conversion with image downsampling

Hi,

I’d like to know if it’s possible to save a PDF file into another one, downsampling images.

Thanks.

Hi Gamboni,


Thanks for using our API’s.

As per my understanding, you want to optimize existing PDF document and save it to a new PDF document. Please use following sample code to optimize the images in the PDF document and save it to another PDF document.

Java
Document pdfDocument = new Document( “Test1.pdf”);
// Optimize for web
pdfDocument.setOptimizeSize(true);
// Optimzie the file size by removing unused objects
Document.OptimizationOptions opt = new Document.OptimizationOptions();
opt.setRemoveUnusedObjects(false);
opt.setLinkDuplcateStreams(false);
opt.setRemoveUnusedStreams(false);
// Enable image compression
opt.setCompressImages(true);
// Set the quality of images in PDF file
opt.setImageQuality(10);
opt.setImageQuality(50);
opt.setMaxResoultion(75);;

pdfDocument.save(“OptimizeDocument_out11.pdf”);

You can also check Optimize PDF document page on our documentation for more details. If you still face any issue, please feel free to contact us.

Best Regards,

Hi,

thanks for your answer, but it’s not what I’m looking for (though I’ll consider to add this code to my solution).

I wrote following code that resizes images larger than 16 MPixel to something lower:

<span style=“font-family: “Courier New”;”>c<span style=“font-family: “Courier New”;”>om.aspose.pdf.Document doc = new Document(new ByteArrayInputStream(blobBytes));
com.aspose.pdf.PageCollection pages = doc.getPages();
for(int i = 1; i <= pages.size(); i++) {
com.aspose.pdf.Page page = pages.get_Item(i);
com.aspose.pdf.XImageCollection images = page.getResources().getImages();
for(int j = 1; j <= images.size(); j++) {
com.aspose.pdf.XImage image = images.get_Item(j);
int height = image.getHeight(), width = image.getWidth();
final double limit = Math.pow(2, 24)-1;
if(height * width >= limit) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.save(baos, 10, 10);
doc.getPages().get_Item(i).getResources().getImages().replace(j, new ByteArrayInputStream(baos.toByteArray()));
}
}
}
<div style=“font-family: “Courier New”;”>ByteArrayOutputStream pdfOs = new ByteArrayOutputStream();
<div style=“font-family: “Courier New”;”>doc.save(pdfOs);<div style=“font-family: “Courier New”;”>
Is this the fastest way to replace images inside a PDF file with downsampled versions?

Do you think is better to put your optimization code after or before the downsampling?

Thanks and best regards.

Another question: can you explain me what unit of measure are used in following functions?


- setMaxResoultion(number)
- save(stream, dpi_x, dpi_y)

Thanks.

Hi Gamboni,


Thanks for sharing the details and sorry for the delayed response.

The earlier shared optimization code can be added to further reduce the size of PDF document but the above shared code snippet shows the steps to reduce/update the dimensions of images inside PDF file. May be you can first update the dimensions and then perform size optimization to generate desired output.

In case you still face any issue while using our API, please share the input PDF files, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Gimbo71:
Another question: can you explain me what unit of measure are used in following functions?

- setMaxResoultion(number)
- save(stream, dpi_x, dpi_y)
Hi Gamboni,

The unit for these methods is Point.

Please note that the basic unit for our API to measure page height and width properties is points, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points.

Converting Points to Pixels

The conversion from point to pixel depends on an image’s DPI (dots per inch) property. For example, if an image’s DPI is 96 (96 pixels for each inch), and it is 100 points high, its height in pixels is (100 / 72) * 96 = 133.3. The general formula is: pixels = ( points / 72 ) * DPI.