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

Free Support Forum - aspose.com

PNG Page Images to PDF Size Issue

Hi

I migrated some code that combined page images into a single PDF from Generator based code to the new Document DOM code, in the Generator code I had compressionLevel = 9, in the new code I tried the following as it gave a very similar quality:

        pdf.OptimizeResources(new Aspose.Pdf.Document.OptimizationOptions()
        {
            LinkDuplcateStreams = true,
            RemoveUnusedObjects = true,
            RemoveUnusedStreams = true,
            CompressImages = true,
            ImageQuality = 90
        });

However the resultant PDF was twice the size of the version created with Generator based code. All I’m doing is combining PNG page images into a single PDF so wondering if there are some optimisation tricks I’m missing?

Is an ImageQuality of 90 about equivalent to the previous CompressionLevel setting?

Thanks

Simon

I also tried dropping the ImageQuality to 10 and that had 0 impact, PDF was still 600+MB, am I maybe adding the files in a way that is stopping the image compression working, I have a loop over the images doing the following:

            var page = pdf.Pages.Add();
            using (FileStream fs = new FileStream(tempfile, FileMode.Open, FileAccess.Read))
            {
                byte[] tmpBytes = new byte[fs.Length];
                fs.Read(tmpBytes, 0, System.Convert.ToInt32(fs.Length));

                MemoryStream mystream = new MemoryStream(tmpBytes);

                Bitmap b = new Bitmap(mystream);

                page.PageInfo.Margin.Bottom = 0;
                page.PageInfo.Margin.Top = 0;
                page.PageInfo.Margin.Left = 0;
                page.PageInfo.Margin.Right = 0;

                page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

                Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

                page.Paragraphs.Add(image1);

                image1.ImageStream = mystream;
            }
        }

Bit confused as the Image quality setting seems to be having no effect.

Simon
PS: For info the PNG images used to generate the PDF have a total size of about 240MB, with Generator code the PDF was about 300MB using CompressionLevel 9

@simon.fairey,
Kindly send us your PNG images to replicate the problem of large sized PDF. We will investigate and share our findings with you. Your response is awaited.

Best Regards,
Imran Rafique

Hi

I’ve created a slightly smaller one 1665 pages, PNGs total about 124MB and with Quality at 10 the PDF is 342MB. The data is private and I know there used to a way to mark the post as private but can’t see how now. Also how and where should I upload the 120MB zip file containing the PNGs.

Thanks

Simon

@simon.fairey,
We have marked this thread as private and you can edit your first post to make thread private or public because there is a “private” check box. As the size of your files is large and you can upload a Zip of files to any free file sharer server (e.g., Dropbox and Google Drive) and then send its URL.

Best Regards,
Imran Rafique

Hi

On a hunch I tried saving the PDF to disk then re-opening it and trying the optimize again thinking there might be a bug in Aspose that meant it didn’t handle compression of the image streams properly so did this:

        pdf.Save(pdfTempfile, Aspose.Pdf.SaveFormat.Pdf);

        var optimizedPdf = new Aspose.Pdf.Document(pdfTempfile);
        optimizedPdf.OptimizeSize = true;
        optimizedPdf.OptimizeResources(new Aspose.Pdf.Document.OptimizationOptions()
        {
            RemovePrivateInfo = true,
            AllowReusePageContent = true,
            LinkDuplcateStreams = true,
            RemoveUnusedObjects = true,
            RemoveUnusedStreams = true,
            CompressImages = true,
            ImageQuality = 50,
        });
        var optimizedPdfTempFile = Path.GetTempFileName();
        optimizedPdf.Save(optimizedPdfTempFile);

This works, running the OptimizeResources() on the PDF before it was first saved didn’t work but optimizing the PDF once you open it fresh does work.

Not sure if you want to class this as a bug or not but think it’s an important point for others who might try it so changed the thread back to public in case it helps others.

Thanks

Simon

Finally worked out why the optimize wasn’t working, after adding the mage to a paragraph you need to run:

pdf.ProcessParagraphs();

Now when you run OptimizeResources it works and you don’t need the intermediate Save step, think this needs documenting :slight_smile: would have saved me hours of bother!

Cheers

Si

@simon.fairey,

Thanks for sharing the details.

From above statement, do you mean when you call OptimizeResources(…) method, there is no impact unless you save the document. re-open it and apply Optimization. Please acknowledge so that we can further look into this matter.

@simon.fairey,
We have managed this behavior in our environment using our own image file. It has been logged under the ticket ID PDFNET-42963 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates. Furthermore, we recommend you please also share your images, so that could replicate the same scenario as you are facing in your environment. We are sorry for the inconvenience caused.