Dear Team,
The quality of image is poor when we convert PDF to image with syncfusion .net core libraries,
How can we improve the image quality in .net core?
we are using "Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows" Version="20.4.0.44"
we have attached sample PDF and extracted image using the code.
Please find the below code
using (PdfRenderer pdfInfo = new PdfRenderer())
{
// Loading the pdf file
pdfInfo.Load(pdfFilePath);
int pageCount = pdfInfo.PageCount;
int i = 1;
while (i <= pageCount)
{
// Exporting image from pdf
using (SKBitmap skBitmap = pdfInfo.ExportAsImage(i - 1))
{
Log.Information($"{nameof(this.ConvertPdfToImage)} -pdfFilePath- {pdfFilePath} - file size {skBitmap.Info.BytesSize}, Width {skBitmap.Width}, Height {skBitmap.Height}");
string fileFullName = string.Empty, temporaryFilePath = string.Empty;
fileFullName = $"{pdfFileName}-p{i}{ImageConstants.PdfToImageFileExtension}";
temporaryFilePath = Path.GetTempPath() + fileFullName;
using (SKImage skImage = SKImage.FromBitmap(skBitmap))
{
using (SKData encoded = skImage.Encode(SKImageEncodedFormat, SKImageEncodedQuality))
{
using (var bitmapImageStream = File.Open(temporaryFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
// Saving to temp folder
encoded.SaveTo(bitmapImageStream);
results.Add(new FileDTO
{
Name = fileFullName,
Path = temporaryFilePath,
});
i++;
}
}
}
}
}
}
We were able to reproduce the reported issue “Image quality is poor” and we will analyze for improvement. We will analyze further on this and update you with more details within two business days.
On Analyzing further we found that we can modify the sample to convert high-resolution images.
We have provided the sample below to export the image with high quality.
Code snippet:
public IActionResult OnGetImage() { PdfRenderer pdfviewer = new PdfRenderer(); var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "data/imagepdf.pdf"); var docPath = System.IO.File.OpenRead(imagePath);
var docProperty= pdfviewer.Load(docPath); string dd = System.Text.Json.JsonSerializer.Serialize(docProperty); DocumentProperty weatherForecast = System.Text.Json.JsonSerializer.Deserialize<DocumentProperty>(dd)!; Syncfusion.Drawing.SizeF customSize = new Syncfusion.Drawing.SizeF(weatherForecast.pageSizes[0].Width*2, weatherForecast.pageSizes[0].Height*2); SKBitmap bitmapimage = pdfviewer.ExportAsImage(0, customSize,true);
using (var stream1 = new FileStream(Path.Combine(_hostingEnvironment.WebRootPath, "data/high21.png"), FileMode.Create)) { //bitmapimage.Resize( new SKSizeI((int)weatherForecast.pageSizes[0].Width*2, (int)weatherForecast.pageSizes[0].Height*2), SKFilterQuality.High); var image = SKImage.FromBitmap(bitmapimage); var data1 = image.Encode(SKEncodedImageFormat.Png, 200); data1.SaveTo(stream1); } var imageFileStream = System.IO.File.OpenRead(Path.Combine(_hostingEnvironment.WebRootPath, "data/high21.png")); return File(imageFileStream, "image/png");
} }
public class DocumentProperty { public int pageCount { get; set; }
public Dictionary<int, Syncfusion.Drawing.SizeF> pageSizes { get;set; } }
|
Sample - https://www.syncfusion.com/downloads/support/directtrac/general/ze/pdf_to_image-451165198.zip
Kindly try the above sample and let us know if you have any concerns.