Issues when converting PDF to JPG

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

@CQcesar

Cause
The issue of getting a black background when converting a PDF to JPG using Aspose.Imaging is likely due to how the background color is set during the conversion process. The VectorRasterizationOptions allows you to specify a background color, but the current settings may not be applied correctly.

Solution
To resolve the black background issue, ensure that you set the BackgroundColor property in the VectorRasterizationOptions to a color of your choice, such as Aspose.Imaging.Color.White. Additionally, verify that you are using the correct options when saving the image.

Code
Here’s an updated version of your ConvertPageToImage method with the background color set to white:

private string ConvertPageToImage(Aspose.Imaging.Image sourceImage, int pageIndex, string outputDirectory, string fileNamePrefix)
{
    var fileName = $"{fileNamePrefix}_{pageIndex + 1:D3}.{_imageExtension}";
    var outputPath = Path.Combine(outputDirectory, fileName);

    if (sourceImage is IMultipageImage multipageImage && pageIndex < multipageImage.PageCount)
    {
        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.White, // Set background color to white
                    TextRenderingHint = Aspose.Imaging.TextRenderingHint.AntiAliasGridFit,
                    SmoothingMode = Aspose.Imaging.SmoothingMode.HighQuality,
                },
                PreblendAlphaIfPresent = true,
                ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Rgb,
            };

            pageImage.Save(outputPath, jpegOptions); // Ensure you are using jpegOptions here
        }
    }

    return outputPath;
}

Next Steps

  1. Test the updated code to confirm that the background issue is resolved.
  2. If the problem persists, check the PDF file for any specific properties that may affect rendering.
  3. If you suspect this might be a bug or regression, contact Aspose support with your version number and a sample PDF file for further assistance.

Please let me know if you need any further help!

Hello @CQcesar
We appreciate your interest in Aspose.Imaging.
We will check this issue ASAP.

Best wishes

@CQcesar
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): IMAGINGNET-7733

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.