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:

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

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:

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

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:

1const getMouseLocation = e => {
2 const scrollElement = documentViewer.getScrollViewElement();
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};

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.

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

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales