Working with Image Extraction

23 Jul 202611 minutes to read

Syncfusion® PDF provides support to extract images from a particular page or from an entire PDF document. You can extract images from a page using the ExtractImages method of the PdfPageBase class. For lower memory consumption, use the PdfDocumentExtractor class.

Extracting images from a PDF page

The following code example shows how to extract images from a specific page of a PDF document.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;

//Load the PDF document.
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
//Load the first page.
PdfPageBase pageBase = loadedDocument.Pages[0];

//Extract images from the first page.
Stream[] extractedImages = pageBase.ExtractImages();

//Save the extracted images to disk.
for (int i = 0; i < extractedImages.Length; i++)
{
    FileStream outputStream = new FileStream($"Image{i + 1}.png", FileMode.Create, FileAccess.Write);
    extractedImages[i].CopyTo(outputStream);
    outputStream.Close();
    extractedImages[i].Close();
}

//Close the document.
loadedDocument.Close(true);
inputStream.Close();
using System.Drawing;
using System.Drawing.Imaging;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;

//Load an existing PDF.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase pageBase = loadedDocument.Pages[0];

//Extract images from the first page.
Image[] extractedImages = pageBase.ExtractImages();

//Save the extracted images to disk.
for (int i = 0; i < extractedImages.Length; i++)
{
    extractedImages[i].Save($"Image{i + 1}.png", ImageFormat.Png);
}

//Close the document.
loadedDocument.Close(true);
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Exporting
Imports Syncfusion.Pdf.Parsing

'Load an existing PDF.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Load the first page.
Dim pageBase As PdfPageBase = loadedDocument.Pages(0)

'Extract images from the first page.
Dim extractedImages As Image() = pageBase.ExtractImages()

'Save the extracted images to disk.
For i As Integer = 0 To extractedImages.Length - 1
    extractedImages(i).Save($"Image{i + 1}.png", ImageFormat.Png)
Next

'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

NOTE

To extract images from PDF page in .NET Core application, add the Syncfusion.Pdf.Imaging.Net.Core package to your project.

Extracting image information

To extract the image properties such as bounds, image index, and more from a page, you can use the ImagesInfo property of the PdfPageBase class. The property returns an array of PdfImageInfo objects, each of which contains the image bounds, the page index, and a reference to the extracted image stream.

The following code example shows how to extract image information from a PDF page.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;

//Load the PDF document.
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
//Load the first page.
PdfPageBase pageBase = loadedDocument.Pages[0];

//Extract the image information from the first page.
PdfImageInfo[] imagesInfo = pageBase.ImagesInfo;

//Iterate through each image and access its details.
foreach (PdfImageInfo info in imagesInfo)
{
    System.Console.WriteLine($"Bounds: {info.Bounds}, Index: {info.ImageIndex}");
}

//Close the document.
loadedDocument.Close(true);
inputStream.Close();
using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Exporting;
using Syncfusion.Pdf.Parsing;

//Load an existing PDF.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase pageBase = loadedDocument.Pages[0];

//Extract the image information from the first page.
PdfImageInfo[] imagesInfo = pageBase.ImagesInfo;

//Iterate through each image and access its details.
foreach (PdfImageInfo info in imagesInfo)
{
    System.Console.WriteLine($"Bounds: {info.Bounds}, Index: {info.ImageIndex}");
}

//Close the document.
loadedDocument.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Exporting
Imports Syncfusion.Pdf.Parsing

'Load an existing PDF.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Load the first page.
Dim pageBase As PdfPageBase = loadedDocument.Pages(0)

'Extract the image information from the first page.
Dim imagesInfo As PdfImageInfo() = pageBase.ImagesInfo

'Iterate through each image and access its details.
For Each info As PdfImageInfo In imagesInfo
    System.Console.WriteLine($"Bounds: {info.Bounds}, Index: {info.ImageIndex}")
Next

'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Extract images from PDF documents with better memory consumption and performance

The PdfDocumentExtractor class extracts images from an entire PDF document using a streaming approach, which keeps memory usage low even for large documents. The class exposes the PageCount property and the ExtractImages() overloads, which allow you to extract images from the entire document or from a specific page range.

The following code example illustrates how to extract images from an entire PDF document.

using System.IO;
using Syncfusion.Pdf.Parsing;

