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.

private static void ConvertPdfPagesToImages01()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfConverter object using 'using' block to ensure proper disposal
    using (var converter = new Aspose.Pdf.Facades.PdfConverter())
    {
        // Bind input pdf file
        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);
        }

        // The converter will be automatically disposed at the end of the 'using' block
    }
}

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.

private static void ConvertPdfPagesToImages02()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfConverter object using 'using' block to ensure proper disposal
    using (var converter = new Aspose.Pdf.Facades.PdfConverter())
    {
        // Bind input pdf file
        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 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);
        }

        // The converter will be automatically disposed at the end of the 'using' block
    }
}

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.