For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

WebViewer({
  ...
}, document.getElementById('viewer'))
  .then(instance => {
    const { documentViewer } = instance.Core;

    documentViewer.addEventListener('documentLoaded', () => {
      const doc = documentViewer.getDocument();

      doc.getLayersArray().then(layers => {
        // Set all layers to not visible
        layers.forEach((layer, index) => {
          layers[index].visible = false;
        });
        doc.setLayersArray(layers);
        // clears page cache
        documentViewer.refreshAll();
        // redraws
        documentViewer.updateView();
      });
    })
  });

Last updated

Was this helpful?