How to Extract Image From PDF in C#?

PDF is an ISO-standardized version of the Portable Document Format (PDF) specialized for the digital preservation of electronic documents. PDF document can be inserted images with numbers of formats like format of Bmp, PNG, Tiff and Jpeg and so on. For many developers, to extract the images out from PDF in programming is not easy. While, it is sure that Spire.PDF for NET is an efficient tool to realize this function in .NET application. It supports applications types including Console platform, WinForm and Asp.net

Firstly, download the Spire.PDF for .NET. The following page14 will be selected as an example by using Spire.PDF for .NET:

PDF image

Step 1: To create a console application, and set its target framework as .NET 2.0. Then, reference assemblies such as System. Drawing.dll. Spire.Pdf.dll.

Step 2: Instantiate an object of Spire.Pdf.PdfDocument

[C#]
PdfDocument doc = new PdfDocument();

Step 3: Load a PDF document

[C#]
doc.LoadFromFile("sample.pdf");

Step 4: Get an object of Spire.Pdf.PdfPageBase, and call its method of ExtractImages to extract the images.

[C#]
PdfPageBase page = doc.Pages[i];
Image[] images = page.ExtractImages();

Check the final image of page 14 which has already been extracted out from the PDF file:

PDF image

The following code snippet reveals all the code while extracting images from PDF:

[C#]
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Spire.Pdf;

namespace ExtractImagesFromPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate an object of Spire.Pdf.PdfDocument
            PdfDocument doc = new PdfDocument();
            //Load a PDF file 
            doc.LoadFromFile("sample.pdf");
            List<Image> ListImage = new List<Image>();
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // Get an object of Spire.Pdf.PdfPageBase
                PdfPageBase page = doc.Pages[i];
                // Extract images from Spire.Pdf.PdfPageBase
                Image[] images = page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    ListImage.AddRange(images);
                }

            }
            if (ListImage.Count > 0)
            {
                for (int i = 0; i < ListImage.Count; i++)
                {
                    Image image = ListImage[i];
                    image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
                System.Diagnostics.Process.Start("image1.png");
            }


        }
    }
}