Convert PDF to JPG in C#
PDF to JPG
To create a JPG image from a page in a PDF document, follow the steps below:
- Create a
GdPicturePDF
object and aGdPictureImaging
object. - Load the source document by passing its path to the
LoadFromFile
method of theGdPicturePDF
object. - Select the page that you want to convert to an image with the
SelectPage
method of theGdPicturePDF
object. - Render the selected page to a 200 dots-per-inch (DPI) image with the
RenderPageToGdPictureImageEx
method of theGdPicturePDF
object. - Save the output in a JPG image with the
SaveAsJPEG
method.
The example below creates a JPG 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 JPG image.gdpictureImaging.SaveAsJPEG(imageId, @"C:\temp\output.jpg");// Release unnecessary resources.gdpictureImaging.ReleaseGdPictureImage(imageId);gdpicturePDF.CloseDocument();
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()Using gdpictureImaging As 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. Dim imageId As Integer = gdpicturePDF.RenderPageToGdPictureImageEx(200, True) ' Save the output in a JPG image. gdpictureImaging.SaveAsJPEG(imageId, "C:\temp\output.jpg") ' Release unnecessary resources. gdpictureImaging.ReleaseGdPictureImage(imageId) gdpicturePDF.CloseDocument()End UsingEnd Using
Related topics