Skip to main content
Participating Frequently
December 12, 2023
Answered

How to zoom to a rectangular region of a pdf page using javascript

  • December 12, 2023
  • 1 reply
  • 2044 views

I have experimented a lot using viewState function

 

this.viewState.toSource();

var refView = eval(viewState.toSource());

refView.pageViewZoom= 5;

refView.pageViewY = 1500;

refView.pageViewX = 800;

this.viewState = refView;

this.viewState.toSource();

 

but it is going haywire and is not able to focus on the page at the X and Y coordinates.

 

I have tried page scroll also and there is no description in the manual. 

 

Your help is very much appreciated.

This topic has been closed for replies.
Correct answer Thom Parker

Dear Thom  Parker,

I know i am taking your time and this involves a lot of geometry..  

i experimented with this "scroll" function line for various x and y values. I know the page dimensions using the  getpageBox properties.

this.scroll(0, 792 - (2 * 72));

but this not behaving as expected as it is not moving in x or y direction for some values.

 

How do i get the screen view dimension using javascript. i tried "crop" properties but doesn't seem to be helping.


In that particular line of code, the x coord is 0, which scrolls to the left edge of the page. 

Perhaps a better way to think of it is that you are not scrolling (or moving ) the page, but you are moving the viewport over the page. To see this in action, open the "page navigation" panel on the left side of the Acrobat window. The viewport is drawn over the thumbnail for the current page. As you zoom and scroll you can see the viewport change size and move.  

 

Also keep in mind that the viewport will not scroll beyond it's limits. If the zoom is set to show the full page, then scrolling isn't possible. Scrolling only works when zoomed in enough to allow room for the viewport to move. 

You'll be able to see how this works by observering the viewport in the page navigation window.  

 

1 reply

Thom Parker
Community Expert
December 12, 2023

To navigate to a particular page location you need to know more than is available in a single viewstate. For example, the pageViewX and pageViewY are in screen coordinates, not PDF page coordinates.  They are measured from the top left corner of the view box to the top left corner of the page.  So to scroll using the viewState you have to know the scaling between page coordinates and screen coordinates, and you have to know how much of the page is showing in the view box.   To get this information you need to acquire the view state when the page width or height are known relative to the view box.  This gives you the view box size and a starting point for calculating the scaling, i.e. the zoom level at which the full document size matches the view box in screen coordinates. From there the scaling between page coords and screen coords is proportional to the zoom level. So you can get coordinates in either system. Use the screen coords to navigate using the viewState, or use page coords to navigate using the scroll() function. 

I worked all this out years ago and it's on the paid for page, with examples:

https://www.pdfscripting.com/public/DocumentNavigation.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
December 12, 2023

Thanks for your reply. I see those links are in the membership pages. 

I don't use these scripting often. Is it possible to provide me the method only for this specific purpose of focusing to the page coordinates.

Thanks in advance

Thom Parker
Community Expert
December 12, 2023

Explain what you are trying to accomplish. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often