Get the coordinates of RadPdfViewerElement

4 posts, 0 answers
  1. Shashika
    Shashika avatar
    2 posts
    Member since:
    Dec 2015

    Posted 08 Oct Link to this post

    In my Windows forms application I'm using GetLocationFromViewPoint method to get the coordinates when drawing a rectangle on the Pdf file.Again when I'm loading the same Pdf I need to draw the rectangle using the coordinates defined in the earlier case. But when drawing the rectangle the Y coordinate is incorrrectly drawn. The problem here is I'm using the RadFixedPageElement positions for the GetLocationFromViewPoint method and Now I need to get the RadPdfViewerElement coordinates to draw the rectangle in correct position. Is there any way to get this done ? Please help.
  2. Hristo
    Admin
    Hristo avatar
    1084 posts

    Posted 11 Oct Link to this post

    Hello Shashika,

    Thank you for writing.

    I am not exactly sure how you are using the GetLocationFromViewPoint method. I am assuming, however, that you might be missing adjusting the positions with the scroll offset. Please check my code snippet below showing how this adjustment can be performed so that you receive the correct coordinates with respect to the clicked page: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            this.radPdfViewer1.LoadDocument(@"..\..\sample.pdf");
     
            this.radPdfViewer1.MouseDown += RadPdfViewer1_MouseDown;
        }
     
        private void RadPdfViewer1_MouseDown(object sender, MouseEventArgs e)
        {
            RadFixedPage page = null;
            System.Drawing.Point location;
     
            if (this.radPdfViewer1.PdfViewerElement.GetLocationFromViewPoint(this.GetPositionWithScrollOffset(e.Location), out page, out location))
            {
                RadMessageBox.Show("Location: " + location + " Page: " + page);
            }
        }
     
        private System.Drawing.Point GetPositionWithScrollOffset(Point position)
        {
            System.Drawing.Point res = position;
            System.Drawing.Point scrollOffset = this.radPdfViewer1.PdfViewerElement.GetScrollOffset();
            res.X += scrollOffset.X;
            res.Y += scrollOffset.Y;
     
            return res;
        }
    }

    GetLocationFromViewPoint
    I am also attaching a short video showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Shashika
    Shashika avatar
    2 posts
    Member since:
    Dec 2015

    Posted 11 Oct in reply to Hristo Link to this post

    Hi Hristo,

    Thank you for reply me back. I'm using GetLocationFromViewPoint method in MouseDown and MouseUp events to draw rectangles on a Pdf file. Then I'm saving those coordinates in a separate file. When I' m redrawing the rectangle again on the same Pdf it it not correctly drawing on the locations it draws earlier. I assume it is because of the gap between the RadPdfViewerElement and the RadFixedPageElement. I need to to get the extract position to draw the rectangle in correct position.

    Here is my code I'm using to get the coordinates in MouseUp and MouseDown events.

    private void radPdfVwr_MouseDown(object sender, MouseEventArgs e)
            {
                if ((radPdfVwr.ElementTree.GetElementAtPoint(((MouseEventArgs)e).Location)).GetType().Name == typeof(RadFixedPageElement).Name)
                {
                    if (isMouseDwnClked == true)
                        return;

                    currentPos = startPos = e.Location;
                    drawing = true;

                    Point currentPos1 = new Point(e.X, e.Y);
                    currentPos1 = new Point(currentPos.X + this.radPdfVwr.PdfViewerElement.HScrollBar.Value, currentPos.Y + this.radPdfVwr.PdfViewerElement.VScrollBar.Value);

                    Graphics formGraphics = this.CreateGraphics();

                    Telerik.Windows.Pdf.Documents.Fixed.Model.RadFixedPage page;
                    this.radPdfVwr.PdfViewerElement.GetLocationFromViewPoint(currentPos1, out page, out down);

                    _top = Math.Round(((down.Y / formGraphics.DpiY) * 25.4), 2);
                    _left = Math.Round(((down.X / formGraphics.DpiX) * 25.4), 2);

        }

    }

    private void radPdfVwr_MouseUp(object sender, MouseEventArgs e)
            {
                if ((radPdfVwr.ElementTree.GetElementAtPoint(((MouseEventArgs)e).Location)) != null)
                {
                    if ((radPdfVwr.ElementTree.GetElementAtPoint(((MouseEventArgs)e).Location)).GetType().Name == typeof(RadFixedPageElement).Name)
                    {
                        if (isMouseUpClked == true && rectanglesList.Count > 0)
                            return;

                        if (drawing)
                        {
                            drawing = false;
                            var rc = GetRectangle();
                            if (rc.Width > 0 && rc.Height > 0)
                                rectanglesList.Add(rc);

                            radPdfVwr.Invalidate();
                        }

                        Point currentPos = new Point(e.X, e.Y);
                        currentPos = new Point(currentPos.X + this.radPdfVwr.PdfViewerElement.HScrollBar.Value, currentPos.Y + this.radPdfVwr.PdfViewerElement.VScrollBar.Value);

                        Graphics formGraphics = this.CreateGraphics();
                        Telerik.Windows.Pdf.Documents.Fixed.Model.RadFixedPage page;

                        Point pointLoc;
                        Point x;

                        this.radPdfVwr.PdfViewerElement.GetLocationFromViewPoint(currentPos, out page, out pointLoc);

                        _height = Math.Round((((pointLoc.Y - down.Y) / formGraphics.DpiY) * 25.4), 2);
                        _width = Math.Round((((pointLoc.X - down.X) / formGraphics.DpiX) * 25.4), 2);

    }

    }

  4. Hristo
    Admin
    Hristo avatar
    1084 posts

    Posted 12 Oct Link to this post

    Hello Shashika,

    Thank you for writing back.

    Please bear in mind that the GetLocationFromViewPoint returns boolean and it may not always extract the page and location within the page, for example, if you pass wrong source view-point. In order to extract the correct clicked location on a page, please consider using the snippet I sent you. It takes into consideration the applied horizontal and vertical scrollbar values. Since you are caching these coordinates please also consider saving the applied zoom by caching the ScaleFactor property.

    I hope this information was useful. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top