How to: Replace a Form Field with an Image
- 4 minutes to read
This topic describes how to substitute an interactive form field in a PDF document with an image. To perform this task, 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 the 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.
- Iterate through the PdfInteractiveForm.Fields collection (PdfDocumentProcessor.Document.AcroForm.Fields) to obtain a form field you want to replace.
- Use the PdfInteractiveFormField.Widget property to access widget annotation associated with the form field. The PdfAnnotation.Page property returns the page where this widget annotation is located.
- Obtain the page boundaries and the widget’s size and position on the page using the PdfPage.CropBox (PdfPageTreeObject.CropBox) and PdfAnnotation.Rect properties, respectively.
Call the PdfGraphics.DrawImage method to draw an image at the form field’s position.
Note that PdfGraphics uses the world coordinate system, whereas the PdfAnnotation.Page and PdfPageTreeObject.CropBox properties work with the user coordinate system (refer to the Coordinate Systems article for more information on coordinate systems in the PDF Document API). The following formula is used to convert user coordinates to world coordinates.
UnitX = ((rect.Left – cropBox.Left) / 72) * dpiX;
UnitY = ((cropBox.Top – rect.Bottom) / 72 ) * dpiY;
where
PdfRectangle rect = field.Widget.Rect;
PdfRectangle cropBox = field.Widget.Page.CropBox;
This code example uses 72 for dpiX and dpiY values to simplify the formula above.
- 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.
- Remove the form field by calling the PdfDocumentProcessor.RemoveFormField method.
Note
Show Me A complete sample project is available on GitHub at https://github.com/DevExpress-Examples/how-to-replace-form-field-with-image.
using System.Drawing;
using DevExpress.Pdf;
namespace ReplaceFormFieldWithImage {
class Program {
static void Main(string[] args) {
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load a PDF document with AcroForm data.
processor.LoadDocument("..\\..\\InteractiveForm.pdf");
foreach (PdfInteractiveFormField field in processor.Document.AcroForm.Fields) {
if (field.Name == "Address") {
PdfRectangle cropBox = field.Widget.Page.CropBox;
PdfRectangle rect = field.Widget.Rect;
double x = rect.Left - cropBox.Left;
double y = cropBox.Top - rect.Bottom;
// Create graphics and draw an image instead of the form field.
using (PdfGraphics graphics = processor.CreateGraphics()) {
DrawImage(graphics, rect, x, y);
graphics.AddToPageForeground(processor.Document.Pages[0], 72, 72);
}
}
}
// Remove the form field.
processor.RemoveFormField("Address");
// Save the document.
processor.SaveDocument("..\\..\\Result.pdf");
}
}
static void DrawImage(PdfGraphics graphics, PdfRectangle rect, double x, double y) {
Bitmap image = new Bitmap("..\\..\\AddressFormField.png");
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;
}
RectangleF imageRect = new RectangleF((float)x, (float)(y - height), (float)width, (float)height);
graphics.DrawImage(image, imageRect);
}
}
}