Convert multiple PDF pages to bitmap
In this article, we will convert multple pages of a PDF to a single bitmap. The pages will be rendered as a horizontal strip. A margin of 5 pixels will be added around the edges of each page. Each page will be vertically aligned at the center. A black rectangle is drawn around each page.
Open the PDF document
The first step is to open a PDF and retrieve the first page. Let’s use the OpenFileDialog class to select a PDF document from disk.
// prompt the user to select a PDF document
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "PDF documents (*.pdf)|*.pdf";
if (DialogResult.OK == openDialog.ShowDialog())
{
// open the selected PDF document
string path = openDialog.FileName;
using (FileStream pdfFile = new FileStream(
path, FileMode.Open, FileAccess.Read))
{
Document pdfDocument = new Document(pdfFile);
// more code follows
}
}
Determine the width and height of the bitmap
Next we calculate the size of the bitmap to which we would like the PDF to convert to. The width is calculated by adding all page widths including left and right margins. The height is calculated as the maximum height of all pages including top and bottom margins. Note that we use ‘scale’ to calculate the width and height of the page in pixels. Scale follows from the resolution. If the resolution would be 72 dpi, then the scale would equal 1. This means that 1 point (1/72 inch) corresponds to 1 pixel.
float resolution = 18;
float scale = resolution / 72f;
// determine width and height - we create a horizontal strip...
int MARGIN = 5;
int totalWidth = 0;
int totalHeight = 0;
for (int i = 0; i < pdfDocument.Pages.Count; i++)
{
Page pdfPage = pdfDocument.Pages[i];
totalWidth += (int)(scale * pdfPage.Width + 2 * MARGIN);
totalHeight = Math.Max(
totalHeight, (int)(scale * pdfPage.Height) + 2 * MARGIN);
}
Convert the PDF to bitmap
Now that we have calculated the size of the bitmap, we render the pages one <span class=“hljs-keyword”>by</span> one <span class=“hljs-keyword”>using</span> the following code:
using (Bitmap bitmap = new Bitmap(totalWidth, totalHeight))
using (Graphics graphics = Graphics.FromImage(bitmap))
using (Pen pen = new Pen(Color.Black))
{
// we maintain a horizontal cursor to position the next page
int x = 0;
// draw each page
for (int i = 0; i < pdfDocument.Pages.Count; i++)
{
GraphicsState state = graphics.Save();
// get the next page
Page pdfPage = pdfDocument.Pages[i];
// draw the page at the given resolution and position
graphics.TranslateTransform(
(float) (x + MARGIN),
(float) (totalHeight - scale * pdfPage.Height) / 2);
graphics.ScaleTransform(scale, scale);
pdfPage.Draw(graphics);
// draw a black rectangle around the page
graphics.DrawRectangle(
pen, 0, 0, (int)pdfPage.Width, (int)pdfPage.Height);
// update the horizontal cursor
x += (int)(scale * pdfPage.Width);
x += 2 * MARGIN;
graphics.Restore(state);
}
// prompt the user to save the bitmap
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "PNG files (*.png)|*.png";
if (DialogResult.OK == saveDialog.ShowDialog())
{
bitmap.Save(saveDialog.FileName);
}
}