VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf.UI.Annotations Namespace / PdfAnnotationTool Class / AddAndBuildFormField Methods / AddAndBuildFormField(PdfInteractiveFormField) Method
Syntax Example Requirements SeeAlso
In This Topic
AddAndBuildFormField(PdfInteractiveFormField) Method (PdfAnnotationTool)
In This Topic
Adds and builds the form field.
Syntax

Parameters

field
The field.

Return Value

The annotation view that corresponds to the field.
Example

This C#/VB.NET code shows how to start the building of button form field.


''' <summary>
''' Adds and builds a button form field.
''' </summary>
''' <param name="annotationTool">The PDF annotation tool, which will build a field.</param>
Public Shared Sub AddAndBuildButtonField(annotationTool As Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool)
    Dim focusedPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = annotationTool.FocusedPage
    If focusedPage Is Nothing Then
        Throw New System.InvalidOperationException()
    End If

    ' create a Button form field
    Dim field As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField(focusedPage.Document, GetFieldName(focusedPage.Document, Nothing, "Button{0}"), New System.Drawing.RectangleF(0, 0, 150, 22))

    ' set the field appearance generator
    Dim appearanceGenerator As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.AppearanceGenerators.PdfButton3DBorderFieldAppearanceGenerator()
    field.Annotation.AppearanceGenerator = appearanceGenerator
    appearanceGenerator.NormalCaption = "JavaScript 'Hello!'"
    appearanceGenerator.FontSize = 14

    ' if tool mode does not allow to build annotation
    If annotationTool.InteractionMode <> Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Edit Then
        ' change the tool mode
        annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Edit
    End If

    ' set JavaScript action to Activate trigger event
    field.Annotation.ActivateAction = New Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(focusedPage.Document, "app.alert('Hello!', 3);")

    ' add and build annotation
    annotationTool.AddAndBuildFormField(field)
End Sub

''' <summary>
''' Returns the name of new field.
''' </summary>
''' <param name="document">PDF document.</param>
''' <param name="parentField">The parent interactive form field.</param>
''' <param name="fieldNameFormat">The format of field name.</param>
''' <returns>The name of new field.</returns>
Private Shared Function GetFieldName(document As Vintasoft.Imaging.Pdf.PdfDocument, parentField As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormField, fieldNameFormat As String) As String
    Dim format As String = fieldNameFormat
    If parentField IsNot Nothing Then
        format = String.Format("{0}.{1}", parentField.FullyQualifiedName, fieldNameFormat)
    End If
    If document.InteractiveForm Is Nothing Then
        Return String.Format(fieldNameFormat, 1)
    End If
    Dim i As Integer = 1
    While document.InteractiveForm.FindField(String.Format(format, i)) IsNot Nothing
        i += 1
    End While
    Return String.Format(fieldNameFormat, i)
End Function


/// <summary>
/// Adds and builds a button form field.
/// </summary>
/// <param name="annotationTool">The PDF annotation tool, which will build a field.</param>
public static void AddAndBuildButtonField(Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool annotationTool)
{
    Vintasoft.Imaging.Pdf.Tree.PdfPage focusedPage = annotationTool.FocusedPage;
    if (focusedPage == null)
        throw new System.InvalidOperationException();

    // create a Button form field
    Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField field = 
        new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormPushButtonField(
            focusedPage.Document,
            GetFieldName(focusedPage.Document, null, "Button{0}"),
            new System.Drawing.RectangleF(0, 0, 150, 22));

    // set the field appearance generator
    Vintasoft.Imaging.Pdf.Tree.InteractiveForms.AppearanceGenerators.PdfButton3DBorderFieldAppearanceGenerator appearanceGenerator =
        new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.AppearanceGenerators.PdfButton3DBorderFieldAppearanceGenerator();
    field.Annotation.AppearanceGenerator = appearanceGenerator;
    appearanceGenerator.NormalCaption = "JavaScript 'Hello!'";
    appearanceGenerator.FontSize = 14;

    // if tool mode does not allow to build annotation
    if (annotationTool.InteractionMode != Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Edit)
        // change the tool mode
        annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Edit;

    // set JavaScript action to Activate trigger event
    field.Annotation.ActivateAction =
        new Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(focusedPage.Document, "app.alert('Hello!', 3);");

    // add and build annotation
    annotationTool.AddAndBuildFormField(field);
}

/// <summary>
/// Returns the name of new field.
/// </summary>
/// <param name="document">PDF document.</param>
/// <param name="parentField">The parent interactive form field.</param>
/// <param name="fieldNameFormat">The format of field name.</param>
/// <returns>The name of new field.</returns>
private static string GetFieldName(
    Vintasoft.Imaging.Pdf.PdfDocument document,
    Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormField parentField,
    string fieldNameFormat)
{
    string format = fieldNameFormat;
    if (parentField != null)
        format = string.Format("{0}.{1}", parentField.FullyQualifiedName, fieldNameFormat);
    if (document.InteractiveForm == null)
        return string.Format(fieldNameFormat, 1);
    int i = 1;
    while (document.InteractiveForm.FindField(string.Format(format, i)) != null)
        i++;
    return string.Format(fieldNameFormat, i);
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also