This C# sample introduces how to load a PDF document from file at disk, and then render and convert it to PNG image file in C# .NET project using pqScan PDF to Image Converter SDK.
using System; using System.Drawing; using System.Drawing.Imaging; using PQScan.PDFToImage; namespace PDF2PNG { class Program { static void Main(string[] args) { // Create an instance of PQScan.PDFToImage.PDFDocument object. PDFDocument pdfDoc = new PDFDocument(); // Load a PDF document. pdfDoc.LoadPDF("sample.pdf"); // Get its total page count. int count = pdfDoc.PageCount; for (int i = 0; i < count; i++) { // Convert page to image. Bitmap pngImage = pdfDoc.ToImage(i); // Save image as png file format. pngImage.Save("output" + i + ".png", ImageFormat.Png); } } } }
We also offer Visual C# demo for how to change and turn PDF file stream to PNG image file format. Moreover, in this coding sample, PNG image size can be set.
using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using PQScan.PDFToImage; namespace PDFToPNG { 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 message. FileStream stream = new FileStream("sample.pdf", FileMode.Open); // Load PDF document from file stream. pdf.LoadPDF(stream); // Set rendered png image width. int width = pdf.GetPageWidth(0) / 2; // Set rendered png image height. int height = pdf.GetPageHeight(0) / 2; // Convert the first PDF page to image with the required size. Bitmap png = pdf.ToImage(0, width, height); // Save image to png file format. png.Save("result.png", ImageFormat.Png); } } }
For more help of changing rendered image size, please see online guide: How to change image size while converting from PDF page using C#.