Back to Devexpress

How to: Replace a Form Field with an Image

officefileapi-120571-pdf-document-api-examples-interactive-form-how-to-replace-a-form-field-with-an-image.md

latest6.8 KB
Original Source

How to: Replace a Form Field with an Image

  • Nov 07, 2025
  • 5 minutes to read

This topic describes how to substitute an interactive form field in a PDF document with an image. To do this, remove the required form field from the document and display an image instead of the deleted field. Form field removal is necessary, because widget annotation is drawn over page content and can overlap an image added at the form field’s position.

Follow the steps below to replace a form field with an image that maintains the same size and position.

  1. Use the PdfAcroFormFacade.GetFormField method to obtain a required form field. Use the PdfDocumentFacade.AcroForm property to access the PdfAcroFormFacade object.

  2. Use the PdfWidgetFacade.Rectangle property to obtain widget annotations and get the annotation rectangle (defines the annotation location on the page). Note that this property is measured in default user space units.

  3. Use the PdfWidgetFacade.PageNumber property to get the page where the annotation is located. Once you obtain that page, use the PdfPageTreeObject.CropBox property to get the page boundaries (the crop box). Note that the crop box uses the user coordinate system.

  4. Call the PdfGraphics.DrawImage method to draw an image at the form field’s position.

  5. Use the PdfGraphics.AddToPageForeground method to add graphics to the PDF page’s foreground. Pass 72 as DPI values to this method to transform world coordinates to page coordinates without scaling.

  6. Call the PdfDocumentProcessor.RemoveFormField method to remove the form field.

View Example: How to replace a form field with an image

csharp
using System.Drawing;
using DevExpress.Pdf;

static void Main(string[] args)
{
    const float dpiX = 72f;
    const float dpiY = 72f;

  using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {

    // Load a PDF document with AcroForm data.
    processor.LoadDocument("..\\..\\InteractiveForm.pdf");
    PdfDocumentFacade documentFacade = processor.DocumentFacade;
    PdfAcroFormFacade acroForm = documentFacade.AcroForm;
    string fieldName = "Address";

    // Obtain the required form field
    PdfTextFormFieldFacade formField = acroForm.GetFormField(fieldName) as PdfTextFormFieldFacade;
    if (formField == null) return;

    foreach (PdfWidgetFacade widget in formField) {
        PdfRectangle rect = widget.Rectangle;
        PdfPage page = processor.Document.Pages[widget.PageNumber - 1];
        double x = (rect.Left - page.CropBox.Left)/72 * dpiX;
        double y = (page.CropBox.Top - rect.Bottom)/72 * dpiY;

        // Create graphics and draw an image.
        using (PdfGraphics graphics = processor.CreateGraphicsWorld(dpiX, dpiY))
        {
            DrawImage(graphics, rect, x, y);
            graphics.AddToPageForeground(page, dpiX, dpiY);
        }
    }
    processor.RemoveFormField(fieldName);
    processor.SaveDocument("..\\..\\Result.pdf");
  }
}

static void DrawImage(PdfGraphics graphics, PdfRectangle rect, double x, double y) {

    DXImage image = DXImage.FromStream(new FileStream("..\\..\\AddressFormField.png", FileMode.Open));

    double aspectRatio = image.Width / image.Height;

    double scaleX = image.Width / rect.Width;
    double scaleY = image.Height / rect.Height;

    double width;
    double height;

    if (scaleX < scaleY) {

        width = rect.Width;
        height = width / aspectRatio;
    }

    else {
        height = rect.Height;
        width = height * aspectRatio;
    }

    // Calculate the rectangle
    // to use in the world coordinate system
    RectangleF imageRect = new RectangleF((float)x, (float)(y - height), (float)width, (float)height);
    graphics.DrawImage(image, imageRect);
}
vb
Imports System.Drawing
Imports DevExpress.Pdf

Shared Sub Main(ByVal args() As String)
    Const dpiX As Single = 72F
    Const dpiY As Single = 72F
  Using processor As New PdfDocumentProcessor()

   ' Load a PDF document with AcroForm data.
    Dim documentFacade As PdfDocumentFacade = processor.DocumentFacade
    Dim acroForm As PdfAcroFormFacade = documentFacade.AcroForm
    Dim fieldName As String = "Address"

    ' Obtain the required form field
    Dim formField As PdfTextFormFieldFacade = TryCast(acroForm.GetFormField(fieldName), PdfTextFormFieldFacade)
    If formField Is Nothing Then
        Return
    End If

    For Each widget As PdfWidgetFacade In formField
        Dim rect As PdfRectangle = widget.Rectangle
        Dim page As PdfPage = processor.Document.Pages(widget.PageNumber - 1)
        Dim x As Double = (rect.Left - page.CropBox.Left)/72 * dpiX
        Dim y As Double = (page.CropBox.Top - rect.Bottom)/72 * dpiY

        ' Create graphics and draw an image.
        Using graphics As PdfGraphics = processor.CreateGraphicsWorld(dpiX, dpiY)
            DrawImage(graphics, rect, x, y)

            graphics.AddToPageForeground(page)
        End Using

    Next widget
    processor.RemoveFormField(fieldName)
    processor.SaveDocument("..\..\Result.pdf")
  End Using
End Sub

Shared Sub DrawImage(ByVal graphics As PdfGraphics, ByVal rect As PdfRectangle, ByVal x As Double, ByVal y As Double)

    Dim image As DXImage = DXImage.FromStream(New FileStream("..\..\AddressFormField.png", FileMode.Open))
    Dim aspectRatio As Double = image.Width \ image.Height

    Dim scaleX As Double = image.Width \ rect.Width
    Dim scaleY As Double = image.Height \ rect.Height

    Dim width As Double
    Dim height As Double

    If scaleX < scaleY Then

        width = rect.Width
        height = width / aspectRatio

    Else
        height = rect.Height
        width = height * aspectRatio
    End If

    ' Calculate the rectangle
    ' to use in the world coordinate system
    Dim imageRect As New RectangleF(CSng(x), CSng(y - height), CSng(width), CSng(height))
    graphics.DrawImage(image, imageRect)
End Sub

See Also

Coordinate Systems