Get position of current view and restore it

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

KarlTH
User
Posts: 57
Joined: Mon Aug 01, 2005 2:33 pm

Get position of current view and restore it

Post by KarlTH »

Hello Tracker-Support,

can I get the position of the current view for a PDF? e.g. middle of the page
Because I am loading a PDF into a memory stream and display the PDF from the memory stream which was opened before as a file and if the user has a high zoom factor and stands on the middle of the page I only can restore the zoom factor but the position always jumps to the beginning of the page. :evil:

I have found in the documentation the property RestoreLastView

Code: Select all

AxPDFXViewCtrl.SetProperty("Documents.RestoreLastView", 1)
but it does not do restore the last view for me. Maybe it is because I am loading the PDF from a memory stream?


Thanks,

Karl
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Get position of current view and restore it

Post by Stefan - PDF-XChange »

Hi Karl,

Yes because your documents are loaded from memory streams - the Viewer treats them as different files.
You will need to store yourself the position at which a document was when it was closed using e.g.

Obtain the coordinates of the top left corner of the Pages View window (main rendering area) using Pages.ScreenX and Pages.ScreenY

Use TranslateScreenPoint - with the coordinates of the top left corner to figure out which point of the document was displayed in that corner of the pages View Windos and store this value.

Then the next time your customer opens the same file - use the MovePointToScreenPoint and the correct zoom level - and the document should open exactly at the same position. The coordinates in MovePointToScreenPoint are relative to the desktop - so don't forget to once again obtain the values of Pages.ScreenX and Pages.ScreenY - and adjust the coordinates accordingly.

Best,
Stefan
KarlTH
User
Posts: 57
Joined: Mon Aug 01, 2005 2:33 pm

Re: Get position of current view and restore it

Post by KarlTH »

Hello Stefan,

you are a genius! :idea:
I did so and know it works like a charm!

Here is my VB .NET code for information purpose:

Code: Select all

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)

        Dim oDataIn As Object
        Dim oDataOut As Object

        Dim dblScreenX As Integer
        Dim dblScreenY As Integer

        Dim dblPagePoints As Double()
        Dim dblPagePointX As Integer
        Dim dblPagePointY As Integer


        oDataOut = Nothing
        '#THK# 07.08.2013 Get Screen X
        AxPDFXViewCtrl.GetDocumentProperty(iPDocCurrentID, "Pages.ScreenX", oDataOut, 0)
        dblScreenX = DirectCast(oDataOut, Integer)

        '#THK# 07.08.2013 Get Screen Y
        AxPDFXViewCtrl.GetDocumentProperty(iPDocCurrentID, "Pages.ScreenY", oDataOut, 0)
        dblScreenY = DirectCast(oDataOut, Integer)

        '#THK# 07.08.2013 Get page point X and Y
        oDataIn = New Object(1) {dblScreenX, dblScreenY}
        AxPDFXViewCtrl.DoDocumentVerb(iPDocCurrentID, "Pages[" + iPDDocCurrentPage.ToString + "]", "TranslateScreenPoint", oDataIn, oDataOut, 0)

        If oDataOut Is Nothing = True Then
            'iSigFieldValues = New Integer() {0, 0, 0, 0}
            dblPagePoints = New Double() {0, 0}
        End If
        If oDataOut.GetType.Name <> "Double[]" Then
            'iSigFieldValues = New Integer() {0, 0, 0, 0}
            dblPagePoints = New Double() {0, 0}
        End If

        dblPagePoints = TryCast(oDataOut, Double())

        dblPagePointX = Convert.ToInt32(dblPagePoints(0))
        dblPagePointY = Convert.ToInt32(dblPagePoints(1))

        '#THK# 07.08.2013 Load PDF into memory stream and show it
        LoadPDAsStream(strPDocFile)

        ' MovePointToScreenPoint
        ' ------------------------------------------------------------------------
        '1 Yes double Specifies x coordinate of the page point in points.
        '2 Yes double Specifies y coordinate of the page point in points.
        '3 Yes LONG Specifies x coordinate of screen point, relative to desktop.
        '  See also to the: Pages.ScreenX, Pages.ScreenY.
        '4 Yes LONG Specifies y coordinate of screen point, relative to desktop.
        oDataIn = New Object(3) {dblPagePointX, dblPagePointY, dblScreenX, dblScreenY}
        AxPDFXViewCtrl.DoDocumentVerb(iPDocCurrentID, "Pages[" + iPDDocCurrentPage.ToString + "]", "MovePointToScreenPoint", oDataIn, Nothing, 0)

End Sub
Thanks a lot,

Karl
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: Get position of current view and restore it

Post by Stefan - PDF-XChange »

Glad to assist Karl,

And thanks for sharing your code! Hopefully it would be useful to other developers too!

Cheers,
Stefan