VintaSoft Imaging .NET SDK 11.0
In This Topic
    PDF: Change field value in PDF interactive form
    In This Topic

    To change a value of a form field is necessary to do the following:


    To change the default value of a form field is necessary to do the following:

    Here is an example that demonstrates how to change the value of a text field:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Pdf
    
    ''' <summary>
    ''' Changes the text field value.
    ''' </summary>
    ''' <param name="document">The PDF document.</param>
    ''' <param name="fieldFullName">Full name of the field.</param>
    ''' <param name="newValue">The new value of the field.</param>
    ''' <returns>
    ''' <b>true</b> - field value is changed successfully;
    ''' <b>false</b> - field value is NOT changed.
    ''' </returns>
    Public Shared Function ChangeTextFieldValue(document As Vintasoft.Imaging.Pdf.PdfDocument, fieldFullName As String, newValue As String) As Boolean
        ' if PDF document has PDF interactive form
        If document.InteractiveForm IsNot Nothing Then
            ' find field by name
            Dim field As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormField = document.InteractiveForm.FindField(fieldFullName)
            ' if field is found
            If field IsNot Nothing Then
                Dim textField As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField = TryCast(field, Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField)
                ' if field is text field
                If textField IsNot Nothing Then
                    ' set new value of the field
                    textField.Value = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextFieldStringValue(document, newValue)
    
                    Return True
                End If
            End If
        End If
        Return False
    End Function
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Pdf
    
    /// <summary>
    /// Changes the text field value.
    /// </summary>
    /// <param name="document">The PDF document.</param>
    /// <param name="fieldFullName">Full name of the field.</param>
    /// <param name="newValue">The new value of the field.</param>
    /// <returns>
    /// <b>true</b> - field value is changed successfully;
    /// <b>false</b> - field value is NOT changed.
    /// </returns>
    public static bool ChangeTextFieldValue(
        Vintasoft.Imaging.Pdf.PdfDocument document,
        string fieldFullName,
        string newValue)
    {
        // if PDF document has PDF interactive form
        if (document.InteractiveForm != null)
        {
            // find field by name
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormField field =
                document.InteractiveForm.FindField(fieldFullName);
            // if field is found
            if (field != null)
            {
                Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField textField =
                    field as Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField;
                // if field is text field
                if (textField != null)
                {
                    // set new value of the field
                    textField.Value =
                        new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextFieldStringValue(document, newValue);
    
                    return true;
                }
            }
        }
        return false;
    }