Xceed PDF Library for .NET Documentation v1.0
In This Topic
    Add form fields
    In This Topic

    Introduction

    The following example demonstrates how to add a form fields in a PDF document.

    The following form fields are covered:


    Add form fields to a PDF document (C#)
    Copy Code
    public static void AddFormFields()
    {
      Console.WriteLine( "=== ADD FORM FIELDS ===" );
      var outputFileName = "AddFormFields.pdf";
      var outputPath = FormFieldsSample.FormFieldsSampleOutputDirectory + outputFileName;
    
      using( var pdfoutput = PdfDocument.Create( outputPath ) )
      {
        // Get first page of document.
        var page = pdfoutput.Pages[ 0 ];
    
        // Add Title.
        var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Add FormFields", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Define a font for text to add.
        var formFieldFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.CourierBoldOblique );
    
        // Add TextBoxFormField.
        page.AddText( "TextBox:", new Point( 50, 100 ) );
        var textBoxField = new TextBoxFormField( "John Doe", new Rectangle( 125, 92, 120, 22 ), TextStyle.WithFontAndColor( formFieldFont, 13d, Color.Blue ) );
        textBoxField.Border.Color = Color.Black;
        textBoxField.Border.Width = 2;
        page.FormFields.Add( textBoxField );
    
        // Add NumberFormField.
        page.AddText( "Number:", new Point( 50, 130 ) );
        var numberField = new NumberFormField( 32.66d, new Rectangle( 125, 122, 120, 22 ), TextStyle.WithFontAndColor( formFieldFont, 13d, Color.DarkBlue ) )
        {
          CurrencySymbol = "$",
          SymbolLocation = SymbolLocation.BeforeNoSpace,
          DecimalPlaces = 2,
        };
        numberField.Border.Color = Color.Green;
        numberField.Border.Width = 1;
        page.FormFields.Add( numberField );
    
        // Add DateTimeFormField.
        page.AddText( "DateTime:", new Point( 50, 160 ) );
        var dateTimeField = new DateTimeFormField( new DateTime( 2025, 7, 20 ), new Rectangle( 125, 152, 120, 22 ) )
        {
          Format = "yyyy/MM/dd",
          FillColor = Color.LightGray
        };
        page.FormFields.Add( dateTimeField );
    
        // Add CheckBoxFormField.
        page.AddText( "CheckBox:", new Point( 50, 190 ) );
        var checkBoxField = new CheckBoxFormField( true, new Rectangle( 125, 182, 20, 20 ) )
        {
          CheckedStyle = CheckBoxFormFieldStyle.Check,
          FillColor = Color.LightCyan,
          TextStyle = TextStyle.WithFontAndColor( formFieldFont, 12d, Color.Red )
        };
        page.FormFields.Add( checkBoxField );
    
    
        // Add ComboBoxFormField.
        page.AddText( "CombBox:", new Point( 50, 220 ) );
        var comboBoxItems = new List<ListItem>()
        {
          new ListItem( "First" ),
          new ListItem( "Second" ),
          new ListItem( "Third" ),
          new ListItem( "Fourth" )
        };
        var comboBoxField = new ComboBoxFormField( comboBoxItems, new Rectangle( 125, 212, 120, 22 ) )
        {
          FillColor = Color.Black,
          TextStyle = TextStyle.WithFontAndColor( formFieldFont, 12d, Color.Yellow ),
          SelectedIndex = 1,
          IsEditable = true
        };
        page.FormFields.Add( comboBoxField );
    
        // Add ListBoxFormField.
        page.AddText( "ListBox:", new Point( 50, 250 ) );
        var listItems = new List<ListItem>()
        {
          new ListItem( "Orange" ),
          new ListItem( "Banana" ),
          new ListItem( "Strawberry" ),
          new ListItem( "Blueberry" ),
          new ListItem( "Grape" ),
          new ListItem( "Pineapple" )
        };
        var listBoxFormFieldFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica );
        var listBoxField = new ListBoxFormField( listItems, new Rectangle( 125, 242, 120, 85 ) )
        {
          FillColor = Color.DarkGray,
          TextStyle = TextStyle.WithFontAndColor( listBoxFormFieldFont, 12d, Color.White ),
          IsMultiSelect = true,
          SelectedValues = new List<string>() { "Banana", "Blueberry" }
        };
        page.FormFields.Add( listBoxField );
    
        // Add RadioButtonsFormField.
        page.AddText( "Radio:", new Point( 50, 350 ) );
        var radioButtons = new List<RadioButtonFormField>()
        {
          new RadioButtonFormField( "Friday", new Rectangle( 125, 342, 20, 20 ) ),
          new RadioButtonFormField( "Saturday", new Rectangle( 125, 367, 20, 20 ) ) { IsChecked = true },
          new RadioButtonFormField( "Sunday", new Rectangle( 125, 392, 20, 20 ) )
        };
        var radioGroupField = new RadioButtonGroupFormField( radioButtons )
        {
          TextStyle = TextStyle.WithFontAndColor( formFieldFont, 13d, Color.LightGreen ),
          FillColor = Color.DeepPink
        };
        page.FormFields.Add( radioGroupField );
        page.AddText( "Friday", new Point( 150, 347 ) );
        page.AddText( "Saturday", new Point( 150, 372 ) );
        page.AddText( "Sunday", new Point( 150, 397 ) );
    
        // Add SignatureFormField.
        page.AddText( "Signature:", new Point( 50, 430 ) );
        var signatureField = new SignatureFormField( new Rectangle( 125, 422, 120, 22 ) )
        {
          Border = new Border() { Color = Color.Blue, Width = 2 }
        };
        page.FormFields.Add( signatureField );
    
        // Save the document.
        pdfoutput.Save();
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }

    See Also