GdPicture.NET.14.API
GdPicture14 Namespace / GdPicturePDF Class / NewOCG Method
The title of the newly created optional content group.
Example





In This Topic
NewOCG Method (GdPicturePDF)
In This Topic
Creates a new optional content group (OCG) entry, means a layer, in the currently loaded PDF document.

Optional Content Groups, also knows as Layers, are a very effective tool to control the visibility of the page content, that can be selectively viewed or hidden, by users or viewers.

Syntax
'Declaration
 
Public Function NewOCG( _
   ByVal Title As String _
) As Integer
public int NewOCG( 
   string Title
)
public function NewOCG( 
    Title: String
): Integer; 
public function NewOCG( 
   Title : String
) : int;
public: int NewOCG( 
   string* Title
) 
public:
int NewOCG( 
   String^ Title
) 

Parameters

Title
The title of the newly created optional content group.

Return Value

The unique identifier of the newly created OCG entry. The GdPicturePDF.GetStat method can be subsequently used to determine if this method has been successful.
Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GdPicturePDF.GetStat method to identify the specific reason for the method's failure, if any.

Example
How to create a new layer (OCG entry) with the image-based content in the PDF document. The layer in this example is best viewed (in some viewers it is only viewed) when zooming between 0% and 100%.
Dim caption As String = "Example: NewOCG"
Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF()
If (gdpicturePDF.NewPDF() = GdPictureStatus.OK) AndAlso
   (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
    'Creating content of a new layer.
    Dim image_res_name As String = gdpicturePDF.AddJpegImageFromFile("image.jpg")
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        Dim width As Single = gdpicturePDF.GetPageWidth()
        Dim height As Single = gdpicturePDF.GetPageHeight()
        'Drawing the layer's content on the current page.
        If gdpicturePDF.DrawImage(image_res_name, 0, 0, width, height) = GdPictureStatus.OK Then
            'Creating the layer and setting its properties.
            Dim ocgID As Integer = gdpicturePDF.NewOCG("Image layer")
            If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
               (gdpicturePDF.SetImageOptional(image_res_name, 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) AndAlso
                   (gdpicturePDF.SetOCGZoomMin(ocgID, 0) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.SetOCGZoomMax(ocgID, 1) = GdPictureStatus.OK) Then
                    If gdpicturePDF.SaveToFile("test_NewOCG.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 SetImageOptional() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("The DrawImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
        End If
    Else
        MessageBox.Show("The AddJpegImageFromFile() 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: NewOCG";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if ((gdpicturePDF.NewPDF() == GdPictureStatus.OK) &&
    (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
{
    //Creating content of a new layer.
    string image_res_name = gdpicturePDF.AddJpegImageFromFile("image.jpg");
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        float width = gdpicturePDF.GetPageWidth();
        float height = gdpicturePDF.GetPageHeight();
        //Drawing the layer's content on the current page.
        if (gdpicturePDF.DrawImage(image_res_name, 0, 0, width, height) == GdPictureStatus.OK)
        {
            //Creating the layer and setting its properties.
            int ocgID = gdpicturePDF.NewOCG("Image layer");
            if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
                (gdpicturePDF.SetImageOptional(image_res_name, 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) &&
                    (gdpicturePDF.SetOCGZoomMin(ocgID, 0) == GdPictureStatus.OK) &&
                    (gdpicturePDF.SetOCGZoomMax(ocgID, 1) == GdPictureStatus.OK))
                {
                    if (gdpicturePDF.SaveToFile("test_NewOCG.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 SetImageOptional() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
        }
        else
        {
            MessageBox.Show("The DrawImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
    }
    else
    {
        MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
    }
}
else
{
    MessageBox.Show("The document can't be created.", caption);
}
gdpicturePDF.Dispose();
See Also