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:
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?
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.
@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.
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
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.
@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.
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.