Articles in this section
Category / Section

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

1 min read

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.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied