Create Text Form Field

  • Reference KB000167
  • Type Code sample
  • Product TallPDF.NET
  • Categories Generate PDF, PDF forms
  • Created 5/8/2012

This code sample shows how to use the TextFieldShape class to add text form fields to your generated PDF document.


Document pdf = new Document();
using (FileStream file = new FileStream(
   @"..\..\out.pdf", FileMode.Create, FileAccess.Write))
{
   Section section = pdf.Sections.Add();
   section.PageSize = PageSize.A4;

   TextParagraph text = new TextParagraph();
   section.Paragraphs.Add(text);
   text.Fragments.Add(new Fragment("Name", Font.Helvetica, 16));

   Drawing drawing = new Drawing();
   section.Paragraphs.Add(drawing);

   TextFieldShape textFieldShape = new TextFieldShape();
   textFieldShape.FullName = "Name";
   textFieldShape.Font = Font.Helvetica;
   textFieldShape.FontSize = 14;
   textFieldShape.Height = textFieldShape.Font.Height * textFieldShape.FontSize + 10;
   textFieldShape.X = 0;
   textFieldShape.Y = 0;
   drawing.Height = textFieldShape.Height;
   drawing.Width = 200;
   drawing.Shapes.Add(textFieldShape);

   pdf.Write(file);
}
 1 Document pdf = new Document();
 2 using (FileStream file = new FileStream(
 3    @"..\..\out.pdf", FileMode.Create, FileAccess.Write))
 4 {
 5    Section section = pdf.Sections.Add();
 6    section.PageSize = PageSize.A4;
 7 
 8    TextParagraph text = new TextParagraph();
 9    section.Paragraphs.Add(text);
10    text.Fragments.Add(new Fragment("Name", Font.Helvetica, 16));
11 
12    Drawing drawing = new Drawing();
13    section.Paragraphs.Add(drawing);
14 
15    TextFieldShape textFieldShape = new TextFieldShape();
16    textFieldShape.FullName = "Name";
17    textFieldShape.Font = Font.Helvetica;
18    textFieldShape.FontSize = 14;
19    textFieldShape.Height = textFieldShape.Font.Height * textFieldShape.FontSize + 10;
20    textFieldShape.X = 0;
21    textFieldShape.Y = 0;
22    drawing.Height = textFieldShape.Height;
23    drawing.Width = 200;
24    drawing.Shapes.Add(textFieldShape);
25 
26    pdf.Write(file);
27 }