We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the x and y coordinates with respect to WPF PdfViewer control?

Platform: WPF |
Control: PdfViewer |
Published Date: August 7, 2018 |
Last Revised Date: July 28, 2021

You can get the coordinates of mouse pointer with respect to WPF PDFViewer control by using Mouse_Up event.

You can refer to the following steps

Step 1:  Add the following code in the MainWindow.xaml.

 XAML

<Window x:Class="FindCoordinatesWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:syncfusion="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <syncfusion:PdfViewerControl x:Name="PdfViewer" Grid.Column="0"/>
        <GroupBox VerticalAlignment="Center" Grid.Column="1" Margin="20">
            <GroupBox.Header>
                <TextBlock Text="Coordinates" FontSize="16" FontWeight="Bold"></TextBlock>
            </GroupBox.Header>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal" Grid.Row="0">
                    <Label Content="X : " FontSize="16" Margin="20,10,0,10"/>
                    <Label x:Name="xLabel" FontSize="16" Margin="0,10,0,10"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Grid.Row="1">
                    <Label Content="Y : " FontSize="16"  Margin="20,10,0,10"/>
                    <Label x:Name="yLabel" FontSize="16" Margin="0,10,0,10"/>
                </StackPanel>
            </Grid>
        </GroupBox>
    </Grid>
</Window>

Step 2: Add the below code snippet in the MainWindow.xaml.cs

C#

using System.Windows;
using System.Windows.Input;
 
namespace FindCoordinatesWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PdfViewer.Load("../../Data/GIS Succinctly.pdf");
            PdfViewer.MouseUp += PdfViewer_MouseUp;
        }
 
        private void PdfViewer_MouseUp(object sender, MouseButtonEventArgs e)
        {
            System.Drawing.Point position = new System.Drawing.Point
            {
                X = (int)e.GetPosition(PdfViewer).X,
                Y = (int)e.GetPosition(PdfViewer).Y
            };
            //Displays the x and y coordinates
            xLabel.Content = position.X;
            yLabel.Content = position.Y;
        }
    }
}

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile