Asad,
We’re splitting the PDF in pages and then we save each one of the pages as HTML. Then, we check each html page size using Fiddler. Basically we check the response size each time we open a page.
Below the several code methods used for this:
private Aspose.Pdf.Document _document;
// Here we set the aspose document. I’ve included the change that I tried following your previous suggestion (commented).
public override void SetDocument(Stream file)
{
file.Position = 0;
Stream = new SmallBlockMemoryStream();
file.CopyTo(Stream);
Stream.Position = 0;
_document = CreateDocument(Stream);
_document.AllowReusePageContent = true;
//_document.OptimizeResources(new Document.OptimizationOptions()
//{
// LinkDuplcateStreams = true,
// RemoveUnusedObjects = true,
// RemoveUnusedStreams = true,
// CompressImages = true,
// ImageQuality = 50
//});
}
// This method generates the html for each page. The commented code was another try to compress images. When opening the first page there are 2 requests, the first one includes the thumbnails and the second one doesn’t. We’re considering the second one size in our tests.
private DocumentPreviewPage GetPagePreview(Page asposePage)
{
//int idx = 1;
//foreach (Aspose.Pdf.XImage pageImage in asposePage.Resources.Images)
//{
// using (MemoryStream imageStream = new MemoryStream())
// {
// pageImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// asposePage.Resources.Images.Replace(idx, imageStream, 50);
// idx = idx + 1;
// }
//}
var output = new MemoryStream();
asposePage.SendTo(new PngDevice(), output);
output.Position = 0;
var image = Aspose.Imaging.Image.Load(output);
byte[] thumbnailBytes;
var options = new Aspose.Imaging.ImageOptions.PngOptions { ColorType = PngColorType.Truecolor };
using (var thumbnail = new MemoryStream())
{
if (image.Width < image.Height)
image.ResizeHeightProportionally(300, ResizeType.LanczosResample);
else
image.ResizeWidthProportionally(300, ResizeType.LanczosResample);
image.Save(thumbnail, options);
thumbnailBytes = thumbnail.ToArray();
}
var htmlBytes = GetPageHtml(asposePage);
var subpath = string.Empty;
if (FileManager.DefaultFileType == StorageType.PhysicalFile)
subpath = "previews";
var file = FileManager.Store(output.ToArray(), FileManager.DefaultFileType, Guid.NewGuid().ToString("N"),
".png", subpath, "");
return new DocumentPreviewPage
{
PreviewContentLink = file.FileHash,
PageNumber = asposePage.Number,
Width = image.Width,
Height = image.Height,
Thumbnail = thumbnailBytes,
Html = htmlBytes
};
}
Thanks in advance,
Let me know if you have any other questions.