Render a PDF to bitmap

This article explains the most basic way to convert PDF to bitmap.

Open the PDF document

The first step is to open a PDF document and retrieve the first page. Let’s use the OpenFileDialog class to select a PDF document from disk.

OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "PDF documents (*.pdf)|*.pdf";

if (DialogResult.OK == openDialog.ShowDialog())
{
  string path = openDialog.FileName;
  using (FileStream pdfFile = new FileStream(
  path, FileMode.Open, FileAccess.Read))
  {
    Document pdfDocument = new Document(pdfFile);
    Page pdfPage = pdfDocument.Pages[0];

    // more code follows
 }
}
Dim openDialog As New OpenFileDialog()
openDialog.Filter = "PDF documents (*.pdf)|*.pdf"

If DialogResult.OK = openDialog.ShowDialog() Then
	Dim path As String = openDialog.FileName
	Using pdfFile As New FileStream(path, FileMode.Open, FileAccess.Read)
		Dim pdfDocument As New Document(pdfFile)

			' more code follows
		Dim pdfPage As Page = pdfDocument.Pages(0)
	End Using
End If

Render at 72 DPI

Next we determine the size of the bitmap to which we would like to render. This depends on two things: 1. the size of the PDF page; 2. the desired resolution of the bitmap. Note that page.Width and page.Height return the size of the page in points. If the size of the bitmap in pixels would equal the size of the page in points, then the effective resolution would by 1 pixel per 1 point, which is the same as 72 pixels per inch (1 inch = 72 points) which is 72 DPI. In this sample we save the image to as a PNG, but you can also save as BMP, TIFF and JPG. Drawing the PDF page to a 72 DPI bitmap looks like this:

// 72 dpi
int bmpWidth = (int) pdfPage.Width;
int bmpHeight = (int) pdfPage.Height;

using (Bitmap bitmap = new Bitmap(bmpWidth, bmpHeight))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
  pdfPage.Draw(graphics);

  SaveFileDialog saveDialog = new SaveFileDialog();
  saveDialog.Filter = "PNG files (*.png)|*.png";
  if (DialogResult.OK == saveDialog.ShowDialog())
  {
    bitmap.Save(saveDialog.FileName, ImageFormat.Png);
  }
}
' 72 dpi
Dim bmpWidth As Integer = CInt(pdfPage.Width)
Dim bmpHeight As Integer = CInt(pdfPage.Height)

Using bitmap As New Bitmap(bmpWidth, bmpHeight)
	Using graphics As Graphics = Graphics.FromImage(bitmap)
		pdfPage.Draw(graphics)

		Dim saveDialog As New SaveFileDialog()
		saveDialog.Filter = "PNG files (*.png)|*.png"
		If DialogResult.OK = saveDialog.ShowDialog() Then
			bitmap.Save(saveDialog.FileName, ImageFormat.Png)
		End If
	End Using
End Using
float resolution = 300;
float scale = resolution / 72f;

int bmpWidth = (int) (scale * pdfPage.Width);
int bmpHeight = (int) (scale * pdfPage.Height);
 
using (Bitmap bitmap = new Bitmap(bmpWidth, bmpHeight))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
  graphics.ScaleTransform(scale, scale);
 
  pdfPage.Draw(graphics);

  SaveFileDialog saveDialog = new SaveFileDialog();
  saveDialog.Filter = "PNG files (*.png)|*.png";
  if (DialogResult.OK == saveDialog.ShowDialog())
  {
    bitmap.Save(saveDialog.FileName, ImageFormat.Png);
  }
}
Dim resolution As Single = 300
Dim scale As Single = resolution / 72F

Dim bmpWidth As Integer = CInt(scale * pdfPage.Width)
Dim bmpHeight As Integer = CInt(scale * pdfPage.Height)

Using bitmap As New Bitmap(bmpWidth, bmpHeight)
	Using graphics As Graphics = Graphics.FromImage(bitmap)
		graphics.ScaleTransform(scale, scale)

		pdfPage.Draw(graphics)

		Dim saveDialog As New SaveFileDialog()
		saveDialog.Filter = "PNG files (*.png)|*.png"
		If DialogResult.OK = saveDialog.ShowDialog() Then
			bitmap.Save(saveDialog.FileName, ImageFormat.Png)
		End If
	End Using
End Using