Some test text!

< Web samples

Define User Permissions in WebViewer with JavaScript

This JavaScript sample demonstrates how you can apply permissions to users viewing your PDF, DOCX, XLSX, or PPTX document (no other external dependencies required). In this specific example you define how a user interacts with annotations in the PDF viewer. One common use case is removing a user’s ability to add annotation by providing them read-only access. Another example would be defining the types of annotations a user sees on a document – they can see highlighted text from another user but not the sticky notes they added. For other users you can give them the ability to respond to comments or give administrative rights to delete annotations. This sample works on all browsers (including IE10) and mobile devices without using plug-ins. To see an example visit our WebViewer User Permission demo.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

var viewerElement = document.getElementById('viewer');
var viewer = new PDFTron.WebViewer({
  path: '../../../lib',
  l: window.sampleL,
  pdftronServer: 'https://demo.pdftron.com/', // comment this out to do client-side only
  initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-annotated.pdf'
}, viewerElement);

viewerElement.addEventListener('documentLoaded', function() {
  var viewerInstance = viewer.getInstance();

  viewerInstance.setAnnotationUser('Justin');
  viewerInstance.setAdminUser(true);
  viewerInstance.openElement('notesPanel');

  document.getElementById('justin').onchange = function() {
    viewerInstance.setAnnotationUser('Justin');
    viewerInstance.setAdminUser(true);
    viewerInstance.setReadOnly(false);
    viewerInstance.setToolMode('AnnotationEdit');
  };

  document.getElementById('sally').onchange = function() {
    viewerInstance.setAnnotationUser('Sally');
    viewerInstance.setAdminUser(false);
    viewerInstance.setReadOnly(false);
    viewerInstance.setToolMode('AnnotationEdit');
  };

  document.getElementById('brian').onchange = function() {
    viewerInstance.setAnnotationUser('Brian');
    viewerInstance.setAdminUser(false);
    viewerInstance.setReadOnly(true);
    viewerInstance.setToolMode('AnnotationEdit');
  };

  document.getElementById('display').onchange = function(e) {
    var currentUser = viewerInstance.getAnnotationUser();
    var annotationManager = viewerInstance.docViewer.getAnnotationManager();
    var allAnnotations = annotationManager.getAnnotationsList().filter(function(annotation) {
      return annotation.Listable;
    });

    if (e.target.checked) {
      annotationManager.showAnnotations(allAnnotations);
    } else {
      var annotationsToHide = allAnnotations.filter(function(annotation) {
        return annotation.Author !== currentUser;
      });
      annotationManager.hideAnnotations(annotationsToHide);
    }
  };
});