Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Dec 12, 2023 Dec 12, 2023

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.

TOPICS
Acrobat SDK and JavaScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 13, 2023 Dec 13, 2023

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

...
Translate
Community Expert ,
Dec 12, 2023 Dec 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2023 Dec 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2023 Dec 12, 2023

Explain what you are trying to accomplish. 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2023 Dec 12, 2023

I am trying to create page link, which on clicking should go to another page with particular zoom and x , y coordinates.

This requires to be automated using JavaScript as I want to do it for several pages.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2023 Dec 12, 2023

This is not a trivial problem.  It takes a little geometry.  I can tell you how to approach a solution, but coding it is up to you. 

The easiest solution is to use the scroll() function. It places the upper left corner of the view box relative to the bottom left corner of the current page. The view box is what the user sees on the screen as a result of the current page, zoom, and scroll. You can see the view box on the Acrobat's pages panel.  The values of the X and Y scroll positions are in standard Acrobat User Coordinates. Positions are given in points, where there are 72 points/inch. The "scroll()" function will not place the view box above or below the limits of the page if the page display is set to single page. But scrolling will cause page changes if the page display is set to continous. So, this scrolling scheme is a bit awkward in the vertical direction.

 

For example, to place the scroll box 2 inches from the top of the page you would need to know the height of the page. Say for example that the page is a standard 8.5(612pts) x 11(792pts) inches. The scroll code would be:

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

 

So a generic scroll to a specific page location is 

this.scroll(x, PageHeight-y);

where x and y are in page coordinates. 

 

But keep in mind that x and y above represent the edges of the scroll, they don't center on the coordinates. If x=72, the page is scrolled 1 inch to the right from the left edge. For centering you have to know the size of the view box at a particular zoom level. I pretty much explained the method for finding the view box in my first post. 

 

 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2023 Dec 12, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2023 Dec 13, 2023

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.  

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2023 Dec 13, 2023

Thanks. but it doesn't get in to me.. that a simple task of able to zoom to a page coordinate is so difficult. 

The scroll is not working at all....

Thanks for your time

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 13, 2023 Dec 13, 2023

Dear Parker, 

Thanks very much for your help. now with the pan and zoom window, i am able to better understand this scroll function.

Very helpful tip.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2023 Dec 13, 2023
LATEST

It always helps when you can see what's going on.  

Good Luck!

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2023 Dec 12, 2023

Create a Named Destination and use the gotoNamedDest method to jump to it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2023 Dec 12, 2023
quote

Create a Named Destination and use the gotoNamedDest method to jump to it.


By try67


Easy method! But time consuming and difficult to maintain. 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2023 Dec 12, 2023

Thanks for your reply. 
Since i want to automate this process using javascript, i dont think there is a way to automatically create named destination using javascript. unless i missed something.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2023 Dec 12, 2023

You are correct, there is no way to create named destinations with JavaScript. You need to workout the geometry. 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2023 Dec 12, 2023

Thanks for your reply. 
Since i want to automate this process using javascript, i dont think there is a way to automatically create named destination using javascript. unless i missed something.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines