Convert PDF to JPG in C#


This code sample shows how to convert a PDF to JPG in C#.

C# - PDF to JPG

1 static void Main(string[] args) 2 { 3 //open the input pdf and select the first page 4 Document pdfDocument = new Document(new FileStream("input.pdf", FileMode.Open, FileAccess.Read)); 5 Page pdfPage = pdfDocument.Pages[0]; 6 7 //create a bitmap to draw to and a graphics object 8 using (Bitmap bitmap = new Bitmap((int)pdfPage.Width, (int)pdfPage.Height)) 9 using (Graphics graphics = Graphics.FromImage(bitmap)) 10 { 11 //draw the image from the first page to the graphics object, which is connected to the bitmap object 12 pdfPage.Draw(graphics); 13 bitmap.Save("out.jpg", ImageFormat.Jpeg); 14 } 15 16 //open the bitmap in the standard application 17 System.Diagnostics.Process.Start("out.jpg"); 18 }

Change the DPI

This is a basic code sample. There is another article where we show how to convert a PDF to BMP and change the DPI.. The same logic described will of course work for JPG as well.