C# Convert PDF to Image

C# Convert PDF to Image

Today, a colleague told me that the PDF file you obtained doesn't quite fit our existing software workflow, so can you convert our PDF file to an image?

Last updated 7/16/2022 1:19 PM
黑哥聊dotNet
3 min read
Category
.NET
Tags
.NET C# PDF

Foreword

A colleague told me today that the PDF files you are obtaining are somewhat incompatible with our existing software workflow. Can you convert our PDF files to images? So I decided to do it. Since I had researched PDF-related components some time ago, I found a repository I had starred on GitHub: PdfiumViewer. Open source address: https://github.com/pvginkel/PdfiumViewer:

First, open NuGet and install PdfiumViewer and ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll.

For a requirement, we should understand what properties the objects we operate on have.

So when we get a PDF file, we should know that PDFs have properties like the number of pages, height, width, etc.

For images, important properties include height, width (resolution), horizontal resolution, and vertical resolution (DPI), among others.

Once we understand these properties, we can start working. The first step is to load the PDF file and obtain the page count and page sizes.

var pdf = PdfiumViewer.PdfDocument.Load(strpdfPath);
var pdfpage = pdf.PageCount;
var pagesizes = pdf.PageSizes;

Then assemble the image height, width, horizontal resolution, vertical resolution, and other properties.

document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);

Finally, save the image.

image.Save(stream, ImageFormat.Jpeg);

The complete code is as follows.

public class PdfToImage
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="filePath">PDF file path</param>
    /// <param name="picPath">Image file path</param>
    public  void PdfToPic(string filePath, string picPath)
    {

        var pdf = PdfiumViewer.PdfDocument.Load(filePath);
        var pdfpage = pdf.PageCount;
        var pagesizes = pdf.PageSizes;

        for (int i = 1; i <= pdfpage; i++)
        {
            Size size = new Size();
            size.Height = (int)pagesizes[(i - 1)].Height;
            size.Width = (int)pagesizes[(i - 1)].Width;
            // You can change ".jpg" to another format
            RenderPage(filePath, i, size, picPath);
        }

    }

    private void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 300)
    {
        using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))
        using (var stream = new FileStream(outputPath, FileMode.Create))
        using (var image = GetPageImage(pageNumber, size, document, dpi))
        {
            image.Save(stream, ImageFormat.Jpeg);
        }
    }
    private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi)
    {
        return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);

    }
}
Keep Exploring

Related Reading

More Articles