Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Jul 31, 2023 12:23 pm

Hi,

I have an issue when I extract an image from a pdf, reduce the quality and then replace the old image with the new in the pdf to compress the pdf.
Some image are just upside down or cut in half, etc... I don't know why this is happening.
Here is my code :

Code: Select all
doc.loadFromFile(filename);
doc.getFileInfo().setIncrementalUpdate(false);
doc.setCompressionLevel(PdfCompressionLevel.Best);
for (int i = 0; i < doc.getPages().getCount(); i++) {
      PdfPageBase page = doc.getPages().get(i);
      PdfImageInfo[] images = page.getImagesInfo();
      if (images != null && images.length > 0) {
            for (int j = 0; j < images.length; j++) {
                  PdfImageInfo image = images[j];
                  PdfBitmap bp = new PdfBitmap(image.getImage());
                  bp.setQuality(8);
                  page.replaceImage(j, bp);
                  page.tryCompressImage(image.getIndex());
            }
      }
}
doc.saveToFile("output/" + name + "Compressed_by_Spire.pdf", FileFormat.PDF);
doc.close();


I use free spire.pdf version 5.1.0.
My pdf contains some private data, do you have an email where I can send you the uncorrect pdf ?

Thank you !

Sarah_JS
 
Posts: 1
Joined: Mon Jul 31, 2023 12:12 pm

Tue Aug 01, 2023 1:30 am

Hello,

Thanks for your inquiry.
You could send your PDF file to us via email ([email protected]). Thanks in advance.

Sincerely,
Wenly
E-iceblue support team
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Tue Aug 01, 2023 10:24 am

Hello,

Thanks for sharing the information via email.
I use the free version and the latest commercial version of Spire.PDF and both reproduced the issue of image rotation after reducing image quality and compressing PDF files, I have logged the issue into our bug tracking system with the ticket number SPIREPDF-6208. Our development team will investigate and fix it. Once it is resolved, I will inform you in time. We apologize for the inconvenience caused.
Additionally, as we only regularly maintain the commercial version, the fix for this issue is only included in the commercial version. Hope you can understand.

Sincerely,
Wenly
E-iceblue support team
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Tue Aug 08, 2023 3:49 am

Hello,

Thank you for your patience.
After our development team's research on SPIREPDF-6208, it was found that the original images on the second and fourth pages of the PDF document you provided were themselves flipped 180 degrees.
attribute.jpg

To prevent this situation, the page itself was also flipped, so the source PDF document is displayed normally in Adobe. The image extracted by page. getImagesInfo() is the image presented in the PDF, but after replacing the image with page. replaceImage (j, bp), the image was flipped 180 degrees due to the rotation of the page itself. So this is not a bug in our product, to avoid this situation, you can use the following code:
Code: Select all
        PdfDocument doc=new PdfDocument();
        doc.loadFromFile("F:\\Original.pdf");
        doc.getFileInfo().setIncrementalUpdate(false);
        doc.setCompressionLevel(PdfCompressionLevel.Best);
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            PdfPageBase page = doc.getPages().get(i);
            //The image extracted by this method is the original image, not the image state presented in the PDF
            BufferedImage[] images=page.extractImages(false);
            if (images != null && images.length > 0) {
                for (int j = 0; j < images.length; j++) {
                    PdfBitmap bp = new PdfBitmap(images[j]);
                    bp.setQuality(8);
                    page.replaceImage(j,bp);
                    page.tryCompressImage(j);
                }
            }
        }
        doc.saveToFile("F:\\Result.pdf", FileFormat.PDF);

If you have any other questions or concerns, please feel free to contact us anytime.

Sincerely
Wenly
E-iceblue support team
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Return to Spire.PDF

cron