Pdf - PdfSetOCGStateAction

18 Nov 20265 minutes to read

Represents an action that changes the visibility state of optional-content layers.

// Create a new PDF document
let document: PdfDocument = new PdfDocument();
let page: PdfPage = document.addPage();
// Create layers and their states
let firstLayer: PdfLayer = document.layers.add('First Layer', true);
let secondLayer: PdfLayer = document.layers.add('Second Layer', false);
let states: PdfLayerState[] = [
    new PdfLayerState(PdfLayerStateOperation.Off, firstLayer),
    new PdfLayerState(PdfLayerStateOperation.On, secondLayer)
];
// Create a layer-state action
let action: PdfSetOCGStateAction = new PdfSetOCGStateAction(states, true);
// Create a button and assign the action
let field: PdfButtonField = new PdfButtonField(
    page, 'LayerState', { x: 20, y: 20, width: 120, height: 30 }
);
field.text = 'Change layers';
field.actions.mouseUp = action;
document.form.add(field);
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Properties

Get next PdfAction

Get the next action to be performed after the action represented by this instance.

Set next void

Set the next action to be performed after the action represented by this instance.

Parameter Type Description
value PdfAction The next action to be executed.

Get preserveRadioButtons boolean

Gets whether the radio-button relationships between layers are preserved.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Get the layer-state button
let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
// Get the layer-state action
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
// Get the radio-button preservation setting
let preserveRadioButtons: boolean = action.preserveRadioButtons;
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Set preserveRadioButtons void

Sets whether the radio-button relationships between layers are preserved.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Get the layer-state button
let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
// Get the layer-state action
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
// Preserve the radio-button relationships between layers
action.preserveRadioButtons = true;
// Save and destroy the document
document.save('output.pdf');
document.destroy();
Parameter Type Description
value boolean true to preserve radio-button relationships; otherwise, false.

Get state PdfLayerState[]

Gets the layer states associated with the action.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Get the layer-state button
let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
// Get the layer-state action
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
// Get the layer states
let states: PdfLayerState[] = action.state;
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Set state void

Sets the layer states associated with the action.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Get the layer-state button
let field: PdfButtonField = document.form.fieldAt(0) as PdfButtonField;
// Get the layer-state action
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
// Get the document layers
let firstLayer: PdfLayer = document.layers.at(0);
let secondLayer: PdfLayer = document.layers.at(1);
// Set the layer states
action.state = [
    new PdfLayerState(PdfLayerStateOperation.Off, firstLayer),
    new PdfLayerState(PdfLayerStateOperation.Toggle, secondLayer)
];
// Save and destroy the document
document.save('output.pdf');
document.destroy();
Parameter Type Description
value PdfLayerState[] The layer states to associate with the action.