Drawing annotations with boundaries

16 views
Skip to first unread message

Justin Jung

unread,
Apr 19, 2017, 12:06:27 PM4/19/17
to PDFTron WebViewer
Q: How do I force a rectangle annotation to only be drawn inside of another rectangle annotation?

A: Here is a sample code that you can use in config.js, which creates a sample rectangle annotation and forces other rectangles to be only drawn inside of it.

var boundingAnnot;

$(document).on('documentLoaded', function() {
  boundingAnnot = new Annotations.RectangleAnnotation();
  boundingAnnot.PageNumber = 1;
  boundingAnnot.X = 200;
  boundingAnnot.Y = 200;
  boundingAnnot.Width = 200;
  boundingAnnot.Height = 200;
  readerControl.docViewer.getAnnotationManager().addAnnotation(boundingAnnot);
  readerControl.docViewer.getAnnotationManager().redrawAnnotation(boundingAnnot);
});

var mouseLeftDown = Tools.RectangleCreateTool.prototype.mouseLeftDown;
Tools.RectangleCreateTool.prototype.mouseLeftDown = function(e) {
  mouseLeftDown.apply(this, arguments);
  if (!isPointInBound(e.data.pageCoordinate, boundingAnnot)) {
    this.annotation = null;
  }
}

var mouseMove = Tools.RectangleCreateTool.prototype.mouseMove;
Tools.RectangleCreateTool.prototype.mouseMove = function(e) {
  mouseMove.apply(this, arguments);
  if (this.annotation) {
    if (!isPointInBound(e.data.pageCoordinate, boundingAnnot)) {
      var annotRect = this.annotation.getRect();
      var boundingRect = boundingAnnot.getRect();

      if (annotRect.x1 < boundingRect.x1) {
        annotRect.x1 = boundingRect.x1;
      }
      if (annotRect.y1 < boundingRect.y1) {
        annotRect.y1 = boundingRect.y1;
      }
      if (annotRect.x2 > boundingRect.x2) {
        annotRect.x2 = boundingRect.x2;
      }
      if (annotRect.y2 > boundingRect.y2) {
        annotRect.y2 = boundingRect.y2;
      }

      this.annotation.setRect(annotRect);
      readerControl.docViewer.getAnnotationManager().redrawAnnotation(this.annotation);
    }
  }
}

var isPointInBound = function(pageCoordinate, boundingAnnot) {
  if (!pageCoordinate) {
    return;
  }
  var mouseX = pageCoordinate.x;
  var mouseY = pageCoordinate.y;
  return (mouseX >= boundingAnnot.X && mouseX <= boundingAnnot.X + boundingAnnot.Width &&
    mouseY >= boundingAnnot.Y && mouseY <= boundingAnnot.Y + boundingAnnot.Height);
}
Reply all
Reply to author
Forward
0 new messages