Aspose PDF Large File Size in version 18.7

Please how can this be fixed? I am using the code below and yet still experiencing the large file size of the pdf. Please see code below. Please note i am using a paid licence

 document.OptimizeResources(new Document.OptimizationOptions()
            {
                LinkDuplcateStreams = true,
                RemoveUnusedObjects = true,
                AllowReusePageContent = true,
                RemoveUnusedStreams = true,
                UnembedFonts = false,
                CompressImages = true,
                ImageQuality = 12,
                ResizeImages = true
            });
       
            // Save updated PDF
            document.Save($@"C:\MyPDF.pdf");

@sadogeri

Thanks for contacting support.

Would you please share your sample input PDF document with us. We will test the scenario in our environment and address it accordingly.

PDF Assets.zip (625.6 KB)

Thank you for getting back to me.

I realize if I add an image of size 300kb to the pdf document, the output PDF then increases to about 3mb, but as it is with the few images in the zipped folder attached it is 2mb, which in itself is too large. I have included the images as well as the generated output PDF for your analysis.

Regards
Sunday

@sadogeri

Thanks for sharing sample PDF document.

We have tested the scenario by optimizing your PDF document using Aspose.PDF for .NET 18.7 and same code snippet which you have shared. The size of PDF document was reduced from 2.5MB to 123KB only. SampleOutputPDF_out.pdf (122.8 KB)

While adding images inside a PDF, API adds operators and graphics inside PDF document which help in rendering the images while viewing PDF document. However, could you please share the code snippet, which you are using to add images inside your PDF document as well as the source PDF document. We will again test the scenario in our environment and address it accordingly.

AsposeDemo.zip (3.5 MB)

Hello Asad,
Thank you for the reply, I have attached a .Net Core solution to this message. The project AspoDemo.Test has a Program.cs which generates the PDF when ran.
There is a ResourceImagerHelper.cs file which reads in the image resources used to generated the PDF. Please let me know if there is anything else you might need from me. The location of the PDF generated will be in the Temp directory.

Regards
Sunday

@sadogeri

Thanks for sharing sample VS solution.

I am afraid that I was unable to find ResourceImageHelper.cs file in the solution. Could you please point us to right direction if something was overlooked in the shared solution.

Hi Asad,

Thanks for getting back to me the path to the file in the solution is
AsposeDemo\AsposeDemo.ReportEngine.Common\Helpers\Resource\ResourceImageHelper.cs

Or if its easier you easily do a search, but that is the path to the file.

Regards
Sunday

@sadogeri

Thanks for your response.

We have tested the scenario in our environment and observed that Document.OptimizeResources() method was not making any difference to the size of resultant PDF document. However, we would like to suggest you, please use Document.ProcessParagraphs() method before optimizing resources in document being generated.

Please note that in your scenario, PDF is being generated on fly and all allocated/used resources (i.e. images, text) become part of PDF when document is saved physically. Calling ProcessParagraphs() method is more likely saving a PDF document, so that Document object gets updated in the memory along with all used images and resources. Moreover, you may also save document into MemoryStream and re-initialize it with that stream before optimization of resources. These both ways, you will be able to obtain optimized PDF as output.

By following above suggested approaches, your code snippet in Program.cs would be as follows:

......................
......................
if (x.HasSmpi())
{
 var document = x.SmpiReport();
 //////////////////////////////////////////////////////////////////
 //MemoryStream ms = new MemoryStream();
 //document.Save(ms);
 //ms.Seek(0, SeekOrigin.Begin);
 //document = new Document(ms);
 /////////////////////////////////////////////////////////////////
 document.ProcessParagraphs();
 document.OptimizeResources(new Document.OptimizationOptions()
 {
  LinkDuplcateStreams = true,
  RemoveUnusedObjects = true,
  AllowReusePageContent = true,
  RemoveUnusedStreams = true,
  UnembedFonts = false,
  CompressImages = true,
  ImageQuality = 12,
  ResizeImages = true
 });

 // document.Optimize();
 // Save updated PDF
 document.Save($@"C:\Temp\SMPI_{clientCfg.GetClient()}_{x.ConfigName()}.pdf");
}
..................
..................

In case of further assistance, please feel free to let us know.

Thank you for the quick response I will give your suggestion a try and get back to you as quickly as possible.

Regards
Sunday

Thank you very much Asad, the solution you provided worked. Much appreciated and you have just made my day!

Regards
Sunday

@sadogeri

Thanks for your kind feedback.

Please keep using our API and in event of any further query, feel free to let us know.