Skip to content

PDF to Image – Convert PDF pages to images

PDF4NET library, using the PDF4NET.Render module (included in the PDF4NET+Render paclages), provides extensive support for converting PDF pages to images.

We have implemented the conversion of PDF pages to images through our custom PDF rendering engine. It has been developed from scratch and it has been developed to be cross-platform. This allowed us to support PDF to image capabilities on all platforms plus it allows us to implement various custom features such as native grayscale rendering for reduced memory usage or stripped rendering surfaces for ultra high dpi support.

The conversion of a PDF page to image is a simple 3 step process:

  • load the document
  • create a renderer for the page
  • convert the page to image
C#
1
2
3
PDFFixedDocument doc = new PDFFixedDocument("sample.pdf");
PDFPageRenderer renderer = new PDFPageRenderer(doc.Pages[0]);
Bitmap pageImage = renderer.ConvertPageToImage(96);

The library supports converting PDF pages to RAW images.

C#
1
2
3
4
5
6
7
PDFFixedDocument doc = new PDFFixedDocument("sample.pdf");
PDFPageRenderer renderer = new PDFPageRenderer(doc.Pages[0]);
using (FileStream rawStream = File.OpenWrite("sample.raw"))
{
    renderer.ConvertPageToImage(96, rawStream, PDFPageImageFormat.Raw);
    rawStream.Flush();
}

For RAW images the RGBA layout is used by default but the library provides support for RGBA, BGRA and ARGB rendering surfaces in case you need different color layout.
The code below uses an ARGB rendering surface for converting the PDF to image.

C#
PDFFixedDocument doc = new PDFFixedDocument("sample.pdf");
PDFPageRenderer renderer = new PDFPageRenderer(doc.Pages[0]);
PDFRendererSettings settings = new PDFRendererSettings();
settings.DpiX = settings.DpiY = 96;
settings.RenderingSurface = 
    renderer.CreateRenderingSurface<PDFArgbByteRenderingSurface>(settings.DpiX, settings.DpiY);
using (FileStream rawStream = File.OpenWrite("sample.raw"))
{
    renderer.ConvertPageToImage(rawStream, PDFPageImageFormat.Raw, settings);
    rawStream.Flush();
}

The rendering surface can use either an array of bytes or an array of integers as storage. You select the appropriate storage based on what you want to do with the rendered image.
For example if you’re working with WriteableBitmaps in WPF you should use an int based storage because it will be easier to transfer the pixels from the rendering surface to the WriteableBitmap.

C#
PDFFixedDocument doc = new PDFFixedDocument("sample.pdf");
PDFPageRenderer renderer = new PDFPageRenderer(doc.Pages[0]);

PDFRendererSettings settings = new PDFRendererSettings();
settings.DpiX = settings.DpiY = 96;
PDFArgbIntRenderingSurface rs = 
    renderer.CreateRenderingSurface<PDFArgbIntRenderingSurface>(settings.DpiX, settings.DpiY);
settings.RenderingSurface = rs;

renderer.ConvertPageToImage(settings);

WriteableBitmap wb = new WriteableBitmap(rs.Width, rs.Height);
Array.Copy(rs.Bitmap, wb.Pixels, rs.Bitmap.Length);

The PDF rendering engine is portable and this means almost identical results on all platforms. Why almost? The PDF rendering engine compiled for Windows Forms and WPF has access to the fonts installed on the local machine. If the PDF files use non-embedded fonts then the WinForms and WPF versions will retrieve the fonts from the local machine while the other versions will use a default Helvetica font.

Fell free to contact us with any questions you might have about PDF rendering or if you have a PDF file that is not rendered correctly.