ImageGear v26.3 - Updated
Developer Guide / How to Work with ... / PDF / How to... / Convert... / PDF to Image
In This Topic
    PDF to Image
    In This Topic

    The CreateRasterPageFromPDFPage sample demonstrates this functionality. See Samples.

    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, refer to our samples on github.

    C#

    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;
    }
    

    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:

    C#

    xResolution = 300;
    yResolution = 300;
    

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