Some test text!

menu
Show/hide layerskeyboard_arrow_down

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 { docViewer } = instance;

    docViewer.on('documentLoaded', () => {
      const doc = docViewer.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
        docViewer.refreshAll();
        // redraws
        docViewer.updateView();
      });
    })
  });

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close