This is a migrated thread and some comments may be shown as answers.

Save Click location and GoToDestination

1 Answer 242 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Clément
Top achievements
Rank 1
Clément asked on 24 Jul 2014, 11:37 AM
Hi,
I can not determine the coordinates of a mouse click on a pdf page. 
I would like to save the coordinates and page number in a "bookmark" for then navigate these bookmarks using the method GoToDestination. 

Can you help me?

Clement

1 Answer, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 25 Jul 2014, 03:43 PM
Hello Clément,

Thank you for contacting us.

If you want to get the mouse click event you can subscribe for PreviewMouseDown event on PdfViewer's FixedDocumentPresenter. Then having the view point you can get the clicked Page and Location point on the page by using GetLocationFromViewPoint method. 
You should be aware that currently there is an issue with this method and it currently works with document point instead of view point. This issue will be fixed in the next LIB. As a workaround for now you can simply add PdfViewer's scroll offsets in order to get the document point from the view point.
The following code snippet shows how to subscribe for the PreviewMouseDown event and save the last clicked position as an Destination instance. In order to get this code to work you should add the Initialize method right after the InitializeComponent in the constructor of your Window instance. Additionally you may use the GoToLastClickedPosition method in order to navigate to the last clicked position. 
private Destination lastDestination = null;
private IFixedDocumentPresenter presenter = null;
 
private UIElement Presenter
{
    get
    {
        return this.presenter as UIElement;
    }
}
 
private void Initialize()
{
    this.presenter = this.pdfViewer.FixedDocumentPresenter;
 
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown += PresenterMouseDown;
    }
 
    this.pdfViewer.FixedDocumentPresenterChanged += FixedDocumentPresenterChanged;
}
 
private void FixedDocumentPresenterChanged(object sender, EventArgs e)
{
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown -= PresenterMouseDown;
    }
 
    this.presenter = this.pdfViewer.FixedDocumentPresenter;
 
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown += PresenterMouseDown;
    }
}
 
private void PresenterMouseDown(object sender, MouseButtonEventArgs e)
{
    Point position = e.GetPosition(this.Presenter);
 
    // TODO: Delete this line when the bug is fixed in the next LIB
    position = new Point(position.X + this.pdfViewer.HorizontalScrollOffset, position.Y + this.pdfViewer.VerticalScrollOffset);
 
    RadFixedPage page;
    Point location;
    if (this.pdfViewer.FixedDocumentPresenter.GetLocationFromViewPoint(position, out page, out location))
    {
        this.lastDestination = new Location()
        {
            Page = page,
            Left = location.X,
            Top = location.Y,
            Zoom = this.pdfViewer.ScaleFactor
        };
    }
}
         
private void GoToLastClickedPosition()
{
    if (this.lastDestination != null)
    {
        this.pdfViewer.GoToDestination(this.lastDestination);
    }
}

Please note the line comment showing the workaround on the mentioned GetLocationFromViewPoint's issue. The line below the comment should be deleted after your next upgrade in order to prevent the behaviour breaking change in the sample code.

I hope this is helpful. If you have any more questions or concerns do not hesitate to contact us again.

Regards,
Deyan
the Telerik team
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
PDFViewer
Asked by
Clément
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Share this question
or