//Get stream from an existing PDF document.
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Initialize the PDF document extractor.
PdfDocumentExtractor documentExtractor = new PdfDocumentExtractor();
//Load the PDF document.
documentExtractor.Load(inputStream);
//Get the page count.
int pageCount = documentExtractor.PageCount;

//Extract images from the entire PDF document.
Stream[] images = documentExtractor.ExtractImages();

//Extract images by page range (pages 2 through 6).
Stream[] streams = documentExtractor.ExtractImages(2, 6);

//Save the extracted images to disk.
for (int i = 0; i < images.Length; i++)
{
    FileStream outputStream = new FileStream($"Image{i + 1}.png", FileMode.Create, FileAccess.Write);
    images[i].CopyTo(outputStream);
    outputStream.Close();
    images[i].Close();
}

//Release all resources used by the PDF image extractor.
documentExtractor.Dispose();
inputStream.Close();
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Syncfusion.Pdf.Parsing;

//Get stream from an existing PDF document.
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Initialize the PDF document extractor.
PdfDocumentExtractor documentExtractor = new PdfDocumentExtractor();
//Load the PDF document.
documentExtractor.Load(inputStream);
//Get the page count.
int pageCount = documentExtractor.PageCount;

//Extract images from the entire PDF document.
Image[] images = documentExtractor.ExtractImages();

//Extract images by page range (pages 2 through 6).
Image[] streams = documentExtractor.ExtractImages(2, 6);

//Save the extracted images to disk.
for (int i = 0; i < images.Length; i++)
{
    images[i].Save($"Image{i + 1}.png", ImageFormat.Png);
}

//Release all resources used by the PDF image extractor.
documentExtractor.Dispose();
inputStream.Close();
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Syncfusion.Pdf.Parsing

'Get stream from an existing PDF document.
Dim inputStream As FileStream = New FileStream("Input.pdf", FileMode.Open, FileAccess.Read)
'Initialize the PDF document extractor.
Dim documentExtractor As PdfDocumentExtractor = New PdfDocumentExtractor()
'Load the PDF document.
documentExtractor.Load(inputStream)
'Get the page count.
Dim pageCount As Integer = documentExtractor.PageCount

'Extract images from the entire PDF document.
Dim images As Image() = documentExtractor.ExtractImages()

'Extract images by page range.
Dim streams As Image() = documentExtractor.ExtractImages(2, 6)

'Save the extracted images to disk.
For i As Integer = 0 To images.Length - 1
    images(i).Save($"Image{i + 1}.png", ImageFormat.Png)
Next

'Release all resources used by the PDF image extractor.
documentExtractor.Dispose()
inputStream.Close()

You can download a complete working sample from GitHub.

NOTE

To extract images from PDF page in .NET Core application, add the Syncfusion.Pdf.Imaging.Net.Core package to your project.

Troubleshooting and FAQs

Image extraction fails on cross-platform targets

Issue Image extraction throws a PlatformNotSupportedException or returns no images on Linux, macOS, or other non-Windows targets.
Reason Some image formats used in PDF documents rely on platform-specific native libraries. The default Syncfusion® PDF package does not include the imaging helpers required for cross-platform scenarios.
Solution Add the Syncfusion.Pdf.Imaging.Net.Core NuGet package to your project. This package provides the required image decoders for .NET Core, .NET, Xamarin, and Blazor applications.

Missing SkiaSharp native assets on Ubuntu ARM64

Issue Image extraction fails on Ubuntu 22.04.5 LTS servers running on ARM64 architecture due to missing SkiaSharp native dependencies.
Reason SkiaSharp requires platform-specific native binaries for graphics operations:
  1. The default SkiaSharp package does not include ARM64 Linux binaries.
  2. Ubuntu ARM64 environments lack these native assets by default.
  3. SkiaSharp fails to initialize without these dependencies.
Solution Add the appropriate native assets package based on your environment:
  1. For standard Linux environments (Ubuntu, Alpine, CentOS, Debian, Fedora, RHEL, Azure App Service, Google App Engine):
  2. C#
  3. dotnet add package SkiaSharp.NativeAssets.Linux --version 3.116.1
  4. For cloud native deployments (AWS Lambda, AWS Elastic Beanstalk):
  5. C#
  6. dotnet add package SkiaSharp.NativeAssets.Linux.NoDependencies --version 3.116.1
For Ubuntu 22.04.5 LTS on ARM64, use SkiaSharp.NativeAssets.Linux. Verify your .csproj file contains the following entry:
  • XML
  • &lt;PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" /&gt;