Convert PDF to PNG | C#

Use the Aspose.PDF for .NET library for converting PDF Pages to PNG Images in an accessible and convenient way.

The PngDevice class allows you to convert PDF pages to PNG images. This class provides a method named Process which allows you to convert a particular page of the PDF file to PNG image format.

Convert PDF Pages to PNG Images

Aspose.PDF for .NET show you how to convert all pages in a PDF file to images:

  1. Loop through all pages in the file.
  2. Convert each page individually:
    1. Create an object of the Document class to load the PDF document.
    2. Get the page you want to convert.
    3. Call the Process method to convert the page to Png.

The following code snippet shows you how to convert all PDF pages to PNG images.

Convert single PDF page to PNG image

Aspose.PDF for .NET show you how to convert a particular page to PNG format:

Pass the page index as an argument to the Process(..) method. The following code snippet shows the steps to convert the first page of PDF to PNG format.

public static void ConvertPDFtoPngSinglePage()
{
    // Open document
    Document pdfDocument = new Document(_dataDir + "PageToPng.pdf");

    using (FileStream imageStream = new FileStream(_dataDir + "image_out.Png", FileMode.Create))
    {
        // Create Resolution object
        Resolution resolution = new Resolution(300);
        // Create Png device with specified attributes
        // Width, Height, Resolution
        PngDevice PngDevice = new PngDevice(500, 700, resolution);

        // Convert a particular page and save the image to stream
        PngDevice.Process(pdfDocument.Pages[1], imageStream);

        // Close stream
        imageStream.Close();
    }
}