The form field identifier you want to add to be the OCG's (layer's) content. You can obtain this identifier using methods like GetFormFieldId, GetFormFieldChildID or methods intended to add form fields.
The unique identifier of the required OCG entry. You can obtain this identifier using the GetOCG method.
Example





In This Topic
GdPicture14 Namespace / GdPicturePDF Class / SetFormFieldOptional Method

SetFormFieldOptional Method (GdPicturePDF)

In This Topic
Adds a required form field to be the content of an optional content group, specified by its unique identifier.
Syntax
'Declaration
 
Public Function SetFormFieldOptional( _
   ByVal FieldId As Integer, _
   ByVal OCGId As Integer _
) As GdPictureStatus
public GdPictureStatus SetFormFieldOptional( 
   int FieldId,
   int OCGId
)
public function SetFormFieldOptional( 
    FieldId: Integer;
    OCGId: Integer
): GdPictureStatus; 
public function SetFormFieldOptional( 
   FieldId : int,
   OCGId : int
) : GdPictureStatus;
public: GdPictureStatus SetFormFieldOptional( 
   int FieldId,
   int OCGId
) 
public:
GdPictureStatus SetFormFieldOptional( 
   int FieldId,
   int OCGId
) 

Parameters

FieldId
The form field identifier you want to add to be the OCG's (layer's) content. You can obtain this identifier using methods like GetFormFieldId, GetFormFieldChildID or methods intended to add form fields.
OCGId
The unique identifier of the required OCG entry. You can obtain this identifier using the GetOCG method.

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.
Example
How to add a form field to be the content of the specified layer in the PDF document.
Dim caption As String = "Example: SetFormFieldOptional"
Dim gdpicturePDF As New GdPicturePDF()
If (gdpicturePDF.NewPDF() = GdPictureStatus.OK) AndAlso
   (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
    Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesBold)
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        Dim fieldID As Integer = gdpicturePDF.AddTextFormField(50, 700, 200, 30, "Name", "GdPicture", False, fontName, 12, 165, 42, 42)
        If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFormFieldBorderColor(fieldID, 0, 0, 255) = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.SetFormFieldBackgroundColor(fieldID, 255, 228, 196) = GdPictureStatus.OK) Then
            Dim ocgID As Integer = gdpicturePDF.NewOCG("Form field layer")
            If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
               (gdpicturePDF.SetFormFieldOptional(fieldID, ocgID) = GdPictureStatus.OK) Then
                If (gdpicturePDF.SetOCGExportState(ocgID, PdfOcgState.StateOff) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.SetOCGLockedState(ocgID, True) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.SetOCGPrintState(ocgID, PdfOcgState.StateOn) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn) = GdPictureStatus.OK) Then
                    If gdpicturePDF.SaveToFile("test_FormFieldLayer.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
                Else
                    MessageBox.Show("Setting up layer's options has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("The NewOCG() or SetFormFieldOptional() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("Adding the form field has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
        End If
    Else
        MessageBox.Show("The AddStandardFont() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
    End If
Else
    MessageBox.Show("The document can't be created.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: SetFormFieldOptional";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if ((gdpicturePDF.NewPDF() == GdPictureStatus.OK) &&
    (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
{
    string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesBold);
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        int fieldID = gdpicturePDF.AddTextFormField(50, 700, 200, 30, "Name", "GdPicture", false, fontName, 12, 165, 42, 42);
        if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFormFieldBorderColor(fieldID, 0, 0, 255) == GdPictureStatus.OK) &&
            (gdpicturePDF.SetFormFieldBackgroundColor(fieldID, 255, 228, 196) == GdPictureStatus.OK))
        {
            int ocgID = gdpicturePDF.NewOCG("Form field layer");
            if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
                (gdpicturePDF.SetFormFieldOptional(fieldID, ocgID) == GdPictureStatus.OK))
            {
                if ((gdpicturePDF.SetOCGExportState(ocgID, PdfOcgState.StateOff) == GdPictureStatus.OK) &&
                    (gdpicturePDF.SetOCGLockedState(ocgID, true) == GdPictureStatus.OK) &&
                    (gdpicturePDF.SetOCGPrintState(ocgID, PdfOcgState.StateOn) == GdPictureStatus.OK) &&
                    (gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn) == GdPictureStatus.OK))
                {
                    if (gdpicturePDF.SaveToFile("test_FormFieldLayer.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("Setting up layer's options has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
                }
            }
            else
            {
                MessageBox.Show("The NewOCG() or SetFormFieldOptional() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
        }
        else
        {
            MessageBox.Show("Adding the form field has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
    }
    else
    {
        MessageBox.Show("The AddStandardFont() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
    }
}
else
{
    MessageBox.Show("The document can't be created.", caption);
}
gdpicturePDF.Dispose();
See Also