This article explains the most basic way to generate a bitmap from a PDF page.
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
}
} 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 }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. 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);
}
} 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);
15 }
16 }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.
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);
}
} 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);
19 }
20 }