Exporting large dimension PDF to high dpi PNG creates undersized image

Hello,

I am using the PDF library to create PNG images which I then import into our CAD documents.

    string fileExtension = "png";
    ImageFormat format = ImageFormat.Png;
    PdfConverter pdfConverter = new PdfConverter();
    pdfConverter.BindPdf(sourceFilePath);
    pdfConverter.Resolution = new PdfResolution(documentConnectionInfo.Dpi);
    pdfConverter.StartPage = workingPageNumber;
    pdfConverter.EndPage = workingPageNumber;
    pdfConverter.DoConvert();

I’m finding that for large PDFs (11x17 and greater) and high DPI (300 to 600) the resulting image is smaller than expected:
image.png (41.3 KB)

Is this a known issue or is there a known workaround? I can provide simple test PDFs to reproduce the issue if needed.

Thanks,
Ben Bishoff, Developer
Ideate Software

@IdeateSoftware

Could you please try to use the DOM Approach and use ImageDevice Class to produce PNG images from PDF? In case the issue still persists, please share your sample PDF document with us so that we can test the scenario in our environment and address it accordingly.

Hello,

Thank you for the suggestion of the alternate method using the ImageDevice class, I tried the following code:
PdfResolution resolution = new PdfResolution(documentConnectionInfo.Dpi);
PdfDocument pdfDocument = new PdfDocument(sourceFilePath);
PngDevice pngDevice = new PngDevice(resolution);

            for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
            {
                using (FileStream imageStream = new FileStream(workingImageFilePath, FileMode.Create))
                {
                    // Convert a particular page and save the image to stream
                    pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

                    // Close stream
                    imageStream.Close();
                }
            }

Unfortunately, I am getting the same results as when I used the PdfConverter class, i.e. smaller images at higher dpi

Here are the PDF documents I am using for testing:

Let know if you need any more information to debug this issue. Thanks in advance for your efforts on my behalf!

Sincerely,
Ben Bishoff
Developer - Ideate Software

@IdeateSoftware

We are checking it and will get back to you shortly.

Thank you for your prompt attention!

@IdeateSoftware

Could you please try to specify the width and height as well along with the Resolution in PngDevice Constructor like in the below code and let us know in case output images still do not meet your requirements:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "11x17.pdf");
// Create Aspose.Pdf.RenderingOptions to enable font hinting
RenderingOptions opts = new RenderingOptions();
opts.UseFontHinting = true;

for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
 using (FileStream imageStream = new FileStream(dataDir + $"output{pageCount}.png", FileMode.Create))
 {
  // Create PNG device with specified attributes
  // Width, Height, Resolution, Quality
  // Quality [0-100], 100 is Maximum
  // Create Resolution object
  Resolution resolution = new Resolution(300);
  // 1 inch = 72 points
  PngDevice pngDevice = new PngDevice((17*72), (11*72), resolution);

  // Set predefined rendering options
  pngDevice.RenderingOptions = opts;
  // Convert a particular page and save the image to stream
  pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

  // Close stream
 imageStream.Close();
 }
}

output1.png (6.0 KB)

Hello,

Thank you for your suggestion! The use of PngDevice does give me give me control over the final image size, however it appears to require adding a DPI dependent scale that I do not understand.

Here is the code I created based on your sample. Note that I used the page rectangle to determine that page size from the arbitrary PDF I am given by the user:

            // Create Aspose.Pdf.RenderingOptions to enable font hinting
            RenderingOptions opts = new RenderingOptions();
            opts.UseFontHinting = true;

            for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
            {
                using (FileStream imageStream = new FileStream(workingImageFilePath, FileMode.Create))
                {
                    // Create PNG device with specified attributes
                    // Create Resolution object
                    PdfResolution resolution = new PdfResolution(documentConnectionInfo.Dpi);

                    // Get page dimension in points
                    int width = (int)pdfDocument.Pages[pageCount].GetPageRect(true).Width;
                    int height = (int)pdfDocument.Pages[pageCount].GetPageRect(true).Height;

                    // Create PNG renderer
                    PngDevice pngDevice = new PngDevice(width, height, resolution);

                    // Set predefined rendering options
                    pngDevice.RenderingOptions = opts;

                    // Convert a particular page and save the image to stream
                    pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

                    // Close stream
                    imageStream.Close();
                }
            }

With this code, here are the image results:
image.png (26.4 KB)

As you can see, at DPI higher than 72, the images get consistently scaled smaller. Curiously, I found the images are EXACTLY off by these scale factors?!?:

72 dpi 1
150 dpi 100 / (12 * 4)
300 dpi 100 / (12 * 2)
600 dpi 100 / (12 * 1)

Do you have any insight on these required scaling factors?

Cheers,
Ben Bishoff, Developer
Ideate Software

@IdeateSoftware

We need to further investigate this in order to share some more information with you regarding this behavior of the API. For the purpose, an investigation ticket as PDFNET-51181 has been logged in our issue tracking system. We will surely look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Thank you very much for investigating this further! No worries, as a software developer myself I understand the timeline involved in prioritizing and researching bugs.

I look forward to what you may find. Thank you for your effort on my behalf.

Cheers,
Ben Bishoff
Software Developer, Ideate Software

@IdeateSoftware

Thanks for your feedback. We will surely inform you as soon as we make some definite progress towards ticket resolution.