Skip to main content
All docs
V24.1

How to: Extract a Part of the PDF Page as an Image

  • 4 minutes to read

The following example demonstrates how to use the PdfDocumentProcessor and DXGraphics classes to extract a part of the PDF page as an image.

In this example, the search result is exported as a Bitmap image. Call the PdfDocumentProcessor.FindText to find a specific phrase.

The DXGraphics.DrawImage(DXImage, RectangleF, RectangleF) method overload allows you to to export a part of the PDF page as an image. This method requires the following parameters:

  • An image from which to extract a portion.
  • The size and location of the resulting image.
  • An image area to extract.

The sections below describe how to calculate each parameter.

Convert a Page to an Image

Call the PdfDocumentProcessor.CreateDXBitmap method to export a page to a bitmap image. You can declare an optional scale parameter to scale the page.

Calculate the Extraction Area

The FindText method returns a PdfTextSearchResults instance. The PdfTextSearchResults.Rectangles property returns a list of areas occupied by the result entries. These areas is measured in PDF page units (each point is 1/72 of an inch). Call the Units.PointsToPixelsF method to convert the rectangle coordinates to pixels. Multiply the coordinates by the scale parameter declared earlier to calculate the coordinates correctly.

Calculate The Resulting Image Size

Create a new DevExpress.Drawing.DXBitmap instance to draw a page area into it. Use the width and height calculated earlier so that the extracted portion fits onto the image canvas.

Draw an Image

Pass the exported page and the calculated rectangles as the DXGraphics.DrawImage method parameters. The code sample below shows a complete code sample:

using DevExpress.Pdf;
using System.Drawing;
using DevExpress.Drawing;

PdfDocumentProcessor processor = new PdfDocumentProcessor();
processor.LoadDocument(@"Demo.pdf");
PdfTextSearchResults searchResults = processor.FindText("The PDF Viewer");
if (searchResults != null)
    ConvertRegionToImage(processor, searchResults.Rectangles[0], searchResults.PageNumber);

static void ConvertRegionToImage(PdfDocumentProcessor processor, PdfOrientedRectangle area,
     int pageNumber, float scale = 1f) {

    // Export a page to a bitmap image
    float dpi = 72f;
    PdfPage page = processor.Document.Pages[0];
    DXBitmap pageBitmap = processor.CreateDXBitmap(pageNumber,
         (int)Math.Max(page.CropBox.Height * scale, page.CropBox.Width * scale));
    pageBitmap.Save("source.png", DXImageFormat.Png);

    // Calculate the area width and height in pixels
    float widthInPixels = Units.PointsToPixelsF((float)area.Width * scale, dpi);
    float heightInPixels = Units.PointsToPixelsF((float)area.Height * scale, dpi);

    // Calculate the area's top left coordinates 
    int x = Convert.ToInt32(Units.PointsToPixelsF((float)area.Left * scale, dpi));
    int y = Convert.ToInt32(Units.PointsToPixelsF((float)(page.CropBox.Height - area.Top)
         * scale, dpi));

    using (DXBitmap bitmap = new DXBitmap((int)widthInPixels, (int)heightInPixels))
    {
        // Declare an area into which the extracted portion
        // should be drawn
        Rectangle destRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

        // Declare an area to extract from the page
        Rectangle sourceRect = new Rectangle(x, y, bitmap.Width, bitmap.Height);
        using (DXGraphics graphics = DXGraphics.FromImage(bitmap))
        {
            // Export a page area to a bitmap
            graphics.DrawImage(pageBitmap, destRect, sourceRect);
        }
        bitmap.Save("result.png", DXImageFormat.Png);
    }
}