Skip to main content

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

  • 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.

pdf document api extract images with boundaries

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.CreateGraphics();
            using (var pen = new DXPen(Color.Red, 5))
                graphics.DrawRectangle(pen, new RectangleF(rectFLeft, rectFTop, rectFWidth, rectFHeight));
                graphics.AddToPageForeground(processor.Document.Pages[1], 72, 72);
        }
        // Print the resulting document.
        DXBitmap bitmap = processor.CreateDXBitmap(1, 1000);
        PdfPrinterSettings pdfPrinterSettings = new PdfPrinterSettings();
        processor.Print(pdfPrinterSettings);
    }
}

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

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());
    }
}
See Also