PDF displayed in HTMLViewer not scrolled to top

Hello,
when displaying PDF-files in an HTMLViewer the content of the PDF file is never scrolled to beginning/top. Each fiIe I tried was scrolled to an other Y-position, different from 0. With some files I tried the content is scrolled down only a bit and with others half a page.
To see the problem, please create a new macOS desktop projekt, drag an HTMLViewer to Window1 and put this code into its open event:

Me.LoadURL("http://developer.xojo.com/export/download_pdf?topic_id=1662640")

Now run the project and wait until the PDF-file is displayed.

Loading the same URL in Safari, the content is always scrolled to the top. Is there a way to make the HTMLViewer display PDF-Files scrolled to the beginning/top?

There is no way to scroll other than manually, because a PDF document not being an HTML page, JavaScript cannot be used.

If you embed the PDF into an HTML page as picture, then you can use scrollTo :
https://www.w3schools.com/jsref/met_win_scrollto.asp

Christian, I just tested on macOS 10.12.6 and Xojo 2017r2.1, it always show the top page.

I scrolled down a little, closed the running app, run again and again I see the top.

Edit: I redid my test changing the Window’s default size from 600 x 400 to other values and now I can see that the PDF is a little off. I decided to do this because I put locks all around the htmlviewer and when I resized the window I saw the PDF increase/decrease the size AND move off-top. My guess is that this is a rendering issue/calculation that Xojo could fix.

Edit2: It looks like bigger the window/htmlviewer area = bigger jump from the top when finish rendering. If you want to file a bug report I could add some screen captures/recordings.

Hello and thanks for your replies,

@Alberto: You are right, the PDF displays correctly (scrolled to the top) when the Window is 640 x 480, but the Window in my app is larger an every PDF I tested was scrolled down (sometimes more, sometimes less). And it seems that when increasing the size of Window1 in the IDE and running the project, also the y-displacement increases.
For example when i make width = 2000 and height=4000 and run the project then the PDF is scrolled down more then 1.5 pages!

@Michel: These are PDF-files with multiple pages and new files are created regularly by the users. How could I embed the PDF-files into the HTML page as picture? Could you give me some more information?

Christian, I just did a test on Windows and the PDF renders perfect every time. It shows the top no matter the window start size or if I resize the window with htmlviewer locked from all sides.

Can you create a Feedback case?

If a plugin solution is okay then the follwing makes a picture out of the pdf that is in the variable “data”:

try dim thePDF as new PDFDocumentMBS(data) if thePDF <> nil then dim thePDFPage as PDFPageMBS = thePDF.pageAtIndex(0) dim theColor as NSColorMBS = NSColorMBS.whiteColor if thePDFPage <> nil then dim theImage as NSImageMBS = thePDFPage.Render(72.0, 0, theColor) if theImage <> nil then DataPicture = theImage.CopyPicture end if end if Catch err as NSExceptionMBS 'no pages end try

Mac-only of course.

1 - Add this constant to the window or to a module :

[code]

[/code]

If you want to size the picture to the HTMLViewer, you have to add to the page code width and height :

[code]

[/code]

Use a replace method to suspstitute the tokens wdth and hght with the width and height you need.

2 - Here is how you display the pdf :

dim f as FolderItem = SpecialFolder.desktop.child("xmetro.pdf") me.LoadPage(Replace(Page,"*", f.URLPath), Nil)

2 - Here is what you do to scroll to the top :

HTMLViewer1.ExecuteJavaScript("window.scrollTo(0, 0);")

Note that you can use that to scroll to any position left, top.

I did verify, though, that normally the PDF is sized to the HTMLViewer when you load it directly. I don’t quite understand why it should not be displayed at 0,0.

Thanks for your replies.

@Alberto: Yes, I will create a case in „Feedback“.
Edit: <https://xojo.com/issue/50918>

@Beatrix: Your solution works and maybe I will use this solution by getting all the pages within a loop. Currently only a Mac version of the app is needed.

@Michel: Your solution works too, but only the first page of the PDF-file is shown in my test.

Christian, you did not indicate in your OP it was a multi-pages PDF.

Christian, I did more tests.

I have a Windows 7 machine that uses a different app (as default) to open PDF files, when I ran you example from 50918 it used that app as rendering engine.

So I returned to Mac, downloaded the test file, opened with Preview and it has the same issue. I guess the team will not be able to change the behavior unless they provide a proprietary PDF rendering engine.

I tried to change the default PDF viewer on Mac, to see if Htmlviewer will try to use that but I was not able to make any change. I’m sorry for the bad news.

Yes, I forgot to mention this in the OP because the PDF-file loaded in the example already has mutliple pages. It’s a pity that your example does ot work for PDF-files with mutliple pages. For cases with single-page PDF files your example comes very handy.

@Alberto: Thanks for making these tests! I also did more tests on mac and I was not able to make the PDF files displayed scrolled to top. My current solution is this: Since I already have the MBS-plugins and the app will be used only on mac I will simply use the classes Beatrix mentioned.