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

Free Support Forum - aspose.com

How to pass optimization modes in Aspose.PDF .NET

Hi,
I am using Aspose.PDF .NET Latest Version 20.8.0 Added from nuget package

I am unable to find how to pass optimization modes (low,medium,high) or compress file size

So, i tried following way, but file size is not decreasing , but same file when uploaded in your site


File size is decreasing(actual size is 9 MB), when Low - 8.7 MB, Medium - 6.7 MB, High - 2 MB

                   `string sourceFilename = "E:\\TestPDF.pdf";
                    string targetFilename = "E:\\Test.pdf";
                    var pdfDocument = new DocumentAspose(sourceFilename);

                    if (mode == _low)
                    {
                        pdfDocument.Optimize();
                    }
                    else if (mode == _medium)
                    {
                        pdfDocument.OptimizeResources(new Aspose.Pdf.Optimization.OptimizationOptions()
                        {
                            LinkDuplcateStreams = false,
                            RemoveUnusedObjects = false,
                            RemoveUnusedStreams = true,
                            UnembedFonts = false,

                        });
                    }
                    else if (mode == _high)
                    {
                        var optOptions = new OptimizationOptions
                        {
                            LinkDuplcateStreams = true,
                            RemoveUnusedObjects = true,
                            RemoveUnusedStreams = true,
                            UnembedFonts = false,
                        };
                        optOptions.ImageCompressionOptions.CompressImages = true;
                        optOptions.ImageCompressionOptions.ImageQuality = 50;
                        optOptions.ImageCompressionOptions.MaxResolution = 900;
                        optOptions.ImageCompressionOptions.ResizeImages = true;

                        pdfDocument.OptimizeResources(optOptions);

                    }

                    pdfDocument.Save(targetFilename);`

As mentioned in document

com.aspose.pdf.optimization.OptimizationOptions opt = new com.aspose.pdf.optimization.OptimizationOptions();
opt.setRemoveUnusedObjects(false);
opt.setLinkDuplcateStreams(false);

I don’t see above methods.So i searched in forum and tried like above

Please let me know, what is wrong and why i am not able to decrease file size

Thanks

@srikanthgupta.maple

Could you please share the sample PDF file with us. We will test the scenario in our environment and address it accordingly.

Hi, please check attached file which i used

TestPDF.pdf (9.0 MB)

Thanks

@srikanthgupta.maple

Please use following code snippet to optimize the PDF document:

Document doc = new Document(dataDir + @"TestPDF.pdf");
var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizationOptions.RemoveUnusedObjects = true;
optimizationOptions.RemoveUnusedStreams = true;
optimizationOptions.AllowReusePageContent = true;
optimizationOptions.LinkDuplcateStreams = true;
optimizationOptions.UnembedFonts = true;
optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 30;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
optimizationOptions.ImageCompressionOptions.Version = Aspose.Pdf.Optimization.ImageCompressionVersion.Fast;
optimizationOptions.ImageCompressionOptions.MaxResolution = 72;

doc.OptimizeResources(optimizationOptions);
doc.Save(dataDir + @"ExampleOptimized_20.8.pdf");

ExampleOptimized_20.8.pdf (788.4 KB)

Ok,what about the modes Low,Medium and High like its shown on online compress website how do i differentiate? do i need to keep different values in ImageCompressionOptions if so please suggest what are the ideal values. I need to achieve the same like its on your website.

Thanks

can you check why this 14 kb file when compressed increased size to 55kb
Test_dummy.pdf (13.2 KB)

@srikanthgupta.maple

These modes can be activated using corresponding flags. You can find all details about OptimizationOptions in this forum thread.

The file is already at the maximum optimized stage. We tested the scenario using the same code snippet which we shared in previous post and output file was of 14KB.

ExampleOptimized_20.8.pdf (13.2 KB)

Strange,I tried the same method which is giving me 55Kb file. is it due to Pdf trail version ?
Attached video clip. please check
PDF_compression.zip (759.4 KB)

Thanks

Object not found (Reference table is not referenced) error throwing when i used following Pdf. Please check image attached for error

image.png (14.7 KB)

@srikanthgupta.maple

Yes, it is due to trial version usage as in trial mode, API has limited access to elements in the PDF i.e. it can only process 4 elements of any collection.

We were able to notice a similar exception in our environment and logged it as PDFNET-48673 in our issue management system for the sake of correction. We will further check it in detail and inform you as soon as the investigation is complete and issue is fixed. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi, regd PDFNET-48673, any update ? its almost 1 month

@srikanthgupta.maple

The issue was logged under normal support model where we resolve issue on first come first serve basis. Furthermore, the investigation against the ticket i.e. PDFNET-48673 is not yet done. We will surely let you know as soon as we make some significant progress towards its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.