Location discrepancy with "same mouse coordinates "in PDFViewer control

HI,

In the following code I don't quite understand why the mouse click coordinates on the PDF page generate a difference on the screen as seen in the attached image.

EVENT PageClicked:

 private void pdfViewer_PageClicked(object sender, Syncfusion.Windows.PdfViewer.PageClickedEventArgs e)

 {

     Console.WriteLine("Dentro de PageClick");



     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     //startPoint = e.Position;

     Console.WriteLine("posicion Pageclick" + e.Position);


     PdfUnitConvertor convertor = new PdfUnitConvertor();

     Mouse.OverrideCursor = Cursors.Arrow;

     Point clientPoint = e.Position;

     //Retrieve the page number that corresponds to the client point

     int pageNumber = pdfViewer.CurrentPageIndex;


     //Retrieve the page point

     Point pagePoint = pdfViewer.ConvertClientPointToPagePoint(clientPoint, pageNumber);

     double x = pagePoint.X;

     double y = pagePoint.Y;

     x = convertor.ConvertToPixels((float)e.Position.X, PdfGraphicsUnit.Pixel);

     y = convertor.ConvertToPixels((float)e.Position.Y, PdfGraphicsUnit.Pixel);


     Point position = new Point(x, y);


     //pdfViewer.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.None;



     var bitmapImage = new BitmapImage(new Uri("../../Data/Portu1.jpg", UriKind.RelativeOrAbsolute));

     bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

     var image = new System.Windows.Controls.Image() { Source = bitmapImage };

     var pdfStamp = new PdfStampAnnotation(image);


     if (pdfViewer.ZoomPercentage != 100)

     {

         float zoomFactor = (float)pdfViewer.ZoomPercentage / 100.0f;

         var x1 = position.X / zoomFactor;

         var y1 = position.Y / zoomFactor;

         position = new Point(x1, y1);

     }

     //Enter the required size of the stamp.

     System.Drawing.Size stampSize = new System.Drawing.Size(200, 100);

     pdfViewer.AddStamp(pdfStamp, pageNumber, position, stampSize);

     //-------------------------------------------------------------------------

     System.Drawing.RectangleF bounds = new System.Drawing.RectangleF((float)position.X, (float)position.Y, 200, 100);

     pdfViewer.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.FreeText;

     PdfFreeTextAnnotation freeTextAnnotation = new PdfFreeTextAnnotation(bounds)

     {

         Text = "Texto de ejemplo",

         Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12),

         BorderColor = System.Drawing.Color.Blue,


         Color = System.Drawing.Color.AliceBlue

     };

     pdfViewer.LoadedDocument.Pages[0].Annotations.Add(freeTextAnnotation);


     //----------------------------------------------------------------------------------


 }



Best Regards;


Jesús



Attachment: 20241110165106_7812b696.rar

3 Replies

RB Rajavignesh BalaSankar Syncfusion Team November 11, 2024 01:19 PM UTC

Hi Jesús,


The position retrieved from the page clicked event of the WPF PdfViewer is page point. So there is no need to convert this again to the page point. This page point returned from the PdfViewer will be in pixels. We need to convert this pixel to point after removing the zoom percentage from this point. Please refer the below code snippet for taking the bounds value for annotation from the page clicked event,

 // Event triggered when the page is clicked

 private void PdfViewer_PageClicked(object sender, PageClickedEventArgs args)

 {

     // Get the position of the mouse click and convert it to PdfGraphicsUnit.Point

     PdfUnitConvertor convertor = new PdfUnitConvertor();

     RectangleF bounds = new RectangleF((float)args.Position.X, (float)args.Position.Y, 100, 20);

     float zoomFactor = (float)pdfViewer.ZoomPercentage / 100;

     bounds = new RectangleF(bounds.X / zoomFactor, bounds.Y / zoomFactor, bounds.Width, bounds.Height);

     //The above conversion is not needed for adding annotation to the PdfViewer API (AddStamp)

     bounds = convertor.ConvertFromPixels(bounds, PdfGraphicsUnit.Point);

}


NOTE: For using the `AddStamp` API of the PDFViewer, there is no need for converting the position from pixel to point. But for adding the annotation in Loaded document, it is mandatory to convert pixel values to point.


Regards,

Raja Vignesh.



JE Jesús November 11, 2024 02:42 PM UTC

Hi ,

Perfect, it work

Thank you very much for help

Best Regards


Jesús




RB Rajavignesh BalaSankar Syncfusion Team November 12, 2024 02:00 PM UTC

You are welcome:) Please get back to us if you need any other assistance. 


Loader.
Up arrow icon