How to Render PDF to Images with C#

 Use this awesome project: https://github.com/pvginkel/PdfiumViewer

1) Add the NuGet packs:

2) Make sure you have the correct CPU type

3) Use this code as an example:

using PdfiumViewer; 

float scale = 72 / 96f; // we want a 96 dpi - change the 96 to whatever dpi you need
var doc = PdfDocument.Load(file);
for (int i = 0; i < doc.PageCount; i++)
{
    try
    {
        var width = (int)(doc.PageSizes[i].Width / scale);
        var height = (int)(doc.PageSizes[i].Height / scale);
 
        using (Image image = doc.Render(i, width, height, 96, 96, PdfRenderFlags.ForPrinting))
        {
            string filePath = Path.Combine(_outputFolder, $"{i}.png");
            image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
        }
    }
    catch { }
}


Comments