Optimize/Reduce PDF file size in C# using Aspose.PDF for .NET - PDF Compression

Hello

I want compress a pdf file but the size has not reduce.

My code :

   using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fichierDeSortie))
        {
            pdfDocument.IgnoreCorruptedObjects = true;
            pdfDocument.OptimizeSize = true;

            var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
            optimizationOptions.RemoveUnusedObjects = true;
            optimizationOptions.RemoveUnusedStreams = true;
            optimizationOptions.ImageCompressionOptions.CompressImages = true;
            optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
            optimizationOptions.ImageCompressionOptions.ResizeImages = true;

            pdfDocument.OptimizeResources(optimizationOptions);

            //Enregistrement du document
            pdfDocument.Save(fichierDeSortie);
        }

Could you help me please ?

@frdcned

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

Thanks for your tests…

This message (and any attachments) is confidential and reserved to the recipients named above, and is protected by law. If you are not in the list of recipients, please inform the sender immediately and destroy it. Any disclosure, reproduction, dissemination of this message, or use for any reason whatsoever, is strictly prohibited.

Have you receive the file.pdf ?

@frdcned

We did not receive any file. Would you please make sure to upload it with your post using upload button. In case you do not want to share the file in here, you may send it in a private message. In order to send private message, please click over username and press blue Message button.

I can’t upload the file because it’s too big (36Mo for three pages). I tried to upload one page (12Mo) but it’s the same think.

@frdcned

In case your file size is more than 10MB, you may please upload it to Dropbox or Google Drive and share the link with us.

@frdcned

We have tested the scenario in our environment using following code snippet and Aspose.PDF for .NET 19.2. We were unable to notice any issue as file size was reduced from ~36MB to ~1.2MB. For your kind reference, output PDF is also shared in a private message.

Document doc = new Document(dataDir + @"file.pdf");
var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizationOptions.RemoveUnusedObjects = true;
optimizationOptions.RemoveUnusedStreams = true;
optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
doc.OptimizeResources(optimizationOptions);
doc.Save(dataDir + @"ExampleOptimized_19.2.pdf");

Would you please try using latest version of the API in your environment and in case you still face any issue, please feel free to let us know.

Hi

I have test this code, but I do not reduce a lot : I have 118Kb using this code and 119Kb without.
Same result if I set ImageQuality =1.
Below my test application :

AsposePdfGeneration.zip (1.3 MB)

Regards,
Sybaris

@sybaris

We are checking it and will get back to you soon.

@sybaris

We have checked the sample project shared by you. You were optimizing the PDF document even before adding the image in it. Please try saving the document after adding image so that PDF can be generated with its content and original size. Later you can optimize it. Please consider using following code snippet:

using (Aspose.Pdf.Document docPdf = new Aspose.Pdf.Document())
using (var bitmap = new Bitmap(dataDir + @"CarteVitale.png"))
using (MemoryStream ms = new MemoryStream())
{
 Page page = docPdf.Pages.Add();
 var image = new Aspose.Pdf.Image();
 page.Paragraphs.Add(image);
 page.PageInfo.Height = bitmap.Height;
 page.PageInfo.Width = bitmap.Width;
 page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

 bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 image.ImageStream = ms;

 docPdf.Save(dataDir + "output20.1.pdf");
 var newdocPdf = new Document(dataDir + "output20.1.pdf");
 if (true)
 {
  var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
  optimizationOptions.RemoveUnusedObjects = true;
  optimizationOptions.RemoveUnusedStreams = true;
  optimizationOptions.ImageCompressionOptions.CompressImages = true;
  optimizationOptions.ImageCompressionOptions.ImageQuality = 1;
  optimizationOptions.ImageCompressionOptions.ResizeImages = true;
  newdocPdf.OptimizeResources(optimizationOptions);
 }
 // Save the document
 newdocPdf.Save(dataDir + "output20.1.pdf");
}

Hi

Thanks,
With the provided code, now my file is 87 Kb.
Great.

Regards
Sybaris

@sybaris

Thanks for your feedback.

It is good to know that things have started working at your side. Please keep using our API and in case you need further assistance, please feel free to let us know.

I am having am issue using some of the code examples above as the PDF I am using has a LOT of images in it. I know this PDF can be reduced because other web tools reduce it by 75% or more. I have attached an example using your tool - can you please let me know what I am doing wrong?

PdfCompression.Aspose.zip (5.4 MB)

1 Like

@omnant

We tried to test the scenario using your file and code snippet with Aspose.PDF for .NET 20.8 and noticed that program kept running for more than 30 minutes and never produced an output. However, could you please confirm if you are facing similar issue at your side or a different one?

Also, would you please share which tools you used to reduce the PDF file size upto 75%? Please share those obtained optimized PDF for our reference so that we can investigate the scenario accordingly and share our feedback with you.

One of the online/web tool that I was using is accessible here: Compress PDF - Compress your PDF document online

Attached are two PDFs for each of the two options that they have (Basic or Strong) - not sure about the difference … it takes maybe 30-45 seconds on their site. I have a regular stream of files on this file size that I would prefer to automate though - thus looking at your tool. My testing has the same issue with the 30+ mins and I eventually ended up stopping VS instead of allowing to continue running.Drawing_Compressed_Basic.pdf (4.8 MB)
Drawing_Compressed_Strong.pdf (4.5 MB)

@omnant

We have logged an issue as PDFNET-48721 in our issue tracking system for the sake of correction. We will further investigate this behavior of the API in details and keep you informed with the status of ticket resolution. Please be patient and give us some time.

We are sorry for the inconvenience.

No problem! Looking forward to hearing back.

Checking in to see if there was any update on this issue.

@omnant

I’m afraid, there is no progress on this issue yet. Please note the issues are managed to first come, first serve basis. As this issue was logged recently, our team will get to this soon and work on it. We’ll notify you in this thread once the issue is resolved. I apologize for the inconvenience caused by this issue.