new_sample_1-10_20231012040814386.pdf (3.7 MB)
Hi,
We are using the below mentioned snippet for Grayscale conversion and compression. We have tried with a 3.7MB file, after grayscale conversion the file size has increased to 29MB and then after compression it got reduced to 7.4MB which is double the size of the original file. Kindly validate and confirm the same.
Grayscale Conversion:
using (Aspose.Pdf.Document document = new Aspose.Pdf.Document(SourceFilePath + Filename))
{
Aspose.Pdf.RgbToDeviceGrayConversionStrategy strategy = new Aspose.Pdf.RgbToDeviceGrayConversionStrategy();
for (int idxPage = 1; idxPage <= document.Pages.Count; idxPage++)
{
// Get instance of particular page inside PDF
Page page = document.Pages[idxPage];
try
{
// Convert the RGB colorspace image to GrayScale colorspace
strategy.Convert(page);
}
catch (Exception ex)
{
WriteErrorLog("ConverttoGrayscalePDF Exception - " + " - " + ex);
continue;
}
}
// Save resultant file
document.Save(convertfilename);
document.Dispose();
}
Compression:
using (Aspose.Pdf.Document doc2 = new Aspose.Pdf.Document(convertfilename))
{
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 = 50;
optimizationOptions.ImageCompressionOptions.ResizeImages = true;
optimizationOptions.ImageCompressionOptions.Version = Aspose.Pdf.Optimization.ImageCompressionVersion.Fast;
optimizationOptions.ImageCompressionOptions.MaxResolution = 200;
doc2.OptimizeResources(optimizationOptions);
doc2.Save(Source);
doc2.Dispose();
}