I am converting some images that have text to pdf, but when i download the pdf the images in the pdf are all blurry and the text is unreadable. All the images are high quality and i am resizing them and keeping the proportion. His there anything to do to get a readable pdf?
Could you please share your sample image along with sample code snippet that you are using for conversion? We will test the scenario in our environment and address it accordingly.
test.jpeg (156.7 KB)
test.pdf (57.8 KB)
ssPDF = new byte[] {};
var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
pdfImageSection.PageInfo.Margin.Bottom = 20;
pdfImageSection.PageInfo.Margin.Top = 20;
pdfImageSection.PageInfo.Margin.Left = 20;
pdfImageSection.PageInfo.Margin.Right = 20;
foreach (RCImageRecord file in ssImages)
{
MemoryStream stream = new MemoryStream(file.ssSTImage.ssContent);
System.Drawing.Image img = new System.Drawing.Bitmap(stream);
img = ScaleImage(img, (int)pdfImageSection.PageInfo.Width, (int)pdfImageSection.PageInfo.Height);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var image = new Aspose.Pdf.Image { ImageStream = ms };
image.Margin = new Aspose.Pdf.MarginInfo(0, 10, 0, 0);
pdfImageSection.Paragraphs.Add(image);
}
MemoryStream outStream = new MemoryStream();
var optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizationOptions.ImageCompressionOptions.CompressImages = true;
optimizationOptions.ImageCompressionOptions.ImageQuality = 90;
optimizationOptions.ImageCompressionOptions.MaxResolution = 900;
pdf.OptimizeResources(optimizationOptions);
pdf.Save(outStream);
ssPDF = outStream.ToArray();
private static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new System.Drawing.Bitmap(newWidth, newHeight);
using (var graphics = System.Drawing.Graphics.FromImage(newImage))
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
Sorry for the delayed response.
We have tested the scenario in our environment and noticed the issue in the output PDF generated by Aspose.PDF for .NET 20.12. Furthermore, we have noticed that the issue occurs when ScaleImage() method was called. The mentioned method is degrading the image quality and making the text inside it unreadable. We can also say that the issue is not related to the Aspose.PDF.
If we skip the culprit method, the image in the PDF comes out just fine. ExampleOptimized_20.12.pdf (309.3 KB)
We have also logged an investigation ticket as PDFNET-49185 in our issue tracking system to analyze this case in details. We will further investigate whether an image can be scaled without loosing the quality or not and let you know as soon as the ticket is resolved. Please be patient and spare us some time.
Ok thanks. The quality of the image without scaling is not good enough, so i need to be able to scale the image without loosing quality.
Despite the issue is not related to the Aspose.PDF, we will still try to look into its details. However, the ticket will be investigated on a first come first serve basis and we will surely inform you as soon as it is resolved.