Get and set scroll settings on displayed PDF

I would like to display multiple multi-page PDFs, one at a time, and when I switch back to a given PDF, I would like it to display at the same scroll setting it had when last I saw it. I cannot get or set the scroll values when displaying a PDF in HTMLViewer, of course, and I’ve had no luck using the ExecuteJavaScript fixes I’ve seen here. Could someone help me out here? Thanks!

It will depend on the PDF viewer you are using. If it is rendering it to HTML, you can likely use the scrollTop of the overall enclosing element, which you might access something like this:

var scrollPos = document.getElementById('page-container').scrollTop.toString()

Where “page-container” is whatever element in your PDF viewer is the enclosing elements for the pages.

To reset it when you return, you’d just set this saved value using ExecuteJavaScript.

Or use an array of HTMLViewers each inside a PagePanel page?

I am developing on MacOS. I have an HTMLViewer control, ‘hview’, and to display the PDF, I do:
hview.LoadURL( “file://[path to the file].pdf” )
If I switch to viewing a different PDF in hview, I would like to get the scroll settings of the current page. If I ever go back to viewing the first PDF, I would like to restore it to the scroll settings it had when last it was displayed.
The problem I am encountering is that it seems that I have to do a call to ExecuteJavaScript to get the scroll settings for an HTMLViewer, and when I do that, they are always 0, 0, so I have to figure I am not understanding something.

Yeah, I thought about that, but I might easily have thirty PDFs that I am cycling through - I’m writing a program to help with grading tests - and having an HTMLViewer for each, and each one loaded, … I can’t imagine that would be a good thing, performance-wise. Admittedly though, I’ve not actually tried it. :^)

In that case, most likely the HTMLViewer is using the Preview plugin to display the PDF, though it could also be Acrobat – it’s using whatever is set to handle application/pdf on your computer. I don’t think either can be controlled via Javascript; maybe someone else knows.

You may want to look at using a javascript-based PDF viewer, such as PDF.js. You’d have a little more work (basically load an HTML with this embedded, then tell it to load your PDF), but then you’d be able to interact with it. The upside of this is it would work regardless of platform or how the user’s computer is configured.

Ah, okay, that is reassuring, actually. I was able to get scroll values using ExecuteJavaScript, but regardless of where the document was scrolled to, they were always 0, 0. Looks like that is because they just were not available, not (necessarily) because I screwed up the call. :^)

I might look into PDF.js, but first I will play with the other suggestion – using an array of HTMLViewer controls and flipping through them as needed – if only to see what kind of performance hit I take. Thanks!

I am curious, David: what might be the advantage of putting them into pages in a PagePanel control, rather than putting them directly on the main window and setting Visible = False for all but the current one?

[programs a little] Ahhhh, perhaps because, with PagePanel, you need only say that the desired panel is visible: you don’t have to make sure all the others are not visible.

Yes, it is easy to program and conceptually. It is simpler too if you have multiple items to show/hide eg Buttons, Labels.

I was having some issues with PagePanel, so I figured, as each page held only one HTMLViewer (no other controls), I’d forego the PagePanel, make an array of HTMLViewers, and try doing selection by just marking all as not visible and then marking the desired one visible. It works without any visible hitch. My test - 25 PDFs, ranging in size from 660kb to 28Mb - went smoothly, and the scroll positions were maintained. It appears I should have simply gone with the solution I thought of, which was also your suggestion of using an array of HTMLViewers, and not worried about performance until & unless I saw that it was a problem. :smiley:

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.