PDF Compression by size with Aspose

I have a PDF with Images and text pages in it which is ranges to 25mb. I want to compress the PDF to maximum…
I tried all available options along with license but the new pDF size increases to 28 MB.
tried below code:

    Document pdfDocument = new Document(inputFile);
    OptimizationOptions optimizationOptions = new 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 = ImageCompressionVersion.Fast;
    optimizationOptions.ImageCompressionOptions.MaxResolution = 72;
    pdfDocument.Optimize();

Dont know why sizes increases when compressing even with proper license. Kindly help
SamplePDF.pdf (3.3 MB)

@immoses
I will assume that the document has already been optimized and the changes used in some things only worsen it. Without having a document it is impossible to say anything more specific.

added sample pDF

@immoses
I only see the message, but no attached document.

The attached pdf can be compressed??? I tried it was exceed by 100 kb

@immoses
Try the following code. In my environment the resulting file takes up 2.35 Mb

var pdfDocument = new Document(dataDir + "samplePdf.pdf");

var optimizationOptions = new OptimizationOptions();

optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
optimizationOptions.ImageCompressionOptions.MaxResolution = 72;
optimizationOptions.ImageCompressionOptions.Encoding = ImageEncoding.Jpeg;

optimizationOptions.CompressObjects = true;
optimizationOptions.LinkDuplcateStreams = true;
optimizationOptions.AllowReusePageContent = true;
optimizationOptions.RemoveUnusedStreams = true;
optimizationOptions.RemoveUnusedObjects = true;

optimizationOptions.MaxResoultion = 72;
optimizationOptions.UnembedFonts = true;
optimizationOptions.SubsetFonts = false;
optimizationOptions.RemovePrivateInfo = true;
optimizationOptions.ImageEncoding = ImageEncoding.Jpeg;

pdfDocument.OptimizeResources(optimizationOptions); // Here is the main difference from the code used. 

pdfDocument.Save(dataDir + "samplePdf-out.pdf");
1 Like

When I tried with above code, the Compression is good with around 50 mb pdf. The time it is taking is arounfd 1 minute/ 60 secs.

but for PDF of size 190MB and greater than 200MB, the time it took to compress is morethan an Hour./ 60+ Minutes.

How to compress it fast with less than a minute. so that it will add to the performance of our application.
Any work arounds will help… Thank You.!

@immoses
I need a document to reproduce and assign a task to the development team. Please post it in the cloud and attach a link to download it here.
You can try different options - perhaps some of them do not give a noticeable effect, but take a lot of time. But it’s clear that you can’t be sure that this will produce results (just like the development team’s research).

could not able to upload 300MB file here… adding c# code to generate a PDF with merging same PDF attached PDF. Kindly generate pDF and compress it.

using Aspose.Pdf;
using System.Reflection;

string inputFile = @“TESTPDF.pdf”;

string pdfLicensePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @“\Aspose.PDF.NET.lic”);
License license = new License();
license.SetLicense(pdfLicensePath);

Document document = new Document(inputFile);

for (int i = 1; i <= 100; i++)
{
var document2 = new Document(inputFile);
document.Pages.Add(document2.Pages);
document2 = null;
}

document.Save(@“ConcatenatedPDF.pdf”);

A clever approach.
Just what kind of file should I take for “TESTPDF.pdf”? There is an option to place a large file in cloud storage and post the link here.

my org does not support uploading files to other clouds… couldnt do that sorry for the inconvience.

you can use the PDF attached above… SamplePDF.pdf in place of testpdf

C# Code

using Aspose.Pdf;
using System.Reflection;

string inputFile = @“C:\SamplePDF.pdf”;

string pdfLicensePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @“\Aspose.PDF.NET.lic”);
License license = new License();
license.SetLicense(pdfLicensePath);

Document document = new Document(inputFile);

for (int i = 1; i <= 100; i++)
{
var document2 = new Document(inputFile);
document.Pages.Add(document2.Pages);
document2 = null;
}

document.Save(@“ConcatenatedPDF.pdf”);

@immoses
I performed the gluing operation and received a document of 32118 pages, 362Mb in size.
Code

Console.WriteLine(DateTime.Now.ToString());
var pdfDocument = new Document(dataDir + "ConcatenatedPDF.pdf");

var optimizationOptions = new OptimizationOptions();

optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 50;
optimizationOptions.ImageCompressionOptions.MaxResolution = 72;
optimizationOptions.ImageCompressionOptions.Encoding = ImageEncoding.Jpeg;

optimizationOptions.CompressObjects = true;
optimizationOptions.LinkDuplcateStreams = true;
optimizationOptions.AllowReusePageContent = true;
optimizationOptions.RemoveUnusedStreams = true;
optimizationOptions.RemoveUnusedObjects = true;

optimizationOptions.MaxResoultion = 72;
optimizationOptions.UnembedFonts = true;
optimizationOptions.SubsetFonts = false;
optimizationOptions.RemovePrivateInfo = true;
optimizationOptions.ImageEncoding = ImageEncoding.Jpeg;

pdfDocument.OptimizeResources(optimizationOptions); 

pdfDocument.Save(dataDir + "ConcatenatedPDF-out.pdf");
Console.WriteLine("Done");
Console.WriteLine(DateTime.Now.ToString());

in my environment takes less than 20 minutes, which I consider a very good result for this document.
If you insist, I will create an investigation task for the development team, but I am not sure that it will improve productivity.

1 Like

Thanks for the solution above… would prefer a document compress in lesser time. Kindly procced for an investigation on the same. that helps.

Thanks.

@immoses
Okay, I’ll create an investigation task for the development team.

@immoses
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56424

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.