Section:

Get & convert PDF coordinates using JavaScript

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.

PDF 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.

Here are some APIs to get the width and height of a page.

Apryse Docs Image

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.

Here is an API to get the rotation of a page.

Apryse Docs Image

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

Viewer 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.

Apryse Docs Image

Converting 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:

1const { documentViewer } = instance.Core;
2
3documentViewer.addEventListener('documentLoaded', () => {
4 const doc = documentViewer.getDocument();
5 // top left corner in viewer coordinates
6 const x = 0;
7 const y = 0;
8 const pageNumber = 1;
9
10 const pdfCoords = doc.getPDFCoordinates(pageNumber, x, y);
11 // top left corner has a high y value in PDF coordinates
12 // example return value { x: 0, y: 792 }
13
14 // convert back to viewer coordinates
15 const viewerCoords = doc.getViewerCoordinates(pageNumber, pdfCoords.x, pdfCoords.y);
16 // { x: 0, y: 0 }
17});

Window 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.

Apryse Docs Image

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.

Apryse Docs Image

Converting 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:

1const { documentViewer } = instance.Core;
2
3const displayMode = documentViewer.getDisplayModeManager().getDisplayMode();
4const pageNumber = 1;
5const pagePoint = {
6 x: 0,
7 y: 0
8};
9
10const windowPoint = displayMode.pageToWindow(pagePoint, pageNumber);
11// { x: 212, y: 46 }
12
13const originalPagePoint = displayMode.windowToPage(windowPoint, pageNumber);
14// { x: 0, y: 0 }

Converting 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.

JavaScript

1mouseLeftUp: function(e) {
2 const windowCoords = this.getMouseLocation(e);
3}

Or manually using the scroll values from the viewer element:

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.

In 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.

1const { documentViewer } = instance.Core;
2documentViewer.addEventListener('dblClick', e => {
3 // refer to getMouseLocation implementation above
4 const windowCoordinates = getMouseLocation(e);
5});

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

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

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

JavaScript

1const 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.

1const { iframeWindow } = instance.UI;
2const zoom = documentViewer.getZoom();
3const customElement = document.createElement('div');
4customElement.style.position = 'absolute';
5customElement.style.left = pageCoordinates.x * zoom;
6customElement.style.top = pageCoordinates.y * zoom;
7customElement.style.width = 100 * zoom;
8customElement.style.height = 25 * zoom;
9customElement.style.backgroundColor = 'blue';
10customElement.style.zIndex = 35;
11const pageContainer = iframeWindow.document.getElementById('pageContainer' + clickedPage);
12pageContainer.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:

1const getMouseLocation = e => {
2 const scrollElement = document.getElementById('DocumentViewer');
3 const scrollLeft = scrollElement.scrollLeft || 0;
4 const scrollTop = scrollElement.scrollTop || 0;
5 return {
6 x: e.pageX + scrollLeft,
7 y: e.pageY + scrollTop
8 };
9};
10const domElements = {};
11const elementWidth = 100;
12const elementHeight = 25;
13const { iframeWindow } = instance.UI;
14const { documentViewer } = instance.Core;
15documentViewer.addEventListener('dblClick', e => {
16 // refer to getMouseLocation implementation above
17 const windowCoordinates = getMouseLocation(e);
18 const displayMode = documentViewer.getDisplayModeManager().getDisplayMode();
19 const page = displayMode.getSelectedPages(windowCoordinates, windowCoordinates);
20 const clickedPage = (page.first !== null) ? page.first : documentViewer.getCurrentPage();
21 const pageCoordinates = displayMode.windowToPage(windowCoordinates, clickedPage);
22 const zoom = documentViewer.getZoom();
23 const customElement = document.createElement('div');
24 customElement.style.position = 'absolute';
25 customElement.style.left = pageCoordinates.x * zoom;
26 customElement.style.top = pageCoordinates.y * zoom;
27 customElement.style.width = 100 * zoom;
28 customElement.style.height = 25 * zoom;
29 customElement.style.backgroundColor = 'blue';
30 customElement.style.zIndex = 35;
31 const pageContainer = iframeWindow.document.getElementById('pageContainer' + clickedPage);
32 pageContainer.appendChild(customElement);
33 if (!domElements[clickedPage]) {
34 domElements[clickedPage] = [];
35 }
36 // save left and top so we can scale them when the zoom changes
37 domElements[clickedPage].push({
38 element: customElement,
39 left: pageCoordinates.x,
40 top: pageCoordinates.y
41 });
42});
43documentViewer.addEventListener('pageComplete', pageNumber => {
44 if (domElements[pageNumber]) {
45 const zoom = documentViewer.getZoom();
46 const pageContainer = iframeWindow.document.getElementById('pageContainer' + pageNumber);
47 // add back and scale elements for the rerendered page
48 domElements[pageNumber].forEach(elementData => {
49 elementData.element.style.left = elementData.left * zoom;
50 elementData.element.style.top = elementData.top * zoom;
51 elementData.element.style.width = elementWidth * zoom;
52 elementData.element.style.height = elementHeight * zoom;
53 pageContainer.appendChild(elementData.element);
54 });
55 }
56});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales