Skip to content

Modify PDF Layers Properties

Use the getLayers function to retrieve the OCG layers associated with the document. You can then selectively hide layers.

In the example below, the getLayers function returns an array of layers. To hide a layer, you can call the setLayers function, passing an object that includes the ocg property, node property, and visible property. If you need to modify layers in bulk, you can pass an array containing multiple objects.

After the call is completed, if the passed parameters contain the visible or defaultVisible propertie, the page will be redrawn.

Show/hide layers based on this method.

javascript
// Get layers.
const layers = await docViewer.getLayers();

// Change the name of the first layer and lock the second layer.
docViewer.setLayers([{
  node: layers[0].node,
  ocg: layers[0].ocg,
  name: 'modified name'
}, {
  node: layers[1].node,
  ocg: layers[1].ocg,
  isLocked: 1
}]);

// Modify the export and print properties of the third layer.
docViewer.setLayers({
  node: layers[0].node,
  ocg: layers[0].ocg,
  exportState: 0, // 0 Always, 1 Never, 2 Export when visible.
  printState: 1 // 0 Always, 1 Never, 2 Print when visible.
});