In This Topic
    Get information about the form fields
    In This Topic

    Introduction

    The following example demonstrates how to analyze form fields in order to get information about them, like what type of field they are, where they are located in the PDF document, their dimensions & if they are read-only or not.

    Analyze form fields found in a PDF document (C#)
    Copy Code
    public static void GetFormFields()
    {
      Console.WriteLine( "=== GET FORM FIELDS ===" );
      var fileName = "employment_form.pdf";
      PageFormFieldCollection formFields;
    
      // Loads a PdfDocument.
      using( var pdfDoc = PdfDocument.Load( FormFieldsSample.FormFieldsSampleResourcesDirectory + $"{fileName}" ) )
      {
        // Gets the first Page of the loaded document.
        var page = pdfDoc.Pages[ 0 ];
    
        // Gets the FormFields on the Page.
        formFields = page.FormFields;
      }
    
      var outputFileName = "GetFormFields.pdf";
      var outputPath = FormFieldsSample.FormFieldsSampleOutputDirectory + outputFileName;
    
      // Creates a PdfDocument.
      using( var pdfoutput = PdfDocument.Create( outputPath ) )
      {
        // Gets the first Page.
        var page = pdfoutput.Pages.First();
    
        // Adds a title.
        var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Get FormFields", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        if( formFields.Count > 0 )
        {
          // Adds Courier red text.
          var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d, Brushes.Red );
          page.AddText( $"Form fields extracted from: {fileName}", new Point( 25, 70 ), redTextStyle );
    
          // Adds Courier text.
          var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d );
          page.AddText( $"Total form fields on page: {formFields.Count}", new Point( 25, 90 ), textStyle );
    
          // Shows the information related to the first 5 fields.
          for( int i = 0; i < Math.Min( 5, formFields.Count ); i++ )
          {
            var field = formFields[ i ];
    
            // Adds text related to a specific FormField.
            page.AddText( $"Field {i + 1}: '{field.Name}'", new Point( 25, 120 + ( i * 75 ) ), textStyle );
            page.AddText( $"  Type: {field.GetType().Name}", new Point( 25, 135 + ( i * 75 ) ), textStyle );
            page.AddText( $"  Location: ({field.Bounds.Left:F0}, {field.Bounds.Top:F0})", new Point( 25, 150 + ( i * 75 ) ), textStyle );
            page.AddText( $"  Dimensions: {field.Bounds.Width:F0} x {field.Bounds.Height:F0}", new Point( 25, 165 + ( i * 75 ) ), textStyle );
            page.AddText( $"  Read only: {field.IsReadOnly}", new Point( 25, 180  + ( i * 75 )), textStyle );
          }
    
          // Saves the output document.
          pdfoutput.Save();
          Console.WriteLine( $"Info exported to path: {outputFileName}" );
        }
        else
        {
          Console.WriteLine( "No form fields found in this document." );
        }
      }
    }

    See Also

    Add elements in a PDF document