Converting PDF pages into images in Windows Forms

14 Jul 20265 minutes to read

PdfToImageConverter allows you to convert pages from a PDF document into images using the Convert method. You can convert either a single page or multiple pages into images.

Converting a single page into an image

You can export a single page from a PDF file as an image by specifying the page index and setting the parameters keepTransparency and isSkipAnnotations in the Convert method. To preserve transparency in the output image, make sure to set the keepTransparency parameter to true. If you want to exclude annotations and form field elements from the output image, set the isSkipAnnotations parameter to true. Refer to the following code to export a single page of PDF into PNG image.

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream outputStream = imageConverter.Convert(0, false, false);
Bitmap image = new Bitmap(outputStream);
image.Save("sample.png");

Exporting a specific range of pages

You can export a specific range of PDF pages into images by specifying the start and end page indexes and setting the parameters keepTransparency and isSkipAnnotations in the Convert method. To preserve transparency in the output images, make sure to set the keepTransparency parameter to true. If you want to exclude annotations and form field elements from the output images, set the isSkipAnnotations parameter to true. Refer to the following code to export the pages of PDF into PNG images.

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream[] outputStream = imageConverter.Convert(0, imageConverter.PageCount-1, false, false);
for(int i=0; i < outputStream.Length; i++)
{
    Bitmap image = new Bitmap(outputStream[i]);
    image.Save("sample-"+i+".png");
}

Exporting with a custom image size

You can export a PDF page as an image with custom width and height by passing the required size and setting the parameters keepAspectRatio, keepTransparency, and isSkipAnnotations in the Convert method. To preserve the aspect ratio of the output image, make sure to set the keepAspectRatio parameter to true. To preserve transparency in the output image, make sure to set the keepTransparency parameter to true. If you want to exclude annotations and form field elements from the output image, set the isSkipAnnotations parameter to true. Refer to the following code to export the page at the index of 0 into a PNG image with a width of 1836 pixels and a height of 2372 pixels.

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream outputStream = imageConverter.Convert(0, new SizeF(1836, 2372), false, false, false);
Bitmap image = new Bitmap(outputStream);
image.Save("sample.png");

NOTE

To maintain the aspect ratio of output images, you are required to pass the value true for the keepAspectRatio parameter.

Exporting with a custom image resolution

You can export PDF pages as images with custom horizontal and vertical resolutions by specifying the desired DpiX and DpiY values and configuring the keepTransparency and isSkipAnnotations parameters in the Convert method. To preserve transparency in the output images, make sure to set the keepTransparency parameter to true. If you want to exclude annotations and form field elements from the output images, set the isSkipAnnotations parameter to true. Refer to the following code to export the pages of PDF into PNG images with the horizontal and vertical resolution of 200 respectively.

int startPageIndex = 0;
int endPageIndex = 3;
float dpiX=200;
float dpiY=200;

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream[] outputStream = imageConverter.Convert(startPageIndex, endPageIndex, dpiX, dpiY, false, false);
for(int i=0; i < outputStream.Length; i++)
{
    Bitmap image = new Bitmap(outputStream[i]);
    image.Save("sample-"+i+".png");
}