Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (143).NET Core  (23)Angular  (38)ASP.NET  (47)ASP.NET Core  (53)ASP.NET MVC  (83)Azure  (26)Blazor  (44)DocIO  (18)Essential JS 2  (64)Essential Studio  (159)Flutter  (46)JavaScript  (133)Microsoft  (96)PDF  (47)React  (29)Succinctly series  (118)Syncfusion  (383)TypeScript  (30)Uno Platform  (2)UWP  (4)Vue  (25)Webinar  (13)Windows Forms  (52)WPF  (86)Xamarin  (104)XlsIO  (20)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (97)Chart  (38)Cloud  (8)Company  (445)Dashboard  (4)Data Science  (3)Data Validation  (2)DataGrid  (27)Development  (232)Doc  (7)eBook  (89)Enterprise  (22)Entity Framework  (4)Essential Tools  (14)Excel  (11)Extensions  (6)File Manager  (3)Gantt  (6)Gauge  (4)Git  (3)Grid  (24)HTML  (8)Installer  (1)Knockout  (2)LINQPad  (1)Linux  (1)M-Commerce  (1)Metro Studio  (11)Mobile  (133)Mobile MVC  (9)OLAP server  (1)Orubase  (12)Partners  (20)PDF viewer  (15)Performance  (1)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (5)Road Map  (9)Scheduler  (13)SfDataGrid  (7)Silverlight  (21)Sneak Peek  (12)Solution Services  (2)Spreadsheet  (2)SQL  (4)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (8)Tips and Tricks  (12)UI  (56)Uncategorized  (68)Unix  (2)User interface  (42)Visual State Manager  (1)Visual Studio  (12)Visual Studio Code  (6)Web  (85)What's new  (53)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
PDF to Image Conversion is Made Easy with WPF PDF Viewer

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:

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin

Leave a comment

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top