HiQPdf Documentation

Rasterize PDF Pages to Images

Quickly Create High Quality PDFs

With HiQPdf Library you can rasterize PDF pages to images using the HiQPdfPdfRasterizer class. You can extract the images to memory HiQPdfPdfPageRasterImage objects using the PdfRasterizerRasterizeToImageObjects(String, Int32, Int32) method or to image files using the PdfRasterizerRasterizeToImageFiles(String, Int32, Int32, String, String) method. There is also a special method PdfRasterizerRaisePageRasterizedEvent(String, Int32, Int32) which can be used to rasterize the PDF document pages one by one in memory to avoid high memory usage for large PDF documents. The HiQPdfPdfPageRasterImage objects can be disposed when they are no longer necessary.

You have also the option to convert the pages of a PDF document to a multipage TIFF image into a file on disk or in a memory buffer. You can use the PdfRasterizerRasterizeToTiffFile(String, Int32, Int32, String) method to produce a TIFF file on disk or the PdfRasterizerRasterizeToTiff(String, Int32, Int32) method to produce the TIFF image in a memory buffer.

Rasterize PDF Pages to Images Demo

In this demo you can learn how to rasterize the pages of PDF document to images. You can choose the color space of resulted images, the resolution of the rasterization, the range of PDF pages to rasterize. You can also choose to convert to a multipage TIFF image.

Demo Source Code

C#
private void buttonRasterizeToImages_Click(object sender, EventArgs e)
{
    // get the PDF file
    string pdfFile = textBoxPdfFile.Text;
    if (pdfFile.Length == 0)
    {
        MessageBox.Show("The PDF file cannot be empty");
        return;
    }

    // create the PDF rasterizer
    PdfRasterizer pdfRasterizer = new PdfRasterizer();

    // set the output images color space
    pdfRasterizer.ColorSpace = GetColorSpace();

    // set the rasterization resolution in DPI
    pdfRasterizer.DPI = int.Parse(textBoxDPI.Text);

    // the output directory for resulted images
    string outputDir = Application.StartupPath + @"\DemoOut\PdfRasterizer";

    // the output TIFF file if the PDF is converted to a multipage TIFF image
    string outputTiffFile = System.IO.Path.Combine(outputDir, "pdfpages.tiff");

    Cursor = Cursors.WaitCursor;
    try
    {
        int fromPdfPageNumber = int.Parse(textBoxFromPage.Text);
        int toPdfPageNumber = textBoxToPage.Text.Length > 0 ? int.Parse(textBoxToPage.Text) : 0;

        if (checkBoxToTiff.Checked)
        {
            // convert the PDF document to a multipage TIFF image file
            // the TIFF images can also be produced in memory using the RasterizeToTiff method
            pdfRasterizer.RasterizeToTiffFile(pdfFile, fromPdfPageNumber, toPdfPageNumber, outputTiffFile);
        }
        else
        {
            // rasterize a range of pages of the PDF document and save the images to an output directory
            // the images can also be produced in memory using the RasterizeToImageObjects or RaisePageRasterizedEvent methods
            pdfRasterizer.RasterizeToImageFiles(pdfFile, fromPdfPageNumber, toPdfPageNumber, outputDir, "page");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Rasterization failed. {0}", ex.Message));
        return;
    }
    finally
    {
        Cursor = Cursors.Arrow;
    }

    try
    {
        if (checkBoxToTiff.Checked)
        {
            // open the TIFF file in default viewer
            System.Diagnostics.Process.Start(outputTiffFile);
        }
        else
        {
            // open the output folder in Explorer
            System.Diagnostics.Process.Start(outputDir);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Cannot open the result. {0}", ex.Message));
    }
}
See Also

Other Resources