Convert PDF to PDF and reducing image size

Hi support,

I’m trying to convert a PDF with big image (3000*3000 at least) to another PDF and reduce DPI. I’ve followed your documentation about it (Working with Images in PDF|Aspose.PDF for Java) but in the produced PDF, some images has been transform in grey background and the others are really ugly.

I think what the problem come from image extraction.

Please find below, code which produce the result :
this.document.convert(streamLogger, PdfFormat.PDF_A_1A, ConvertErrorAction.None);
this.document.validate(streamLogger, PdfFormat.PDF_A_1A.getPdf());

OptimizationOptions optimizeOptions = new OptimizationOptions();
optimizeOptions.setRemoveUnusedObjects(true);
optimizeOptions.setLinkDuplcateStreams(true;
optimizeOptions.setRemoveUnusedStreams(true);
optimizeOptions.setCompressImages(true);
optimizeOptions.setImageQuality(60);
this.document.optimizeResources(optimizeOptions);

for (Page page : this.document.getPages()) {
for (XImage image : page.getResources().getImages()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.save(os, 2500, 2500);
image.replace(new ByteArrayInputStream(os.toByteArray()));
}
}

Florian

Hi Florian,


We are sorry for the inconvenience. I am afraid I am unable to open your shared documents, their file type is unknown. We will appreciate it if you please share your sample input/output documents here, so we will test the scenario and will guide you accordingly.

Best Regards,

I’m apoligizing, I’m working on Linux and I always forgot format.
Open it with an image viewer.

I’ve attached images with extension.

Regards,

Florian

Hi Florian,


Thanks for sharing the source documents. We will appreciate it if you please share your input/output PDF document here as well, it will help us to test your scenario exactly.

However I have created a PDF with your shared image and tested your sample code, but I am unable to notice the reported issue.

We are sorry for the inconvenience.

Best Regards,

Hi Tilal,

Please find below the original document.

Regards,

Florian

Hi Florian,


Thanks for sharing the sample document. I have tested the shared PDFA document as following on both Windows and Linux with Aspose.Pdf for Java 11.7.0, but unable to notice the reported issue. If you are using some old version then please download and try latest version of Aspose.Pdf for Java, hopefully it will resolve the issue. However if the issue persist then please share some more details(e.g. environment details), so we will try to investigate it further.

// Internal image resolution change<o:p></o:p>

Document doc = new Document("D:\\Downloads\\PDF+with+big+images.pdf\\PDF with big images.pdf");

OptimizationOptions optimizeOptions = new OptimizationOptions();

optimizeOptions.setRemoveUnusedObjects(true);

optimizeOptions.setLinkDuplcateStreams(true);

optimizeOptions.setRemoveUnusedStreams(true);

optimizeOptions.setCompressImages(true);

optimizeOptions.setImageQuality(60);

doc.optimizeResources(optimizeOptions);

for (Page page : doc.getPages()) {

for (XImage image : page.getResources().getImages()) {

ByteArrayOutputStream os = new ByteArrayOutputStream();

image.save(os, 2500, 2500);

image.replace(new ByteArrayInputStream(os.toByteArray()));

}

}

doc.save(myDir + "imageWithNewResolution.pdf");


We are sorry for the inconvenience.

Best Regards,

Hi Tilal,

Thanks for your quick answer.

I’ve uploaded the wrong file, please find it here : MEGA
Document have a size near 60Mb and I can’t upload it on the forum.


Original document is a ODT and I’ve convert it into PDF without compression.
In my mind, the document produced by Open Office has something wrong and when Aspose open it, images are corrupted. Or in somes cases, Aspose cannot read properly images in PDF.

I have another questions about resizing, the save method with this parameters :

Parameters:
stream - OutputStream where image will be saved
xDpi - Image horizontal resolution
yDpi - Image vertical resolution
need the size in pixels or in DPI ? In fact, method getHeight and getWidth return the size in pixels.




Thanks in advance,

Regards,

Florian



Hi Florian,

florian.melot:


I've uploaded the wrong file, please find it here : https://mega.nz/#!tdRiBJIL
Document have a size near 60Mb and I can't upload it on the forum.


Original document is a ODT and I've convert it into PDF without compression.
In my mind, the document produced by Open Office has something wrong and when Aspose open it, images are corrupted. Or in somes cases, Aspose cannot read properly images in PDF.

.
I am afraid I am unable to download file, it is asking for decryption key, please share the encryption key or share it via some other file sharing service.

florian.melot:

I have another questions about resizing, the save method with this parameters :
Parameters:
stream - OutputStream where image will be saved
xDpi - Image horizontal resolution
yDpi - Image vertical resolution
need the size in pixels or in DPI ? In fact, method getHeight and getWidth return the size in pixels.


I am looking into your query and will update you shortly.

Best Regards,

Oups sorry, new features on Mega :slight_smile:

This is the key : !VKMpp7bUWO0uTbArqW4Q7TzJhJ63b4QMEwbiOZCFwbM

Regards,

Florian

Hi Florian,

florian.melot:

Thanks for your quick answer.

I've uploaded the wrong file, please find it here : https://mega.nz/#!tdRiBJIL
Document have a size near 60Mb and I can't upload it on the forum.


Original document is a ODT and I've convert it into PDF without compression.
In my mind, the document produced by Open Office has something wrong and when Aspose open it, images are corrupted. Or in somes cases, Aspose cannot read properly images in PDF.


Thanks for sharing the sample PDF, I have manged to notice the distorted images after reducing it size, so logged a ticket PDFJAVA-36018 in our issue tracking system for further investigation and rectification. We will keep you updated about the issue resolution.

florian.melot:

I have another questions about resizing, the save method with this parameters :
Parameters:
stream - OutputStream where image will be saved
xDpi - Image horizontal resolution
yDpi - Image vertical resolution
need the size in pixels or in DPI ? In fact, method getHeight and getWidth return the size in pixels.

It is DPI. Please note Monitors do not have dots, but pixels. The closely related concept for monitors and images is pixels per inch or PPI (https://en.wikipedia.org/wiki/Pixel_density).

Please feel free to contact us for any further assistance.

Best Regards,

The issues you have found earlier (filed as PDFJAVA-36018) have been fixed in Aspose.Pdf for Java 11.9.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.