By adding Visual C# sample code below to your .NET project, you will get perfect JPG/JPEG images from PDF file pages.
using System; using System.Drawing; using System.Drawing.Imaging; using PQScan.PDFToImage; namespace PDF2JPG { class Program { static void Main(string[] args) { // Create an instance of PQScan.PDFToImage.PDFDocument object. PDFDocument pdfDoc = new PDFDocument(); // Load PDF document from local file. pdfDoc.LoadPDF("sample.pdf"); // Get the total page count. int count = pdfDoc.PageCount; for (int i = 0; i < count; i++) { // Convert PDF page to image. Bitmap jpgImage = pdfDoc.ToImage(i); // Save image with jpg file type. jpgImage.Save("output" + i + ".jpg", ImageFormat.Jpeg); } } } }
As you see, the above C# programming example shows how to load a PDF file at local disk and convert it to JPG/JPEG image files. And now we will introduce you how to load a PDF from file stream and convert it to JPG/JPEG images with expected size using C# class code.
using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using PQScan.PDFToImage; namespace PDFToJPG { class Program { static void Main(string[] args) { // Make a new instance of PQScan.PDFToImage.PDFDocument object. PDFDocument pdf = new PDFDocument(); // Create a file stream with PDF information FileStream stream = new FileStream("sample.pdf", FileMode.Open); // Load PDF document from file stream. pdf.LoadPDF(stream); // Set the width of output jpeg image. int width = pdf.GetPageWidth(0) / 2; // Set the height output jpeg image. int height = pdf.GetPageHeight(0) / 2; // Convert the first PDF page to image with the expected size. Bitmap jpg = pdf.ToImage(0, width, height); // Save image to jpg format. jpg.Save("result.jpeg", ImageFormat.Jpeg); } } }
In addition to these C# PDF converting examples, we also provide detailed online guide for how to customize JPEG image size while converting from PDF document in C#.