Convert PDF File
Convert PDF Pages to Different Image Formats (Facades)
In order to convert PDF pages to different image formats, you need to create PdfConverter object and open the PDF file using BindPdf method. After that, you need to call DoConvert method for initialization tasks. Then, you can loop through all the pages using HasNextImage and GetNextImage methods. The GetNextImage method allows you to create image of a particular page. You also need to pass ImageFormat to this method in order to create an image of specific type i.e. JPEG, GIF or PNG etc. Finally, call the Close method of the PdfConverter class. The following code snippet shows you how to convert PDF pages to images.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPdfPagesToImages01()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PdfConverter object
using (var converter = new Aspose.Pdf.Facades.PdfConverter())
{
// Bind PDF document
converter.BindPdf(dataDir + "ConvertPdfPagesToImages.pdf");
// Initialize the converting process
converter.DoConvert();
// Check if pages exist and then convert to image one by one
while (converter.HasNextImage())
{
// Generate output file name with '_out' suffix
var outputFileName = dataDir + System.DateTime.Now.Ticks.ToString() + "_out.jpg";
// Convert the page to image and save it
converter.GetNextImage(outputFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
In the next code snippet, we will show how you can change some parameters. With CoordinateType we set the frame ‘CropBox’. Also, we can change Resolution specifying the number of dots per inch. The next one FormPresentationMode - form presentation mode. Then we indicate the StartPage with which the page number of the beginning of the conversion is set. We can also specify the last page by setting a range.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPdfPagesToImages02()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PdfConverter object
using (var converter = new Aspose.Pdf.Facades.PdfConverter())
{
// Bind PDF document
converter.BindPdf(dataDir + "ConvertPdfPagesToImages.pdf");
// Initialize the converting process
converter.DoConvert();
// Set additional conversion settings
converter.CoordinateType = Aspose.Pdf.PageCoordinateType.CropBox;
converter.Resolution = new Aspose.Pdf.Devices.Resolution(600);
converter.FormPresentationMode = Aspose.Pdf.Devices.FormPresentationMode.Production;
converter.StartPage = 2;
// Check if pages exist and then convert to image one by one
while (converter.HasNextImage())
{
// Generate output file name
var outputFileName = dataDir + System.DateTime.Now.Ticks.ToString() + "_out.jpg";
// Convert the page to image and save it
converter.GetNextImage(outputFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
See also
Aspose.PDF for .NET allows converting PDF documents to various formats and also converting from other formats to PDF. Also, you can check the quality of Aspose.PDF conversion and view the results online with Aspose.PDF converter app. Learn Converting section for resolving your tasks.