Copied RSS Feed

PDF

PDF to Image Conversion is Made Easy with WPF PDF Viewer

We’ve all felt that it’s hard to export PDF document pages as images for various use cases. That’s why we decided to make it easy in our WPF PDF Viewer. In this blog, we will walk you through the steps to export PDF pages as high-quality images using this component.

The PDF Viewer allows you to export to the following formats:

How to export the PDF file pages as images

You can export the pages of a PDF file into images using the ExportAsImage method. We will see how to export a single page into an image and a specific range of pages into normal and thumbnail-sized images.

Export a single page of a PDF file as an image

A single page of a PDF file can be exported to an image by passing the page index as a parameter of the ExportAsImage method. The following code example demonstrates how to export a single page to a JPEG image programmatically without using the PDF Viewer UI.

//Initialize PdfViewerControl
PdfViewerControl pdfViewer = new PdfViewerControl();

//Load the input PDF file.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf");
pdfViewer.Load(loadedDocument);

//Export the particular PDF page as image at the page index of 0.
BitmapSource image = pdfViewer.ExportAsImage(0);
 
//Set up the output path.
string output = @"..\..\Output\Image";
if (image != null)
{
        //Initialize the new Jpeg bitmap encoder.
        BitmapEncoder encoder = new JpegBitmapEncoder();
        //Create the bitmap frame using the bitmap source and add it to the encoder.
        encoder.Frames.Add(BitmapFrame.Create(image));
        //Create the file stream for the output in the desired image format.
        FileStream stream = new FileStream(output + ".Jpeg", FileMode.Create);
        //Save the stream so that the image will be generated in the output location.
        encoder.Save(stream);
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;

Export a specific range of pages in a PDF file as images

A specific range of PDF pages can be exported to images by passing the start and end page indices as parameters of the ExportAsImage method. The following code example demonstrates how to export a range of PDF pages to JPEG images programmatically without using the PDF Viewer UI.

//Initialize PdfViewerControl.
PdfViewerControl pdfViewer = new PdfViewerControl();

//Load the input PDF file.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf");
pdfViewer.Load(loadedDocument);

//Export all the pages as images at the specific page range.
BitmapSource[] image = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count - 1);

//Set up the output path.
string output = @"..\..\Output\Image";
if (image != null)
{
        for (int i = 0; i < image.Length; i++)
        {
                //Initialize the new Jpeg bitmap encoder.
                BitmapEncoder encoder = new JpegBitmapEncoder();
                //Create the bitmap frame using the bitmap source and add it to the encoder.
                encoder.Frames.Add(BitmapFrame.Create(image[i]));
                //Create the file stream for the output in the desired image format.
                FileStream stream = new FileStream(output + i.ToString() + ".Jpeg", FileMode.Create);
                //Save the stream so that the image will be generated in the output location.
                encoder.Save(stream);
        }
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;

Export the pages in a PDF file as thumbnails

PDF pages can be exported as thumbnail images with custom sizes by passing the start page index, end page index, and size (width and height) as parameters of the ExportAsImage method. The following code snippet demonstrates how to export the PDF pages to thumbnail images in JPEG format programmatically without using the PDF Viewer UI.

//Initialize PdfViewerControl.
PdfViewerControl pdfViewer = new PdfViewerControl();

//Load the input PDF file.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf");
pdfViewer.Load(loadedDocument);

//Export all the pages as thumbnail images with size (150, 225).
BitmapSource[] image = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count – 1, new SizeF(150, 225), false);

//Set up the output path.
string output = @"..\..\Output\Image";
if (image != null)
{
        for (int i = 0; i < image.Length; i++)
        {
                //Initialize the new Jpeg bitmap encoder.
                BitmapEncoder encoder = new JpegBitmapEncoder();
                //Create the bitmap frame using the bitmap source and add it to the encoder.
                encoder.Frames.Add(BitmapFrame.Create(image[i]));
                //Create the file stream for the output in the desired image format.
                FileStream stream = new FileStream(output + i.ToString() + ".Jpeg", FileMode.Create);
                //Save the stream so that the image will be generated in the output location.
                encoder.Save(stream);
        }
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;

You can find the sample project explaining the export image functionality from this GitHub location. In this sample, you will find the pages in the PDF file (“Barcode.pdf”) are exported as images with default size and as thumbnail images with the smaller size. Execution of this sample application exports the normal and thumbnail-sized images to the Output folder in the application directory.

Conclusion

I hope you have a clear idea about how to export the pages of a PDF document as images. All the examples are provided by converting the pages into JPG format. You can export them into other formats like PNG, BMP, and TIFF, too. Try them yourself and share your feedback in the comment section below.

If you aren’t a customer yet, you can try our 30-day free trial to check out these features. Also, try our other samples from this GitHub location.

If you wish to send us feedback or would like to submit any questions, please feel free to contact us through our support forumDirect-Trac, or feedback portal. We are happy to assist you!

If you liked this post, we think you will also enjoy the following:

Meet the Author

Ramkumar Ravy

Ramkumar Ravy is a Product Manager at Syncfusion. He is passionate about building cross-platform mobile and Windows applications. He also loves working on Artificial Intelligence and Machine Learning and has been active since 2014.