Below is the code snippet. Also attaching the document I have used as an input.
Input Document :
sample.pdf (3.0 KB)
Code Snippet :
using (MemoryStream imageStream = new MemoryStream())
{
var pngDevice = new PngDevice();
// Generate Image
pngDevice.Process(doc.Pages[pageCount], imageStream);
string aratio = string.Empty, height = string.Empty;
//Scale Image using Aspose.Imaging
try
{
using (var img = Aspose.Imaging.Image.Load(new MemoryStream(imageStream.ToArray())))
{
float ratio;
int newWidth;
int newHeight;
if (scaleImages)
{
ratio = scaleRatio;
newWidth = (int)(img.Width * ratio);
newHeight = (int)(img.Height * ratio);
}
else
{
ratio = 0;
newWidth = img.Width;
newHeight = img.Height;
}
aratio = ratio.ToString(CultureInfo.InvariantCulture);
height = newHeight.ToString();
img.Resize(newWidth, newHeight, Aspose.Imaging.ResizeType.HighQualityResample);
byte[] encryptedFile = null;
using (Stream generateImageStream = new MemoryStream())
{
img.Save(generateImageStream);
//Encrypt Image using private key
var b = new byte[generateImageStream.Length];
generateImageStream.Read(b, 0, b.Length);
generateImageStream.Position = 0;
encryptedFile = CommonExtensions.EncryptByteArray(b, model.PrivateKey);
}
pages = string.Concat("image", pageCount, model.ImageExtension);
ratios = aratio;
allheights = height;
PdfFileInfo info = new PdfFileInfo(doc);
rectHeight = info.GetPageHeight(pageCount);
rectWidth = info.GetPageWidth(pageCount);
allRectHeight = string.Concat(allRectHeight, pageCount, ":", rectWidth + ",", rectHeight, "#");
pageDetailModel.Add(new PageDetailModel
{
ImageId = "image" + pageCount + model.ImageExtension,
ImageUrl = Guid.NewGuid().ToString(),
Ratio = aratio,
AllHeights = height,
RectHeight = rectHeight,
RectWidth = rectWidth,
Fields = fields,
AllRectHeightWidth = pageCount + ":" + rectWidth + "," + rectHeight,
ImageStream = encryptedFile != null && encryptedFile.Length > 0 ? new MemoryStream(encryptedFile) : null
});
}
}
catch (Exception)
{
//Ignore
}
}