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

Free Support Forum - aspose.com

Unable to get OptimizeResources to work on PDFs

I am working in .NET 4.5.2 using Aspose.PDF 18.1… Whenever I generate a pdf or combine multiple pdfs and try to reduce the filesize, it seems that the OptimizeResources options are never applied to the document. I’ve tried adding the optimization options to a newly initialized empty document. I have tried to apply them after the document has been populated. Nothing works. Any help would be greatly appreciated.

Here is the current code I have for Merging multiple pdfs together after being created earlier in this file:

            {
                //Dictionary containing all the pdfs created in this session
                var cdrlFiles = (Dictionary<int, string>)HttpContext.Current.Session["print-cdrls-pdf"];

                //initialize variable for each pdf while being iterated
                Aspose.Pdf.Document cdrlFile;

                // We now need to merge them all together. To do this we will add all the CDRLs to one PDF
                var existPdf = new Aspose.Pdf.Document();

                foreach (var file in cdrlFiles.OrderBy(kvp => kvp.Key))
                {
                    var cs = new MemoryStream((byte[])HttpContext.Current.Session[file.Value]);
                    cdrlFile = new Document(cs);

                    cdrlFile.OptimizeResources(new Document.OptimizationOptions()
                    {
                        LinkDuplcateStreams = true,
                        RemoveUnusedObjects = true,
                        RemoveUnusedStreams = true,
                        CompressImages = true,
                        ImageQuality = 10
                    });
                    // Add the pages of the source documents to the target document
                    existPdf.Pages.Add(cdrlFile.Pages);
                }

                // Finally save the combined file back to the session to be presented to the user
                using (var ms = new MemoryStream())
                {
                    existPdf.Save(ms);
                    HttpContext.Current.Session["pdf-file"] = ms.ToArray();
                }

                cdrlFiles.Clear();
                HttpContext.Current.Session["print-cdrls-pdf"] = null;

                return true;
            }

Thank you!

@mwolf,

Please build the cdrlFiles instance in the code, and share all files which you are retrieving from the Session print-cdrls-pdf. We will investigate your scenario in our environment, and share our findings with you.

Okay. I will try to do that today. In the mean time can you tell me if anything looks wrong with how I am using OptimizeResources in the above code?

Thanks!

@mwolf,

The size of PDF document should be reduced, if you have not already optimized. The source code looks fine, and we need to investigate your scenario in our environment. Your response is awaited.

Thank you for the reply. I was able to greatly reduce the file size by adding these two options that I could not find in your documentation, but found in the OptimizationOptions class.

AllowReusePageContent = true
UnembedFonts = true

These should be okay to use, correct?

@mwolf,

If the fonts are not embedded in the PDF document, then the System where users will open this document should have all required fonts. Both these options look fine, and could be incorporated to reduce the size of PDF document.