I am working in .NET 4.5.2 using Aspose.PDF 18.1… Whenever I generate a pdf or combine multiple pdfs and try to reduce the filesize, it seems that the OptimizeResources options are never applied to the document. I’ve tried adding the optimization options to a newly initialized empty document. I have tried to apply them after the document has been populated. Nothing works. Any help would be greatly appreciated.
Here is the current code I have for Merging multiple pdfs together after being created earlier in this file:
{
//Dictionary containing all the pdfs created in this session
var cdrlFiles = (Dictionary<int, string>)HttpContext.Current.Session["print-cdrls-pdf"];
//initialize variable for each pdf while being iterated
Aspose.Pdf.Document cdrlFile;
// We now need to merge them all together. To do this we will add all the CDRLs to one PDF
var existPdf = new Aspose.Pdf.Document();
foreach (var file in cdrlFiles.OrderBy(kvp => kvp.Key))
{
var cs = new MemoryStream((byte[])HttpContext.Current.Session[file.Value]);
cdrlFile = new Document(cs);
cdrlFile.OptimizeResources(new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = true,
ImageQuality = 10
});
// Add the pages of the source documents to the target document
existPdf.Pages.Add(cdrlFile.Pages);
}
// Finally save the combined file back to the session to be presented to the user
using (var ms = new MemoryStream())
{
existPdf.Save(ms);
HttpContext.Current.Session["pdf-file"] = ms.ToArray();
}
cdrlFiles.Clear();
HttpContext.Current.Session["print-cdrls-pdf"] = null;
return true;
}
Thank you!