Hi Imran,
I’m sorry but i can not send you our original files for security reasons but I created a sample PDF Example.pdf. After converting it, it will only be compressed with /Filter/DCTDecode and much larger, or with higher compression values the quality of the picture become more and more degraded. My sample code to convert the file, is the following:
I tried Aspose.PDF.dll Version 18.1.0 and 18.3.0 with the same result.
using System;
using System.IO;
using Aspose.Pdf;
namespace PdfTools
{
public class AsposeTests
{
public static void ConvertPdf()
{
var sourceFile = @"C:\Temp\PDFModify\Example.pdf";
var outputFile = @"C:\Temp\PDFModify\Example_Output.pdf";
var tempFile = Path.GetTempFileName ();
try
{
using (var outStream = File.Open (tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
using (var inStream = File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
using (var doc = new Document (inStream))
{
Convert (doc, PdfFormat.v_1_4);
doc.Save (outStream);
}
}
using (var inStream = File.Open (tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
using (var doc = new Document (inStream))
{
Optimize (doc);
Convert (doc, PdfFormat.PDF_A_1B);
using (var outStream = new MemoryStream ())
{
doc.Save (outStream);
File.WriteAllBytes (outputFile, outStream.ToArray ());
}
}
}
finally
{
File.Delete (tempFile);
}
}
private static void Convert(Document doc, PdfFormat targetFormat)
{
var logFile = Path.GetTempFileName ();
try
{
var success = doc.Convert (logFile, targetFormat, ConvertErrorAction.Delete);
if (!success)
throw new Exception ($"Cannot convert to {targetFormat}. " + File.ReadAllText (logFile));
}
finally
{
File.Delete (logFile);
}
}
private static void Optimize(Document doc)
{
var optimizationOptions = new Document.OptimizationOptions ()
{
AllowReusePageContent = true,
LinkDuplcateStreams = true,
RemoveUnusedObjects = false,
RemoveUnusedStreams = false,
CompressImages = true,
ImageQuality = 80
};
doc.OptimizeResources (optimizationOptions);
}
}
}
I played around with the Document.OptimizationOptions and tried different values for MaxResolution in combination with ResizeImages = true. But the resulting files stay larger than the originals or the quality of the images becomes much worse. We even have examples where the original is about 1MB and the converted files is around 12MB.