Syncfusion AI Assistant

How can I help you?

Export PDF pages as images in ASP.NET Core PDF Viewer

28 Feb 20267 minutes to read

The Syncfusion® ASP.NET Core PDF Viewer provides the ExportAsImage APIs to convert individual pages or a range of pages into raster images. These images can be saved to disk, processed in-memory, or served as downloads in your application.

Steps to export pages as images

Step 1: Create an ASP.NET Core PDF Viewer application by following the getting started guide.

Step 2: Use the PdfRenderer class to load the document and invoke the appropriate ExportAsImage overload based on the requirements.

Export a single page

Convert a specific page to a Bitmap using the default rendering settings.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap bitmapimage = pdfExportImage.ExportAsImage(0);
//Save the exported image in disk
bitmapimage.Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");

Export with custom DPI or size

To control the resolution or dimensions of the exported image, use the overloads that accept DPI values or SizeF objects.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap bitmapimage = pdfExportImage.ExportAsImage(0, 200, 200);
//Save the exported image in disk
bitmapimage.Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");

Export a page with custom size

Exports the specified page as image with respect to the specified custom size.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap bitmapimage = pdfExportImage.ExportAsImage(0, new SizeF(200, 300), true);
//Save the exported image in disk
bitmapimage.Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");

Export a page with custom size and DPI

Exports the specified page as image with respect to the custom size and the specified DPI values.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap bitmapimage = pdfExportImage.ExportAsImage(0, new SizeF(200, 300),200,200, true);
//Save the exported image in disk
bitmapimage.Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");

Export a range of pages

Retrieve an array of bitmaps for a specific page range.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount-1);
for (int i = 0; i < pdfExportImage.PageCount; i++)
{
    // Save the exported image in disk
    bitmapimage[i].Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");
}

Export a range with custom DPI

Exports the specified pages as images with respect to the specified DPI values.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount-1, 200, 200);
for (int i = 0; i < pdfExportImage.PageCount; i++)
{
    //Save the exported image in disk
    bitmapimage[i].Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");
}

Export a range with custom size

Exports the specified pages as images with respect to the specified custom size.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount-1, new SizeF(200, 300), false);
for (int i = 0; i < pdfExportImage.PageCount; i++)
{
    //Save the exported image in disk
    bitmapimage[i].Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");
}

Export a range with custom size and DPI

Exports the specified pages as images with respect to the custom size and the specified DPI values.

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\HTTP Succinctly.pdf");
//Exports the PDF document pages into images
Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount-1, new SizeF(200, 300),200,200,false);
for (int i = 0; i < pdfExportImage.PageCount; i++)
{
    //Save the exported image in disk
    bitmapimage[i].Save(@"currentDirectory\..\..\..\..\Images\" + "bitmapImage" + i.ToString() + ".png");
}

Export pages as images in ASP.NET Core 6.0

To export a PDF to image in ASP.NET Core 6.0 using PDF Viewer and Skia Bitmap, you can follow these steps:

Create an instance of PdfRenderer in the controller, and create an instance of SKBitmap to get the bitmap representation of the page. Exporting can be done using the ExportAsImage() method. This method helps to convert a PDF into an image. Use the SKImage class to save the SKBitmap object as a PNG image, and use the SaveTo method of the SKBitmap to save the image to a file. Return the image file in the response.

Here is a sample that shows how to export as image in ASP.NET Core 6.0 :

//Uses the Syncfusion.EJ2.PdfViewer assembly
PdfRenderer pdfExportImage = new PdfRenderer();
//Loads the PDF document
pdfExportImage.Load(@"currentDirectory\..\..\..\..\Data\hive_succinctly.pdf");
//Exports the PDF document pages into images
SKBitmap bitmapimage = pdfExportImage.ExportAsImage(0);
SKImage image = SKImage.FromBitmap(bitmapimage);
using (var stream = new FileStream(Path.Combine(@"currentDirectory\..\..\..\..\Data\Page1.png"), FileMode.Create))
    {//Save the exported image in disk
        var imageData = image.Encode(SKEncodedImageFormat.Png, 100);
        imageData.SaveTo(stream);
    }
    var imageFileStream = System.IO.File.OpenRead(Path.Combine(@"currentDirectory\..\..\..\..\Data\Page1.png"));
    return File(imageFileStream, "image/png");

View complete sample in GitHub

NOTE

Ensure the application has appropriate read/write permissions for the document and image directories. For cross-platform deployments, refer to the PDFium resolution guide to configure native dependencies.