get/set page position in mobile mode

51 views
Skip to first unread message

Andrew Borzilo

unread,
Oct 23, 2017, 11:49:30 AM10/23/17
to PDFTron WebViewer
I need to to get/set page position/offset in mobile mode when the page is scaled up so it's bigger than screen and can be dragged around.
Something like x/y offsets in pixels of left top corner of the page relative to the left top corner of the screen.
So I could get/set these values for the current page. Also I need some way to subscribe to event of page movement.
How I can achieve this?

Thanks.

Justin Jung

unread,
Oct 23, 2017, 7:58:38 PM10/23/17
to PDFTron WebViewer
Hello,

You could set the page offset as follows:

function setOffset(pageNumber, x, y) {
  readerControl.c.tX = x - readerControl.displayMode.getPageOffset(pageNumber - 1).x;
  readerControl.c.tY = y;
  readerControl.setZoomLevel(readerControl.docViewer.getPageZoom(pageNumber - 1), false);
}

Note that x and y should be negative to shift the view right and downwards.

To subscribe to page movement event, you can listen to pageComplete:

$(document).on('viewerLoaded', function() {
  readerControl.docViewer.on('pageComplete, function(e, pageIndex) {
    // handler
  });
});

This is triggered whenever a page finishes rendering.

Justin Jung
Software Developer
PDFTron Systems Inc.

Andrew Borzilo

unread,
Oct 25, 2017, 12:27:44 PM10/25/17
to PDFTron WebViewer
Hello!

Thanks for responce, but unfortunately your setOffset doesn't work for me.
Actually vertical positioning looks working. At least using y=0 moving view to the top of the page .
But horizontal is totally confusing me. I was testing in chrome mobile emulation mode for Galaxy S5.

Lets say I'm open the document in portrait mode, then while on the first page, I'm double click the page to scale it up.
Then dragging the page to see left top corner and logging values of:
readerControl.c.tX
readerControl.c.tY
readerControl.displayMode.getPageOffset(pageNumber - 1).x
readerControl.displayMode.getPageOffset(pageNumber - 1).y
to the console.

What I'm expecting to see is: tX + pageOffset.x and tY + pageOffset.y both should be zero.
What I have seen actually is:
tX = 360 
tY = -195.96469104665812 
pageOffset.x = 0 
pageOffset.y = 195.96469104665826

and if I'm trying to get these values for the second page, I'm getting:
tX = 720 
tY = -195.96469104665812 
pageOffset.x = 0 
pageOffset.y = 195.96469104665826

if I switch to landscape mode, I'll get these for the first page:
tX = -270.34224598930473 
tY = 0 
pageOffset.x = 578.3422459893047 
pageOffset.y = 0

So I'm totally confused with these values which make impossible for me to use your solution.
Do you have any suggestions?

Thanks.

Justin Jung

unread,
Oct 27, 2017, 5:06:37 PM10/27/17
to PDFTron WebViewer
Thanks for the detailed response. We've tested a bit more, and made a couple of fixes to the original solution.

function setOffset(pageNumber, x, y) {
  readerControl.c.tX = x - readerControl.displayMode.getPageOffset(pageNumber - 1).x - readerControl.vWOffset;
  readerControl.c.tY = y - readerControl.displayMode.getPageOffset(pageNumber - 1).y;
  readerControl.setZoomLevel(readerControl.docViewer.getPageZoom(pageNumber - 1), false);
}

When you are on pages greater than 1, you need to subtract the vWOffset which is basically scroll position through the document.
Also, for the vertical position, you need to subtract the y offset.
On our end, this seems to work for us to set page position correctly (we've tested [0,0], [0,-100], [-100,0] and all of them behaved as expected).
Let us know how this works for you.

Andrew Borzilo

unread,
Oct 30, 2017, 1:00:25 PM10/30/17
to PDFTron WebViewer
Thank you very much, now it's working perfectly.
Reply all
Reply to author
Forward
0 new messages