GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / SetFormFieldChecked Method / SetFormFieldChecked(Int32,Boolean) Method
A unique form field identifier specifying a required form field object. You can obtain this identifier using these methods: AddCheckBoxFormField(Single,Single,Single,Single,String,PdfCheckBoxStyle,Boolean,Byte,Byte,Byte), GetFormFieldId or GetFormFieldChildID.
Set this parameter to true, if you want to check the specified checkbox (select it as On), otherwise set it to false to uncheck it (select it as Off).
Example





In This Topic
SetFormFieldChecked(Int32,Boolean) Method
In This Topic
Checks or unchecks the required form field, here a check box, in other words toggles the field's checked state to On or Off. The check box is specified by its unique form field's identifier and it is related to the currently loaded PDF document. As said, this method is only applicable to check boxes.
Syntax
'Declaration
 
Public Overloads Function SetFormFieldChecked( _
   ByVal FieldId As Integer, _
   ByVal Checked As Boolean _
) As GdPictureStatus
public GdPictureStatus SetFormFieldChecked( 
   int FieldId,
   bool Checked
)
public function SetFormFieldChecked( 
    FieldId: Integer;
    Checked: Boolean
): GdPictureStatus; 
public function SetFormFieldChecked( 
   FieldId : int,
   Checked : boolean
) : GdPictureStatus;
public: GdPictureStatus SetFormFieldChecked( 
   int FieldId,
   bool Checked
) 
public:
GdPictureStatus SetFormFieldChecked( 
   int FieldId,
   bool Checked
) 

Parameters

FieldId
A unique form field identifier specifying a required form field object. You can obtain this identifier using these methods: AddCheckBoxFormField(Single,Single,Single,Single,String,PdfCheckBoxStyle,Boolean,Byte,Byte,Byte), GetFormFieldId or GetFormFieldChildID.
Checked
Set this parameter to true, if you want to check the specified checkbox (select it as On), otherwise set it to false to uncheck it (select it as Off).

Return Value

A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.

We strongly recommend always checking this status first.

Remarks
This method is only allowed for use with non-encrypted documents.

Just to remind you that this method is only meaningful for check boxes, otherwise it will fail.

Example
How to change the checked state to its opposite value for all check boxes in the current document.
Dim caption As String = "Example: SetFormFieldChecked"
Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF()
If gdpicturePDF.LoadFromFile("forms.pdf", False) = GdPictureStatus.OK Then
    Dim count As Integer = gdpicturePDF.GetFormFieldsCount()
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        Dim formID As Integer = 0
        Dim selected As Boolean = False
        Dim type As PdfFormFieldType = PdfFormFieldType.PdfFormFieldTypeUnknown
        For i As Integer = 0 To count - 1
            formID = gdpicturePDF.GetFormFieldId(i)
            If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                type = gdpicturePDF.GetFormFieldType(formID)
                If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                    If type = PdfFormFieldType.PdfFormFieldTypeCheckBoxButton Then
                        selected = gdpicturePDF.GetFormFieldChecked(formID)
                        If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                            If gdpicturePDF.SetFormFieldChecked(formID, Not selected) <> GdPictureStatus.OK Then
                                MessageBox.Show("The SetFormFieldCheckBoxStyle() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                                Exit For
                            End If
                        Else
                            MessageBox.Show("The GetFormFieldCheckBoxStyle() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                            Exit For
                        End If
                    End If
                Else
                    MessageBox.Show("The GetFormFieldType() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                    Exit For
                End If
            Else
                MessageBox.Show("The GetFormFieldId() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                Exit For
            End If
        Next
        If count = 0 Then
            MessageBox.Show("This file doesn't include forms.", caption)
        Else
            If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                If gdpicturePDF.SaveToFile("forms_updated.pdf") = GdPictureStatus.OK Then
                    MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
                Else
                    MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            End If
        End If
    Else
        MessageBox.Show("The GetFormFieldsCount() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
    End If
Else
    MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: SetFormFieldChecked";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if (gdpicturePDF.LoadFromFile("forms.pdf", false) == GdPictureStatus.OK)
{
    int count = gdpicturePDF.GetFormFieldsCount();
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        int formID = 0;
        bool selected = false;
        PdfFormFieldType type = PdfFormFieldType.PdfFormFieldTypeUnknown;
        for (int i = 0; i < count; i++)
        {
            formID = gdpicturePDF.GetFormFieldId(i);
            if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
            {
                type = gdpicturePDF.GetFormFieldType(formID);
                if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
                {
                    if (type == PdfFormFieldType.PdfFormFieldTypeCheckBoxButton)
                    {
                        selected = gdpicturePDF.GetFormFieldChecked(formID);
                        if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
                        {
                            if (gdpicturePDF.SetFormFieldChecked(formID, !selected) != GdPictureStatus.OK)
                            {
                                MessageBox.Show("The SetFormFieldCheckBoxStyle() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
                                break;
                            }
                        }
                        else
                        {
                            MessageBox.Show("The GetFormFieldCheckBoxStyle() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("The GetFormFieldType() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
                    break;
                }
            }
            else
            {
                MessageBox.Show("The GetFormFieldId() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
                break;
            }
        }
        if (count == 0)
            MessageBox.Show("This file doesn't include forms.", caption);
        else
        {
            if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
            {
                if (gdpicturePDF.SaveToFile("forms_updated.pdf") == GdPictureStatus.OK)
                    MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
                else
                    MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
        }
    }
    else
        MessageBox.Show("The GetFormFieldsCount() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
    MessageBox.Show("The file can't be loaded.", caption);
gdpicturePDF.Dispose();
See Also