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

Draw scalable rectangles on document

1 Answer 360 Views
PdfViewer and PdfViewerNavigator
This is a migrated thread and some comments may be shown as answers.
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Zygmunt asked on 08 Nov 2019, 10:32 AM

Hey,

I'm working on Telerik UI for Winforms version 2019 R3. I have to implement in my project below functionality:

  1. Pdf file is loaded using RadPdfViewer
  2. User can mark using rectangles places on each page of the document
  3. After mark place by user I have to get coordinates of the rectangle in current page
  4. Each mark should be visible only on the page on witch is drawn
  5. Rectangles should be scalable and after scroll should point the same place

Is any possible  to realize above points?

I found something about custom layer https://www.telerik.com/forums/text-selection-and-highlight but the solution is in WPF.

Thanks

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Nov 2019, 07:49 AM
Hello, Zygmunt,    

RadPdfViewer in the Telerik UI for WinForms suite is purposed to display pdf files. It is not intended to offer drawing functionality over the pdf content. However, RadPdfViewer exposes two events indicating when a page starts and completes a rendering operation. You have access to the Graphics object in the event handler, so you can perform any rendering you need. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/pdfviewer/how-to/handle-rendering-events 

As an exception, I have prepared a basic example demonstrating a sample approach how you can draw a rectangle once the document is loaded. The attached gif file illustrates the achieved result. It is just a sample approach and it may not cover all possible cases. 
        public RadForm1()
        {
            InitializeComponent();

            this.radPdfViewer1.LoadDocument(@"..\..\Book1.pdf");
            this.radPdfViewer1.ViewerMode = FixedDocumentViewerMode.None;
            this.radPdfViewer1.PageElementCreating += radPdfViewer1_PageElementCreating; 
        }

        List<Rectangle> rectangles = new List<Rectangle>();

        private void radPdfViewer1_PageElementCreating(object sender, RadFixedPageElementEventArgs e)
        {
            e.PageElement.ElementPainted += PageElement_ElementPainted;  
            e.PageElement.MouseMove += PageElement_MouseMove;
            e.PageElement.MouseUp += PageElement_MouseUp;
        }

        private void PageElement_MouseUp(object sender, MouseEventArgs e)
        {
            RadFixedPageElement page = (sender as RadFixedPageElement);
            Rectangle pageRect = page.ControlBoundingRectangle;
            Rectangle rectInitial = new Rectangle(downLocation, new Size(Math.Abs(location.X - downLocation.X), Math.Abs(location.Y - downLocation.Y)));
            RectangleF rect = new RectangleF(new PointF(-pageRect.X + rectInitial.Location.X, -pageRect.Y + rectInitial.Location.Y), rectInitial.Size);
            
            rectangles.Add(Rectangle.Ceiling(rect));

            location = Point.Empty;
            downLocation = Point.Empty;

            this.radPdfViewer1.Invalidate(); 
        }

        Point location = Point.Empty;
        Point downLocation = Point.Empty;
        Graphics g = null;
        
        private void PageElement_MouseMove(object sender, MouseEventArgs e)
        {
            if (Control.MouseButtons == System.Windows.Forms.MouseButtons.Left)
            {
                if (downLocation == Point.Empty)
                {
                    downLocation = e.Location;
                }
                location = e.Location;
                this.radPdfViewer1.Invalidate(); 
            }
        }
       
        private void PageElement_ElementPainted(object sender, PaintEventArgs e)
        { 
            RadFixedPageElement page = (sender as RadFixedPageElement);
            Rectangle rect = page.ControlBoundingRectangle; 
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 255, 0, 0));

            if (location != Point.Empty && downLocation != Point.Empty)
            {
                e.Graphics.ResetTransform(); 
                Rectangle r = new Rectangle(downLocation, new Size(Math.Abs(location.X - downLocation.X),
                    Math.Abs(location.Y - downLocation.Y)));
                e.Graphics.FillRectangle(semiTransBrush, r);
            }
            
            foreach (Rectangle r in rectangles)
            {
                e.Graphics.ResetTransform();  
                e.Graphics.FillRectangle(semiTransBrush, new Rectangle(new Point(rect.X + r.Location.X, rect.Y + r.Location.Y), r.Size)); 
            }
        }

Note that implementing such custom functionality goes beyond the scope of the support service. Feel free to customize and extend the sample code in a way that meets your requirements best.

I hope this information helps.

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PdfViewer and PdfViewerNavigator
Asked by
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or