Back to Devexpress

How to: Extract Images from a Document with DevExpress PDF Document API

officefileapi-17588-pdf-document-api-examples-extract-content-from-a-pdf-document-how-to-extract-images-from-a-document.md

latest7.5 KB
Original Source

How to: Extract Images from a Document with DevExpress PDF Document API

  • Sep 22, 2025
  • 4 minutes to read

The following help topic explains how to use PdfDocumentProcessor.GetDXImages and PdfDocumentProcessor.GetImagesInfo methods to retrieve images from a document in Windows and non-Windows environments.

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

Obtain and Manage Image Coordinates

The following example uses the PdfDocumentProcessor.GetImagesInfo(PdfDocumentArea) method to obtain all images and their boundaries from the specified page and to draw a red rectangle around them.

csharp
using DevExpress.Drawing;
using DevExpress.Pdf;
using System.Collections.Generic;
using System.Drawing;
// ...
static void Main(string[] args) {

    using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
        processor.LoadDocument(@"..\\..\\Demo.pdf");
        // Specify the area from which the images are obtained.
        PdfDocumentArea area = new PdfDocumentArea(1,
            new PdfRectangle(0, 0, processor.Document.Pages[0].CropBox.Width,
                processor.Document.Pages[0].CropBox.Height));
        // Retrieve images and their boundaries.
        IList<PdfBitmapBox> bitmaps = processor.GetImagesInfo(area);
        // Draw a rectangle around boundaries of each image.
        for (int i = 0; i < bitmaps.Count; i++) {
            var r = bitmaps[i].Bounds;
            float rectFLeft = (float)r.Left;
            float rectFTop = (float)(processor.Document.Pages[0].CropBox.Height - r.Top);
            float rectFWidth = (float)r.Width;
            float rectFHeight = (float)r.Height;

            PdfGraphics graphics = processor.CreateGraphicsWorldSystem();
            using (var pen = new DXPen(Color.Red, 5))
                graphics.DrawRectangle(pen, new RectangleF(rectFLeft, rectFTop, rectFWidth, rectFHeight));
                graphics.AddToPageForeground(processor.Document.Pages[1]);
        }
        // Print the resulting document.
        DXBitmap bitmap = processor.CreateDXBitmap(1, 1000);
        PdfPrinterSettings pdfPrinterSettings = new PdfPrinterSettings();
        processor.Print(pdfPrinterSettings);
    }
}
vb
Imports DevExpress.Drawing
Imports DevExpress.Pdf
Imports System.Collections.Generic
Imports System.Drawing
' ...
Shared Sub Main(ByVal args() As String)
    Using processor As New PdfDocumentProcessor()
        processor.LoadDocument("..\\..\\Demo.pdf")
        ' Specify the area from which the images are obtained.
        Dim area As New PdfDocumentArea(1, New PdfRectangle(0, 0, processor.Document.Pages(0).CropBox.Width,
            processor.Document.Pages(0).CropBox.Height))
        ' Retrieve images and their boundaries.
        Dim bitmaps As IList(Of PdfBitmapBox) = processor.GetImagesInfo(area)
        ' Draw a rectangle around boundaries of each image.
        For i As Integer = 0 To bitmaps.Count - 1
            Dim r = bitmaps(i).Bounds
            Dim rectFLeft As Single = CSng(r.Left)
            Dim rectFTop As Single = CSng(processor.Document.Pages(0).CropBox.Height - r.Top)
            Dim rectFWidth As Single = CSng(r.Width)
            Dim rectFHeight As Single = CSng(r.Height)

            Dim graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            Using pen = New DXPen(Color.Red, 5)
                graphics.DrawRectangle(pen, New RectangleF(rectFLeft, rectFTop, rectFWidth, rectFHeight))
            End Using
                graphics.AddToPageForeground(processor.Document.Pages(1))
        Next i
        ' Print the resulting document.
        Dim bitmap As DXBitmap = processor.CreateDXBitmap(1, 1000)
        Dim pdfPrinterSettings As New PdfPrinterSettings()
        processor.Print(pdfPrinterSettings)
    End Using
End Sub

Extract Images from the Specified Area

This example illustrates the use of the PdfDocumentProcessor.GetDXImages method to obtain images from a PDF file.

The following code retrieves images from the specified page area:

View Example

csharp
using System;
using System.Collections.Generic;
using DevExpress.Drawing;
using DevExpress.Pdf;
// ...
PdfDocumentProcessor processor = new PdfDocumentProcessor();
processor.LoadDocument(@"..\\..\\Demo.pdf");

int xCount = 8; 
int yCount = 2;
double cardWidth = 150.5; // Measured in points (equals 2.09 inches).
double cardHeight = 442; // Measured in points (equals 6.138 inches).
double xMargin = 122; // Measured in points (equals 1.694 inches).
double yMargin = 77; // Measured in points (equals 1.069 inches).
double yCoord = yMargin;

for (int y = 0; y < yCount; y++, yCoord += cardHeight) {
    double xCoord = xMargin;

    for (int x = 0; x < xCount; x++, xCoord += cardWidth) {

        PdfDocumentArea area = new PdfDocumentArea(1,
            new PdfRectangle(xCoord, yCoord, xCoord + cardWidth, yCoord + cardHeight));
        IList<DXBitmap> bitmaps = processor.GetDXImages(area);
        if (bitmaps.Count != 0) {
            bitmaps[0].Save(String.Format(@"{0}_{1}.bmp", x, y), DXImageFormat.Bmp);
            bitmaps[0].Dispose();
        }
        Console.WriteLine(bitmaps.Count.ToString());
    }
}
vb
Imports DevExpress.Drawing
Imports DevExpress.Pdf

Shared Sub Main(ByVal args() As String)
    Dim processor As New PdfDocumentProcessor()
    processor.LoadDocument("..\\..\\Demo.pdf")

    Dim xCount As Integer = 8
    Dim yCount As Integer = 2
    Dim cardWidth As Double = 150.5 ' Measured in points (equals 2.09 inches).
    Dim cardHeight As Double = 442 ' Measured in points (equals 6.138 inches).
    Dim xMargin As Double = 122 ' Measured in points (equals 1.694 inches).
    Dim yMargin As Double = 77 ' Measured in points (equals 1.069 inches).
    Dim yCoord As Double = yMargin

    Dim y As Integer = 0
    Do While y < yCount
        Dim xCoord As Double = xMargin
        Dim x As Integer = 0
        Do While x < xCount
            Dim area As New PdfDocumentArea(1, New PdfRectangle(xCoord, yCoord, xCoord + cardWidth, yCoord + cardHeight))
            Dim bitmaps As IList(Of DXBitmap) = processor.GetDXImages(area)
            If bitmaps.Count <> 0 Then
                bitmaps(0).Save(String.Format("{0}_{1}.bmp", x, y), DXImageFormat.Bmp)
                bitmaps(0).Dispose()
            End If
            Console.WriteLine(bitmaps.Count.ToString())
            x += 1
            xCoord += cardWidth
        Loop
        y += 1
        yCoord += cardHeight
    Loop
End Sub

See Also

How to: Extract Text from a Document with DevExpress PDF Document API