RadPdfViewer : Get PDF coordinates based on mouse position

1 Answer 1050 Views
PdfViewer and PdfViewerNavigator
Andrea
Top achievements
Rank 1
Andrea asked on 15 Dec 2022, 11:36 AM

Hi all.

Question: is it possible to translate (back and forth) mouse coordinates in RadPdfViewer to PDF coordinates on the relative page?

I do not need location inside Telerik internal document representation nor on screen but on the PDF representation for interoperability with other tools.

What I mean by PDF coordinates: a coordinate system where the unit is 1/72th of an inch, having the origin (coordinate 0,0) at the bottom left of the page, and X Y increase toward the top right corner, for example an A4 bottom left coordinates are (0,0) top right  (597.6 , 842.4) 

The desired effect would be something like

 

public (int pageNo, float pdfX, float pdfY) GetPdfCoordinates(RadPdfViewer viewer,  Point mouseEventLocation){
public (int pageNo, int X, int Y) GetViewerCoordinates(RadPdfViewer viewer, int page, float pdfX, float pdfY);

Reasons: I am currently using other libraries only for that specific purpose but the telerik pdf viewer is just as good except for this important feature that allows software to interoperate with third parties PDF tools.

Thank you all.

 

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 20 Dec 2022, 09:49 AM

Hello Andrea,

Thank you for contacting us. 

If I understand your requirement correctly, you want to get the document location clicked position and convert the coordinates to start from the bottom left position. I am assuming that you want to do this on mouse click. In this case, first, you will need to convert the mouse click position to document location. In the following code snippet, the e.Location will be the mouse position from the Click event handler arguments.

var tt = element.TotalTransform;               
tt.Scale(this.radPdfViewer1.ScaleFactor, this.radPdfViewer1.ScaleFactor);
tt.Invert();
var pointInDoc = tt.TransformPoint(e.Location);

To convert the documentation location you need only to transform the Y value. For this purpose, you need the height of the current page. You can get it from the current page element.

Location documentlocation = new Location()
                {
                    Left = pointInDoc.X,
                    Top = pointInDoc.Y,
                    Page = element.Page,
                };
RadFixedPageElement element = radPdfViewer1.ElementTree.GetElementAtPoint(args.Location) as RadFixedPageElement;
var transformtPoint = element.Size.Height - documentlocation.Top;

Here is the result from my side:

I am also attaching the sample project which I used to test your requirement. I am not sure that this will cover all your cases, but I think it will be a good starting point. You can extend the approach to fit your application.

I hope that I was able to help you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Andrea
Top achievements
Rank 1
commented on 20 Dec 2022, 11:12 AM

Thank you, but my requirements are that units are expressed in 1/72th of an inch, as PDF does.

Your code would be perfect if i can have the page measures in physical unit. is that possible?

 

Dimitar
Telerik team
commented on 21 Dec 2022, 09:46 AM

Hi Andrea. 

You can use the Unit class to convert sever types of measures:

var newSize = Unit.PointToDip(9);

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,

Dimitar

 

 

Tags
PdfViewer and PdfViewerNavigator
Asked by
Andrea
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or