Highlight fields in PDF

Manipulate PDF
5/9/2014

Downloads

This code sample shows how to mark all fields in a PDF document.

Each field will be highlighted with a colored border. We will use a RectangleShape as the border. The color of the border depends on the type of a field.

C# code sample

1 // enumerate the pages 2 foreach (var page in document.Pages) 3 { 4 // enumerate the widgets 5 foreach (var widget in page.Widgets) 6 { 7 //create a highlight shape for each widget 8 var highlight = CreateHighlight(widget); 9 10 if (highlight == null) continue; 11 12 //add the highlight to the page 13 page.Overlay.Add(highlight); 14 } 15 }

VB.NET code sample

1 ' enumerate the pages 2 For Each page As var In document.Pages 3 ' enumerate the widgets 4 For Each widget As var In page.Widgets 5 'create a highlight shape for each widget 6 Dim highlight = CreateHighlight(widget) 7 8 If highlight Is Nothing Then 9 Continue For 10 End If 11 12 'add the highlight to the page 13 page.Overlay.Add(highlight) 14 Next 15 Next

In CreateHighlight routine we create the RectangleShape. The simplest way to create rectangles with different colors is to use conditions(if..else..)

C# code sample

1 private static Shape CreateHighlight(Widget widget) 2 { 3 //create different pens for different field types 4 Pen pen; 5 if (widget.Field is TextField) 6 { 7 pen = new Pen(System.Drawing.Color.Red, 3); 8 } 9 else if (widget.Field is CheckBoxField) 10 { 11 pen = new Pen(System.Drawing.Color.Blue, 3); 12 } 13 else if (widget.Field is RadioButtonField) 14 { 15 pen = new Pen(System.Drawing.Color.Green, 3); 16 } 17 else if (widget.Field is ValueField) 18 { 19 pen = new Pen(System.Drawing.Color.Orange, 3); 20 } 21 else 22 { 23 pen = new Pen(System.Drawing.Color.Yellow, 3); 24 } 25 26 //create a rectangular highlighting 27 return new RectangleShape(widget.Left, widget.Bottom, 28 widget.Width, widget.Height, pen); 29 }

VB.NET code sample

1 Private Shared Function CreateHighlight(widget As Widget) As Shape 2 'create different pens for different field types 3 Dim pen As Pen 4 If TypeOf widget.Field Is TextField Then 5 pen = New Pen(System.Drawing.Color.Red, 3) 6 ElseIf TypeOf widget.Field Is CheckBoxField Then 7 pen = New Pen(System.Drawing.Color.Blue, 3) 8 ElseIf TypeOf widget.Field Is RadioButtonField Then 9 pen = New Pen(System.Drawing.Color.Green, 3) 10 ElseIf TypeOf widget.Field Is ValueField Then 11 pen = New Pen(System.Drawing.Color.Orange, 3) 12 Else 13 pen = New Pen(System.Drawing.Color.Yellow, 3) 14 End If 15 16 'create a rectangular highlighting 17 Return New RectangleShape(widget.Left, widget.Bottom, widget.Width, widget.Height, pen) 18 End Function

Another way is to implement a custom field visitor inherited from the FieldVisitor class.

C#

1 internal class MarkFieldVisitor : FieldVisitor 2 { 3 public override object Visit(TextField field, object argument) 4 { 5 //we passed the widget as argument; 6 var widget = argument as Widget; 7 var pen = new Pen(Color.Red, 3); 8 return CreateHighlight(widget, pen); 9 } 10 11 public override object Visit(CheckBoxField field, object argument) 12 { 13 //we passed the widget as argument; 14 var widget = argument as Widget; 15 var pen = new Pen(Color.Blue, 3); 16 return CreateHighlight(widget, pen); 17 } 18 19 public override object Visit(RadioButtonField field, object argument) 20 { 21 //we passed the widget as argument; 22 var widget = argument as Widget; 23 var pen = new Pen(Color.Green, 3); 24 return CreateHighlight(widget, pen); 25 } 26 27 public override object Visit(ValueField field, object argument) 28 { 29 //we passed the widget as argument; 30 var widget = argument as Widget; 31 var pen = new Pen(Color.Orange, 3); 32 return CreateHighlight(widget, pen); 33 } 34 35 public override object Visit(Field field, object argument) 36 { 37 //we passed the widget as argument; 38 var widget = argument as Widget; 39 var pen = new Pen(Color.Yellow, 3); 40 return CreateHighlight(widget, pen); 41 } 42 43 private static object CreateHighlight(Widget widget, Pen pen) 44 { 45 return new RectangleShape(widget.Left, widget.Bottom, 46 widget.Width, widget.Height, pen); 47 } 48 }

VB.NET code sample

1 Friend Class MarkFieldVisitor 2 Inherits FieldVisitor 3 Public Overrides Function Visit(field As TextField, argument As Object) As Object 4 'we passed the widget as argument; 5 Dim widget = TryCast(argument, Widget) 6 Dim pen = New Pen(Color.Red, 3) 7 Return CreateHighlight(widget, pen) 8 End Function 9 10 Public Overrides Function Visit(field As CheckBoxField, argument As Object) As Object 11 'we passed the widget as argument; 12 Dim widget = TryCast(argument, Widget) 13 Dim pen = New Pen(Color.Blue, 3) 14 Return CreateHighlight(widget, pen) 15 End Function 16 17 Public Overrides Function Visit(field As RadioButtonField, argument As Object) As Object 18 'we passed the widget as argument; 19 Dim widget = TryCast(argument, Widget) 20 Dim pen = New Pen(Color.Green, 3) 21 Return CreateHighlight(widget, pen) 22 End Function 23 24 Public Overrides Function Visit(field As ValueField, argument As Object) As Object 25 'we passed the widget as argument; 26 Dim widget = TryCast(argument, Widget) 27 Dim pen = New Pen(Color.Orange, 3) 28 Return CreateHighlight(widget, pen) 29 End Function 30 31 Public Overrides Function Visit(field As Field, argument As Object) As Object 32 'we passed the widget as argument; 33 Dim widget = TryCast(argument, Widget) 34 Dim pen = New Pen(Color.Yellow, 3) 35 Return CreateHighlight(widget, pen) 36 End Function 37 38 Private Shared Function CreateHighlight(widget As Widget, pen As Pen) As Object 39 Return New RectangleShape(widget.Left, widget.Bottom, widget.Width, widget.Height, pen) 40 End Function 41 End Class

The visitor we can use in the CreateHighlight method:

C#

1 private static Shape CreateHighlight(Widget widget) 2 { 3 var fieldVisitor = new MarkFieldVisitor(); 4 return widget.Field.Accept(fieldVisitor, widget) as Shape; 5 }

VB.NET code sample

1 Private Shared Function CreateHighlight(widget As Widget) As Shape 2 Dim fieldVisitor = New MarkFieldVisitor() 3 Return TryCast(widget.Field.Accept(fieldVisitor, widget), Shape) 4 End Function

The input PDF:

The-orginal-PDF-no-highlight.png

The result:

PDF-with-highlight-fields.png