PDF to Image Conversion is Made Easy with WPF PDF Viewer | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
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:

Comments (2)

Does this work with .Net Core web applications? I seem to be getting a lot of compatibility issues with the required dlls.

Hi Richard,

To export PDF pages as image in .NET Core Web application, you need to use our PDF Viewer [Web] server-side libraries.

NuGet package to be installed in your app.

To run or deploy in Windows, include package Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows to your application
To run or deploy in Linux, refer Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux
To run or deploy in macOS, refer Syncfusion.EJ2.PdfViewer.AspNet.Core.OSX

Please find the code snippet to export to PDF as image from the below documentation link:

Documentation link: https://ej2.syncfusion.com/aspnetcore/documentation/pdfviewer/how-to/export-as-image

Regards,
Ramkumar

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed