Render a PDF to bitmap

Convert PDF
10/24/2009

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.

C#

1 OpenFileDialog openDialog = new OpenFileDialog(); 2 openDialog.Filter = "PDF documents (*.pdf)|*.pdf"; 3 4 if (DialogResult.OK == openDialog.ShowDialog()) 5 { 6 string path = openDialog.FileName; 7 using (FileStream pdfFile = new FileStream( 8 path, FileMode.Open, FileAccess.Read)) 9 { 10 Document pdfDocument = new Document(pdfFile); 11 Page pdfPage = pdfDocument.Pages[0]; 12 13 // more code follows 14 } 15 }

VB .NET

1 Dim openDialog As New OpenFileDialog() 2 openDialog.Filter = "PDF documents (*.pdf)|*.pdf" 3 4 If DialogResult.OK = openDialog.ShowDialog() Then 5 Dim path As String = openDialog.FileName 6 Using pdfFile As New FileStream(path, FileMode.Open, FileAccess.Read) 7 Dim pdfDocument As New Document(pdfFile) 8 9 ' more code follows 10 Dim pdfPage As Page = pdfDocument.Pages(0) 11 End Using 12 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:

C#

1 // 72 dpi 2 int bmpWidth = (int) pdfPage.Width; 3 int bmpHeight = (int) pdfPage.Height; 4 5 using (Bitmap bitmap = new Bitmap(bmpWidth, bmpHeight)) 6 using (Graphics graphics = Graphics.FromImage(bitmap)) 7 { 8 pdfPage.Draw(graphics); 9 10 SaveFileDialog saveDialog = new SaveFileDialog(); 11 saveDialog.Filter = "PNG files (*.png)|*.png"; 12 if (DialogResult.OK == saveDialog.ShowDialog()) 13 { 14 bitmap.Save(saveDialog.FileName, ImageFormat.Png); 15 } 16 }

VB .NET

1 ' 72 dpi 2 Dim bmpWidth As Integer = CInt(pdfPage.Width) 3 Dim bmpHeight As Integer = CInt(pdfPage.Height) 4 5 Using bitmap As New Bitmap(bmpWidth, bmpHeight) 6 Using graphics As Graphics = Graphics.FromImage(bitmap) 7 pdfPage.Draw(graphics) 8 9 Dim saveDialog As New SaveFileDialog() 10 saveDialog.Filter = "PNG files (*.png)|*.png" 11 If DialogResult.OK = saveDialog.ShowDialog() Then 12 bitmap.Save(saveDialog.FileName, ImageFormat.Png) 13 End If 14 End Using 15 End Using

Render at any DPI

If you want to render the PDF page at a different resolution, then you will have to change the size of the output bitmap and you will have to scale the Graphics object before you render to it. The following code can be used to render at any given resolution.

C#

1 float resolution = 300; 2 float scale = resolution / 72f; 3 4 int bmpWidth = (int) (scale * pdfPage.Width); 5 int bmpHeight = (int) (scale * pdfPage.Height); 6 7 using (Bitmap bitmap = new Bitmap(bmpWidth, bmpHeight)) 8 using (Graphics graphics = Graphics.FromImage(bitmap)) 9 { 10 graphics.ScaleTransform(scale, scale); 11 12 pdfPage.Draw(graphics); 13 14 SaveFileDialog saveDialog = new SaveFileDialog(); 15 saveDialog.Filter = "PNG files (*.png)|*.png"; 16 if (DialogResult.OK == saveDialog.ShowDialog()) 17 { 18 bitmap.Save(saveDialog.FileName, ImageFormat.Png); 19 } 20 }

VB .NET

1 Dim resolution As Single = 300 2 Dim scale As Single = resolution / 72F 3 4 Dim bmpWidth As Integer = CInt(scale * pdfPage.Width) 5 Dim bmpHeight As Integer = CInt(scale * pdfPage.Height) 6 7 Using bitmap As New Bitmap(bmpWidth, bmpHeight) 8 Using graphics As Graphics = Graphics.FromImage(bitmap) 9 graphics.ScaleTransform(scale, scale) 10 11 pdfPage.Draw(graphics) 12 13 Dim saveDialog As New SaveFileDialog() 14 saveDialog.Filter = "PNG files (*.png)|*.png" 15 If DialogResult.OK = saveDialog.ShowDialog() Then 16 bitmap.Save(saveDialog.FileName, ImageFormat.Png) 17 End If 18 End Using 19 End Using