Pdf Component - PdfLayerState

18 Nov 20264 minutes to read

Represents an operation applied to a layer by a layer-state action.

// Create a new PDF document
let document: PdfDocument = new PdfDocument();
let page: PdfPage = document.addPage();
// Create a layer and its state
let layer: PdfLayer = document.layers.add('Content Layer', true);
let state: PdfLayerState = new PdfLayerState(PdfLayerStateOperation.Off, layer);
// Create a layer-state action
let action: PdfSetOCGStateAction = new PdfSetOCGStateAction([state], 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 layer';
field.actions.mouseUp = action;
document.form.add(field);
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Properties

Get layer PdfLayer

Gets the layer affected by the operation.

// 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 and its first state
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
let state: PdfLayerState = action.state[0];
// Get the layer affected by the operation
let layer: PdfLayer = state.layer;
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Set layer void

Sets the layer affected by the operation.

// 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 and its first state
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
let state: PdfLayerState = action.state[0];
// Get another layer from the document
let layer: PdfLayer = document.layers.at(1);
// Set the layer affected by the operation
state.layer = layer;
action.state = action.state;
// Save and destroy the document
document.save('output.pdf');
document.destroy();
Parameter Type Description
value PdfLayer The layer to associate with the operation.

Get operation PdfLayerStateOperation

Gets the operation applied to the layer.

// 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 and its first state
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
let state: PdfLayerState = action.state[0];
// Get the operation applied to the layer
let operation: PdfLayerStateOperation = state.operation;
// Save and destroy the document
document.save('output.pdf');
document.destroy();

Set operation void

Sets the operation applied to the layer.

// 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 and its first state
let action: PdfSetOCGStateAction = field.actions.mouseUp as PdfSetOCGStateAction;
let state: PdfLayerState = action.state[0];
// Set the operation applied to the layer
state.operation = PdfLayerStateOperation.Toggle;
action.state = action.state;
// Save and destroy the document
document.save('output.pdf');
document.destroy();
Parameter Type Description
value PdfLayerStateOperation The operation to apply to the layer.