Showing & hiding PDF layers using JavaScript

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

In the example below, the getLayersArray function returns an array with layers. To hide a layer, the visible property can be set to false. You must then call setLayersArray with the modified layers array. Finally, call refreshAll and updateView to clear the page cache and redraw.

1WebViewer({
2 ...
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer } = instance.Core;
6
7 documentViewer.addEventListener('documentLoaded', () => {
8 const doc = documentViewer.getDocument();
9
10 doc.getLayersArray().then(layers => {
11 // Set all layers to not visible
12 layers.forEach((layer, index) => {
13 layers[index].visible = false;
14 });
15 doc.setLayersArray(layers);
16 // clears page cache
17 documentViewer.refreshAll();
18 // redraws
19 documentViewer.updateView();
20 });
21 })
22 });

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales