ImageGear .NET v25.2 - Updated
Developer Guide / How to Work with... / PDF / How to... / Convert... / PDF to Image
In This Topic
    PDF to Image
    In This Topic

    The following CreateRasterPageFromPDFPage sample illustrates how you can create a raster page from a PDF page:

    PDF support needs to be initialized first for this snippet to work. To get familiar with initializing IGNET, initializing PDF support, loading a PDF, saving a PDF, and terminating PDF support, try any one of the tutorials.
    C#
    Copy Code
    using System;
    
    using ImageGear.Core;
    using ImageGear.Formats;
    using ImageGear.Formats.PDF;
    
    private ImGearRasterPage CreateRasterPageFromPDFPage(ImGearPDFPage pdfPage)
           {
               // Bit depth for rasterization process. It may be changed.
               const int rasterizationBitDepth = 24;
    
               #region setResolution
               // Get device independent bitmap from input page.
               ImGearDIB dib = pdfPage.DIB;
               // Get resolution of source page.
               IImGearResolution resolution = dib.ImageResolution;
               // Convert source page resolution if the units are not inches.
               if (resolution.Units != ImGearResolutionUnits.INCHES)
               resolution.ConvertUnits(ImGearResolutionUnits.INCHES);
               // Get maximal size of source page for resolution calculation.
               int maximalPageSize = Math.Max(dib.Width, dib.Height);
               // Calculate X resolution for destination page so the X size of
               // destination page will be 250X250 pixels.
               int xResolution = 250 * resolution.XNumerator / resolution.XDenominator / maximalPageSize;
               // Adjust calculated resolution to 1 because there are conditions when
               // the resolution will be less or equal than 0.
               if (xResolution <= 0) xResolution = 1;
               // Calculate Y resolution for destination page so the X size of
               // destination page will be 250xX250 pixels.
               int yResolution = 250 * resolution.YNumerator / resolution.YDenominator / maximalPageSize;
               #endregion
    
               //Rasterize page.
               ImGearRasterPage rasterPage = pdfPage.Rasterize(rasterizationBitDepth, xResolution, yResolution);
               return rasterPage;
    }
    
    VB.NET
    Copy Code
    Imports ImageGear.Core
    Imports ImageGear.Formats
    Imports ImageGear.Formats.PDF
    
    Private Function CreateRasterPageFromPDFPage(pdfPage As ImGearPDFPage) As ImGearRasterPage
                ' Bit depth for rasterization process. It may be changed.
                Const  rasterizationBitDepth As Integer = 24
    
                '#Region "setResolution"
                ' Get device independent bitmap from input page.
                Dim dib As ImGearDIB = pdfPage.DIB
                ' Get resolution of source page.
                Dim resolution As IImGearResolution = dib.ImageResolution
                ' Convert source page resolution if the units are not inches.
                If resolution.Units <> ImGearResolutionUnits.INCHES Then
                            resolution.ConvertUnits(ImGearResolutionUnits.INCHES)
                End If
                ' Get maximal size of source page for resolution calculation.
                Dim maximalPageSize As Integer = Math.Max(dib.Width, dib.Height)
                ' Calculate X resolution for destination page so the X size of
                ' destination page will be 250X250 pixels.
                Dim xResolution As Integer = 250 * resolution.XNumerator / resolution.XDenominator / maximalPageSize
                ' Adjust calculated resolution to 1 because there are conditions when
                ' the resolution will be less or equal than 0.
                If xResolution <= 0 Then
                            xResolution = 1
                End If
                ' Calculate Y resolution for destination page so the X size of
                ' destination page will be 250xX250 pixels.
                Dim yResolution As Integer = 250 * resolution.YNumerator / resolution.YDenominator / maximalPageSize
                '#End Region
    
                'Rasterize page.
                Dim rasterPage As ImGearRasterPage = pdfPage.Rasterize(rasterizationBitDepth, xResolution, yResolution)
                Return rasterPage
    End Function
    

    Remarks

    Implementation of this method uses only the ImGearPDFPage.Rasterize(int bitDepth, int xResolution, int yResolution) method of ImageGear. Argument bitDepth defines the resulting image bit depth. In the above sample, the value 24 is used, however other values can be used. Arguments xResolution and yResolution define the resolution of the output raster image. For example, in the sample above, the region  #region setResolution can be replaced for fixed resolutions as follows:

     
    Copy Code
    xResolution = 300;
    yResolution = 300;
    

    These resolutions will be used for the new image, i.e., in resultImage.DIB.ImageResolution.