Some test text!

Understanding coordinates

In this document
chevron_rightPDF page coordinates
chevron_rightViewer page coordinates
chevron_rightConverting between PDF and viewer coordinates
chevron_rightWindow coordinates
chevron_rightConverting between window and viewer page coordinates
chevron_rightConverting between mouse locations and window coordinates
chevron_rightIn practice

When dealing with locations in WebViewer it can be important to understand what coordinate space they are located in. For example when you set the (x, y) value of an annotation through the WebViewer API the location is relative to the unzoomed page in viewer page coordinates, not PDF page coordinates.

linkPDF page coordinates

In a PDF document the location (0, 0) is at the bottom left corner of the page. The x axis extends horizontally to the right and y axis extends vertically upward.

A page may have a rotation or translation associated with it and in this case (0, 0) may no longer correspond to the bottom left corner of the page relative to the viewer.

For example here is the same page as above but rotated 90 degrees clockwise. Notice how the coordinates have all stayed the same relative to each other but (0, 0) is now in the top left corner relative to the viewport.

Generally when you're using WebViewer you won't be dealing with PDF coordinates directly.

linkViewer page coordinates

When reading or writing annotation locations in WebViewer these values are in viewer coordinates. The (0, 0) point is located at the top left of the page. The x axis extends horizontally to the right and the y axis extends vertically downward.

linkConverting between PDF and viewer coordinates

WebViewer uses a transformation matrix for each page to allow it to convert between PDF and viewer coordinates. The matrix takes into account the flipped y values and possibly translation or scaling.

Since XOD files are considered to be at 96 DPI and PDF files at 72 DPI the scaling factor for XOD files is 96/72 or 4/3. For PDF files there is no scaling applied, just the flipped y values.

Annotation locations inside XFDF are in PDF coordinates. When XFDF is imported into WebViewer the locations will be transformed into viewer coordinates automatically using the matrix. When exporting XFDF WebViewer reverses the process and converts back to PDF coordinates.

If you ever need to convert between PDF and viewer coordinates yourself you can use the getPDFCoordinates function to get PDF coordinates or the getXODCoordinates (for XOD files) or getViewerCoordinates (for PDF or Office files) functions.

For example:

var doc = readerControl.docViewer.getDocument();
// top left corner in viewer coordinates
var x = 0;
var y = 0;
var pageIndex = 0;

var pdfCoords = doc.getPDFCoordinates(pageIndex, x, y);
// top left corner has a high y value in PDF coordinates
// example return value { x: 0, y: 792 }

// convert back to viewer coordinates
var viewerCoords = doc.getViewerCoordinates(pageIndex, pdfCoords.x, pdfCoords.y);
// { x: 0, y: 0 }

linkWindow coordinates

These coordinates are relative to the browser window with (0, 0) in the top left corner. The x axis extends to the right and the y axis extends downwards as you scroll through the document content.

Note that the scroll position of the viewer does not affect these coordinates. For example if the user has scrolled to page 10 the window coordinates of the first page will still be the same.

Below you can see an example of the document being scrolled downwards but the window coordinates for the pages stay the same.

linkConverting between window and viewer page coordinates

WebViewer provides functions on the current display mode to convert between window and page coordinates. The pageToWindow and windowToPage functions. For example:

var displayMode = readerControl.docViewer.getDisplayModeManager().getDisplayMode();
var pageIndex = 0;
var pagePoint = {
  x: 0,
  y: 0
};

var windowPoint = displayMode.pageToWindow(pagePoint, pageIndex);
// { x: 212, y: 46 }

var originalPagePoint = displayMode.windowToPage(windowPoint, pageIndex);
// { x: 0, y: 0 }

linkConverting between mouse locations and window coordinates

Mouse locations are relative to the viewport so all that's required to convert to window coordinates is scrollLeft and scrollTop values of the viewer. If you're inside of a tool (for example your own custom tool) you can use this.getMouseLocation(e) to get window coordinates from a mouse event.

mouseLeftUp: function(e) {
  var windowCoords = this.getMouseLocation(e);
}

Or manually using the scroll values from the viewer element:

var getMouseLocation = function(e) {
  var scrollElement = document.getElementById('DocumentViewer');
  var scrollLeft = scrollElement.scrollLeft || 0;
  var scrollTop = scrollElement.scrollTop || 0;

  return {
    x: e.pageX + scrollLeft,
    y: e.pageY + scrollTop
  };
};

This is what happens internally in the default tools, and as we saw above the window coordinate can be transformed to a page coordinate with a function call.

