Hello,
I need to convert a PDF using Aspose.Imaging but always get an image with black background.
This is my code:
LoadOptions loadOptions = new LoadOptions
{
BufferSizeHint = 100, // Set buffer size hint to avoid memory issues
DataBackgroundColor = Aspose.Imaging.Color.WhiteSmoke,
ConcurrentImageProcessing = true,
DataRecoveryMode = DataRecoveryMode.ConsistentRecover,
};
var image = Image.Load(pdfPath, loadOptions);
if (image is IMultipageImage multipageImage)
{
Console.WriteLine($"PDF loaded successfully. Pages: {multipageImage.PageCount}");
for (int pageIndex = 0; pageIndex < multipageImage.PageCount; pageIndex++)
{
var imagePath = ConvertPageToImage(image, pageIndex, outputDirectory, fileNamePrefix);
outputPaths.Add(imagePath);
Console.WriteLine($"Converted page {pageIndex + 1} to: {Path.GetFileName(imagePath)}");
}
}
else
{
// Single page PDF
var imagePath = ConvertPageToImage(image, 0, outputDirectory, fileNamePrefix);
outputPaths.Add(imagePath);
Console.WriteLine($"Converted single page to: {Path.GetFileName(imagePath)}");
}
private string ConvertPageToImage(Aspose.Imaging.Image sourceImage, int pageIndex, string outputDirectory, string fileNamePrefix)
{
// Create output file path
var fileName = $"{fileNamePrefix}_{pageIndex + 1:D3}.{_imageExtension}";
var outputPath = Path.Combine(outputDirectory, fileName);
// Handle multi-page images
if (sourceImage is IMultipageImage multipageImage && pageIndex < multipageImage.PageCount)
{
// Extract specific page
using (var pageImage = multipageImage.Pages[pageIndex])
{
var jpegOptions = new Aspose.Imaging.ImageOptions.JpegOptions()
{
Quality = 95,
ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96, 96),
VectorRasterizationOptions = new VectorRasterizationOptions()
{
PageWidth = pageImage.Width,
PageHeight = pageImage.Height,
BackgroundColor = Aspose.Imaging.Color.WhiteSmoke,
TextRenderingHint = Aspose.Imaging.TextRenderingHint.AntiAliasGridFit,
SmoothingMode = Aspose.Imaging.SmoothingMode.HighQuality,
},
PreblendAlphaIfPresent = true,
ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Rgb,
};
pageImage.Save(outputPath, pngOptions);
}
}
return outputPath;
}
Am I doing something wrong?
How can I achieve this process? I would like to set different image quality and resolution.
Also, how can I rescale to improve visibility of the image? Something similar to zooming.
Please help.
Thank you,
Cesar