HiQPdf Documentation

Extract Images from PDF

Quickly Create High Quality PDFs
Extract Images from PDF Demo

In this demo you can learn how to extract the images from a PDF document. You can choose the range of PDF pages from where to extract the images. Images will be extracted in memory in .NET Image objects.

Demo Source Code

C#
protected void buttonExtractImages_Click(object sender, EventArgs e)
{
    // get the PDF file
    string pdfFile = Server.MapPath("~") + @"\DemoFiles\Pdf\InputPdf.pdf";

    // create the PDF images extractor
    PdfImagesExtract pdfImagesExtract = new PdfImagesExtract();

    int fromPdfPageNumber = int.Parse(textBoxFromPage.Text);
    int toPdfPageNumber = textBoxToPage.Text.Length > 0 ? int.Parse(textBoxToPage.Text) : 0;

    // extract the images from PDF document to memory in .NET Image objects
    // the images can also be extracted to a folder using the ExtractToImageFiles method 
    // or they can be extracted one by one using the RaiseImageExtractedEvent method
    ExtractedPdfImage[] extractedImages = pdfImagesExtract.ExtractToImageObjects(pdfFile, fromPdfPageNumber, toPdfPageNumber);

    // return if no image was extracted
    if (extractedImages.Length == 0)
        return;

    // get the largest image bytes in a buffer
    byte[] imageBuffer = null;
    try
    {
        // select the largest image
        ExtractedPdfImage largestImage = null;
        for (int i = 0; i < extractedImages.Length; i++)
        {
            if (largestImage == null || extractedImages[i].ImageObject.Size.Width * extractedImages[i].ImageObject.Size.Height >
                largestImage.ImageObject.Size.Width * largestImage.ImageObject.Size.Height)
            {
                largestImage = extractedImages[i];
            }
        }

        // get the .NET Image object
        System.Drawing.Image imageObject = largestImage.ImageObject;

        // get the image data in a buffer
        imageBuffer = GetImageBuffer(imageObject);
    }
    finally
    {
        // dispose the extracted images
        for (int i = 0; i < extractedImages.Length; i++)
            extractedImages[i].Dispose();
    }

    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type", "image/png");

    // let the browser know how to open the image and the image name
    HttpContext.Current.Response.AddHeader("Content-Disposition",
                String.Format("attachment; filename={0}; size={1}", "ExtractedImage", imageBuffer.Length.ToString()));

    // write the image buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(imageBuffer);

    // call End() method of HTTP response to stop ASP.NET page processing
    HttpContext.Current.Response.End();
}
See Also

Other Resources