Back to Devexpress

How to: Extract a Part of the PDF Page as an Image with DevExpress PDF Document API

officefileapi-404321-pdf-document-api-examples-extract-content-from-a-pdf-document-how-to-extract-a-part-of-the-page-as-an-image.md

latest6.5 KB
Original Source

How to: Extract a Part of the PDF Page as an Image with DevExpress PDF Document API

  • Sep 22, 2025
  • 4 minutes to read

The following example describes 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 following code snippet shows a complete code sample:

csharp
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);
    }
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
Imports DevExpress.Drawing

Dim processor As New PdfDocumentProcessor()
processor.LoadDocument("Demo.pdf")
Dim searchResults As PdfTextSearchResults = processor.FindText("The PDF Viewer")
If searchResults IsNot Nothing Then
    ConvertRegionToImage(processor, searchResults.Rectangles(0), searchResults.PageNumber)
End If

Shared Sub ConvertRegionToImage(ByVal processor As PdfDocumentProcessor, ByVal area As PdfOrientedRectangle, ByVal pageNumber As Integer, Optional ByVal scale As Single = 1F)
Imports System

Shared Sub ConvertRegionToImage(ByVal processor As PdfDocumentProcessor, ByVal area As PdfOrientedRectangle, ByVal pageNumber As Integer, Optional ByVal scale As Single = 1F)
    ' Export a page to a bitmap image
    Dim dpi As Single = 72F
    Dim page As PdfPage = processor.Document.Pages(0)
    Dim pageBitmap As DXBitmap = processor.CreateDXBitmap(pageNumber, CInt(Math.Truncate(Math.Max(page.CropBox.Height * scale, page.CropBox.Width * scale))))
    pageBitmap.Save("source.png", DXImageFormat.Png)

    ' Calculate the area width and height in pixels
    Dim widthInPixels As Single = Units.PointsToPixelsF(CSng(area.Width) * scale, dpi)
    Dim heightInPixels As Single = Units.PointsToPixelsF(CSng(area.Height) * scale, dpi)

    ' Calculate the area's top left coordinates 
    Dim x As Integer = Convert.ToInt32(Units.PointsToPixelsF(CSng(area.Left) * scale, dpi))
    Dim y As Integer = Convert.ToInt32(Units.PointsToPixelsF(CSng(page.CropBox.Height - area.Top) * scale, dpi))

    Using bitmap As New DXBitmap(CInt(Math.Truncate(widthInPixels)), CInt(Math.Truncate(heightInPixels)))
        ' Declare an area into which the extracted portion
        ' should be drawn
        Dim destRect As New Rectangle(0, 0, bitmap.Width, bitmap.Height)

        ' Declare an area to extract from the page
        Dim sourceRect As New Rectangle(x, y, bitmap.Width, bitmap.Height)
        Using graphics As DXGraphics = DXGraphics.FromImage(bitmap)
            ' Export a page area to a bitmap
            graphics.DrawImage(pageBitmap, destRect, sourceRect)
        End Using
        bitmap.Save("result.png", DXImageFormat.Png)
    End Using
End Sub