Some test text!

Search
Hamburger Icon

Web / FAQ / Where to find code snippets for WebViewer 5.0 or earlier?

Where can I find code snippets for WebViewer 5.0 or earlier?

With the release of WebViewer 6.0, code snippets of all the guides have been updated to include only v6.0+ and v5.1+. If you are still using WebViewer 5.0.x, you can use v5.1+ snippets with a few tweaks.

The biggest difference between WebViewer 5.1 and 5.0 is how it is instantiated. The promisified instantiation isn't available in 5.0 so you need to listen to the ready event instead. Also, objects and namespaces such as Annotations, Tools, CoreControls aren't attached to the instance object in 5.0 so you need to grab them from the iframe window.

// v5.1+
WebViewer({
  ...
}, document.getElementById('viewer'))
  .then(instance => {
    const {
      docViewer,
      annotManager,
      Tools,
      Annotations,
      CoreControls,
      PartRetrievers,
      Actions,
      PDFNet,
    } = instance;

    // call instance APIs here
  });
// v5.0
const viewerElement = document.getElementById('viewer');
const viewer = new PDFTron.WebViewer({
  ...
}, viewerElement);

viewer.addEventListener('ready', () => {
  const instance = viewer.getInstance();
  
  const docViewer = instance.docViewer;
  const annotManager = docViewer.getAnnotationManager();
  const {
    Tools,
    Annotations,
    CoreControls,
    PartRetrievers,
    Actions,
    PDFNet,
  } = document.querySelector('iframe').contentWindow;
  
  // call instance APIs here
});

Get the answers you need: Chat with us