Add text field to PDF

Manipulate PDF, Shapes, PDF forms
4/13/2011

Downloads

Code sample that shows how to add a text field to an existing PDF document

Add text field to an existing PDF document

This code sample illustrates how you can add TextFields to your PDF. Do take note that you need to add a Widget to your document; in MVC terms the textfield is the model and the Widget is the view.

C# code sample

1 Document document = new Document(); 2 Page page = new Page( PageSize.Letter ); 3 document.Pages.Add( page ); 4 5 TextField textField = new TextField( "text1" ); 6 textField.Value = "Hello!"; 7 document.Fields.Add( textField ); 8 9 Widget widget = new Widget( 200, 400, 200, 50 ); 10 widget.FontSize = 0; // auto-size 11 widget.TextColor = RgbColor.Red; 12 widget.BorderColor = RgbColor.Black; 13 widget.BorderStyle = BorderStyle.Solid; 14 widget.BorderWidth = 2; 15 textField.Widgets.Add( widget ); 16 page.Widgets.Add( widget ); 17 18 using ( FileStream file = new FileStream( @"..\..\addtextfield.pdf", FileMode.Create, FileAccess.Write ) ) 19 { 20 document.Write( file ); 21 }

VB.NET code sample

1 Dim document As New Document() 2 Dim page As New Page(PageSize.Letter) 3 document.Pages.Add(page) 4 5 Dim textField As New TextField("text1") 6 textField.Value = "Hello!" 7 document.Fields.Add(textField) 8 9 Dim widget As New Widget(200, 400, 200, 50) 10 widget.FontSize = 0 11 ' auto-size 12 widget.TextColor = RgbColor.Red 13 widget.BorderColor = RgbColor.Black 14 widget.BorderStyle = BorderStyle.Solid 15 widget.BorderWidth = 2 16 textField.Widgets.Add(widget) 17 page.Widgets.Add(widget) 18 19 Using file As New FileStream("..\..\addtextfield.pdf", FileMode.Create, FileAccess.Write) 20 document.Write(file) 21 End Using