PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

Add text field to PDF

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.

Document document = new Document();
Page page = new Page( PageSize.Letter );
document.Pages.Add( page );

TextField textField = new TextField( "text1" );
textField.Value = "Hello!";
document.Fields.Add( textField );

Widget widget = new Widget( 200, 400, 200, 50 );
widget.FontSize = 0; // auto-size
widget.TextColor = RgbColor.Red;
widget.BorderColor = RgbColor.Black;
widget.BorderStyle = BorderStyle.Solid;
widget.BorderWidth = 2;
textField.Widgets.Add( widget );
page.Widgets.Add( widget );
         
using ( FileStream file = new FileStream("addtextfield.pdf", FileMode.Create, FileAccess.Write))
{
    document.Write( file );
}
Dim document As New Document()
Dim page As New Page(PageSize.Letter)
document.Pages.Add(page)

Dim textField As New TextField("text1")
textField.Value = "Hello!"
document.Fields.Add(textField)

Dim widget As New Widget(200, 400, 200, 50)
widget.FontSize = 0
' auto-size
widget.TextColor = RgbColor.Red
widget.BorderColor = RgbColor.Black
widget.BorderStyle = BorderStyle.Solid
widget.BorderWidth = 2
textField.Widgets.Add(widget)
page.Widgets.Add(widget)

Using file As New FileStream("addtextfield.pdf", FileMode.Create, FileAccess.Write)
    document.Write(file)
End Using
Download PDFKit.NET 5.0
We will send you a download link

  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.