linkIn practice

What if you want to double click on the page and add a DOM element at that location? You probably want the element to automatically scroll with the page and be able to reposition it after the zoom changes. The easiest way to do this is to position it absolutely inside the pageContainer element.

So you'll get the event object from the mouse double click event, transform that into window coordinates and convert those into page coordinates. DocumentViewer triggers a dblClick event so we can use that to get double clicks inside the viewing area.

viewerElement.addEventListener('ready', function() {
  var docViewer = myWebViewer.getInstance().docViewer;
  docViewer.on('dblClick', function(jqueryE, e) {
    // refer to getMouseLocation implementation above
    var windowCoordinates = getMouseLocation(e);
  });
});

You also need to figure out what page you double clicked on and you can use the getSelectedPages function to do this:

var displayMode = docViewer.getDisplayModeManager().getDisplayMode();
// takes a start and end point but we just want to see where a single point is located
var page = displayMode.getSelectedPages(windowCoordinates, windowCoordinates);
var clickedPage = (page.first !== null) ? page.first : docViewer.getCurrentPage() - 1;

Once you have the page you can get the page coordinates from the window coordinates:

var pageCoordinates = displayMode.windowToPage(windowCoordinates, clickedPage);

Then you can create your custom element and add it to the page container element. Note that the position and size need to be scaled by the zoom level.

var zoom = docViewer.getZoom();

var customElement = document.createElement('div');
customElement.style.position = 'absolute';
customElement.style.left = pageCoordinates.x * zoom;
customElement.style.top = pageCoordinates.y * zoom;
customElement.style.width = 100 * zoom;
customElement.style.height = 25 * zoom;
customElement.style.backgroundColor = 'blue';
customElement.style.zIndex = 35;

var pageContainer = document.getElementById('pageContainer' + clickedPage);
pageContainer.appendChild(customElement);

You'll notice that if you change the zoom or make the page rerender the elements will disappear. This is because when a page is rerendered it is resized and re-added to the DOM.

To handle this you can keep track of your custom elements and add them to the updated pageContainer on the pageComplete event. The full code looks like this:

var getMouseLocation = function(e) {
  var scrollElement = document.getElementById('DocumentViewer');
  var scrollLeft = scrollElement.scrollLeft || 0;
  var scrollTop = scrollElement.scrollTop || 0;

  return {
    x: e.pageX + scrollLeft,
    y: e.pageY + scrollTop
  };
};

var domElements = {};
var elementWidth = 100;
var elementHeight = 25;

viewerElement.addEventListener('ready', function() {
  var docViewer = myWebViewer.getInstance().docViewer;

  docViewer.on('dblClick', function(jqueryE, e) {
    // refer to getMouseLocation implementation above
    var windowCoordinates = getMouseLocation(e);

    var displayMode = docViewer.getDisplayModeManager().getDisplayMode();
    var page = displayMode.getSelectedPages(windowCoordinates, windowCoordinates);
    var clickedPage = (page.first !== null) ? page.first : docViewer.getCurrentPage() - 1;

    var pageCoordinates = displayMode.windowToPage(windowCoordinates, clickedPage);

    var zoom = docViewer.getZoom();

    var customElement = document.createElement('div');
    customElement.style.position = 'absolute';
    customElement.style.left = pageCoordinates.x * zoom;
    customElement.style.top = pageCoordinates.y * zoom;
    customElement.style.width = 100 * zoom;
    customElement.style.height = 25 * zoom;
    customElement.style.backgroundColor = 'blue';
    customElement.style.zIndex = 35;

    var pageContainer = document.getElementById('pageContainer' + clickedPage);
    pageContainer.appendChild(customElement);

    if (!domElements[clickedPage]) {
      domElements[clickedPage] = [];
    }

    // save left and top so we can scale them when the zoom changes
    domElements[clickedPage].push({
      element: customElement,
      left: pageCoordinates.x,
      top: pageCoordinates.y
    });
  });

  docViewer.on('pageComplete', function(e, pageIndex) {
    if (domElements[pageIndex]) {
      var zoom = docViewer.getZoom();
      var pageContainer = document.getElementById('pageContainer' + pageIndex);

      // add back and scale elements for the rerendered page
      domElements[pageIndex].forEach(function(elementData) {
        elementData.element.style.left = elementData.left * zoom;
        elementData.element.style.top = elementData.top * zoom;
        elementData.element.style.width = elementWidth * zoom;
        elementData.element.style.height = elementHeight * zoom;
        pageContainer.appendChild(elementData.element);
      });
    }
  });
});