Convert PDF to PNG in C#

PDF to PNG

To create a PNG image from a page in a PDF document, follow the steps below:

  1. Create a GdPicturePDF object and a GdPictureImaging object.
  2. Load the source document by passing its path to the LoadFromFile method of the GdPicturePDF object.
  3. Select the page that you want to convert to an image with the SelectPage method of the GdPicturePDF object.
  4. Render the selected page to a 200 dots-per-inch (DPI) image with the RenderPageToGdPictureImageEx method of the GdPicturePDF object.
  5. Save the output in a PNG image with the SaveAsPNG method.

The example below creates a PNG image from the first page of a PDF document:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the source PDF document.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Select the first page of the PDF.
gdpicturePDF.SelectPage(1);
// Render the selected page to an image.
int imageId = gdpicturePDF.RenderPageToGdPictureImageEx(200, true);
// Save the output in a PNG image.
gdpictureImaging.SaveAsPNG(imageId, @"C:\temp\output.png");
// Release unnecessary resources.
gdpictureImaging.ReleaseGdPictureImage(imageId);
gdpicturePDF.CloseDocument();