DEV Community

Jeremy K.
Jeremy K.

Posted on

How to Extract Images from PDF Using C# (Single Page & Full Document)

In .NET development, extracting images from PDF files with C# is a top request—whether you’re migrating data (needing to separate charts from documents), analyzing content (pulling key illustrations), or archiving visuals. Yet traditional methods fall short: paid PDF libraries are costly, and heavy tools like Adobe Acrobat require manual work, slowing down development for small-to-medium projects.

This guide shows you how to use Free Spire.PDF for .NET—a lightweight, free library—to implement two critical use cases: extracting images from entire PDFs and targeting specific pages. No complex parsing, no expensive tools—just copy-pasteable C# code and actionable steps.


1. Why Extract PDF Images in C#? Common .NET Use Cases

Before diving into code, let’s align with your goals: these are the most frequent scenarios where C# PDF image extraction adds value:

  • Data Migration: Strip charts/graphs from legacy PDFs to import into databases or new software (e.g., CRM systems).
  • Content Analysis: Extract product images from catalog PDFs or research visuals from academic papers for AI/ML processing.
  • Report Automation: Pull cover images or section graphics from PDFs to embed in .NET-generated reports (e.g., Excel, Word).
  • Archiving: Organize PDF visuals into separate folders (e.g., "2025_Q1_Report_Images") for easy retrieval.

2. Set Up the Free .NET Library

To get started, you’ll need to install Free Spire.PDF. NuGet is the fastest way (no manual file downloads):

  1. Open Visual Studio → Right-click your project → Select Manage NuGet Packages.
  2. Go to the Browse tab → Search for Free Spire.PDF.
  3. Click Install → Wait for completion (Visual Studio auto-adds references).

⚠️ Note: The free version supports PDFs with up to 10 pages.


3. C# PDF Image Extraction: 2 Key Scenarios (Code + Explanations)

Below are the two most common use cases.

3.1 Scenario 1: Extract All Images from a PDF Document

Ideal for: Archiving all visuals in a PDF (e.g., a 10-page product catalog) or batch processing multiple files.

How It Works

The core logic uses PdfImageHelper.GetImagesInfo() to fetch image data from each page, then PdfImageInfo.Image.Save() to store files locally.

Full C# Code

using Spire.Pdf;
using Spire.Pdf.Utilities;
using System.Drawing;

namespace ExtractAllImages
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Load the target PDF document
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Input.pdf"); // Replace with your PDF path

            // 2. Initialize the image processing tool
            PdfImageHelper imageHelper = new PdfImageHelper();

            // 3. Traverse all pages and extract images
            int imageCount = 0; // Used to name images to avoid duplicates
            for (int pageIndex = 0; pageIndex < pdf.Pages.Count; pageIndex++)
            {
                // Get the current page object
                PdfPageBase currentPage = pdf.Pages[pageIndex];
                // Get all image information of the current page
                PdfImageInfo[] imageInfos = imageHelper.GetImagesInfo(currentPage);

                // Save images of the current page
                foreach (var imageInfo in imageInfos)
                {
                    Image image = imageInfo.Image;
                    // The save path can be customized
                    image.Save($"Output\\image_{imageCount}.png"); 
                    imageCount++;
                }
            }

            // 4. Release resources
            pdf.Dispose();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3.2 Scenario 2: Extract Images from Specific PDF Pages

Ideal for: Pulling a single chart (e.g., page 5 of a financial report) or cover image (page 1) without processing the entire file.

Full C# Code

using Spire.Pdf;
using Spire.Pdf.Utilities;
using System.Drawing;

namespace ExtractImagesFromSpecificPage
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Load the target PDF document
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Input.pdf"); // Replace with your PDF path

            // 2. Locate the specified page (take page 1 as an example, index is 0)
            int targetPageIndex = 0; // To extract page 3, change to 2
            PdfPageBase targetPage = pdf.Pages[targetPageIndex];

            // 3. Initialize the image processing tool and get image information
            PdfImageHelper imageHelper = new PdfImageHelper();
            PdfImageInfo[] imageInfos = imageHelper.GetImagesInfo(targetPage);

            // 4. Save images of the specified page
            for (int i = 0; i < imageInfos.Length; i++)
            {
                Image image = imageInfos[i].Image;
                // The save path can be customized
                image.Save($"Output\\page_{targetPageIndex + 1}_image_{i}.png"); 
            }

            // 5. Release resources
            pdf.Dispose();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Common Issues & Fixes

Issue Root Cause Fix
"File not found" error Output folder doesn’t exist Add Directory.CreateDirectory(outputFolder); .
Blurry images Outdated .NET framework Target .NET 5+ (older versions compress high-res images).
"Index out of range" Invalid page index The page index in Free Spire.PDF starts from 0
Code won’t compile Missing Spire.Pdf reference Reinstall via NuGet or re-add the DLL manually

5. Conclusion: Start Extracting PDF Images in C# Today

Free Spire.PDF for .NET eliminates the pain of C# PDF image extraction: no costly libraries, no heavy tools, and no complex code. Whether you’re batch-processing entire documents or targeting specific pages, this solution is fast, free, and easy to integrate into .NET projects.

Next Steps:

  1. Install Free Spire.PDF via NuGet.
  2. Copy the code from Scenario 1 or 2.
  3. Customize the pdfPath and outputFolder—run the project.

Top comments (0)