Back to Devexpress

How to: Determine the Page Content Type under the Mouse Pointer

wpf-120439-controls-and-libraries-pdf-viewer-examples-interactivity-how-to-determine-the-page-content-type-under-the-mouse-pointer.md

latest5.3 KB
Original Source

How to: Determine the Page Content Type under the Mouse Pointer

  • Mar 27, 2019
  • 2 minutes to read

View Example: https://github.com/DevExpress-Examples/how-to-determine-the-page-content-type-under-the-mouse-pointer

This example illustrates how to use the PdfViewerControl.HitTest method to determine the type of the page content under the mouse pointer.

This method returns a PdfHitTestResult instance with information about the page content type (text, an image or annotation). You can get the page content type using the PdfHitTestResult.ContentType property and the page content selection status using the PdfHitTestResult.IsSelected property.

In this example, the retrieved information is shown on the Content Type ribbon page group.

Call the PdfViewerControl.HitTest method in the PdfViewerControl.MouseMove event handler to perform hit testing.

csharp
using DevExpress.Pdf;
using DevExpress.Xpf.PdfViewer;
using System.Windows;
using System.Windows.Input;

namespace DetermineContentType {

    public partial class MainWindow : Window {

        public MainWindow() {
            InitializeComponent();
            viewer.OpenDocument("..\\..\\demo.pdf");
        }

        private void viewer_MouseMove(object sender, MouseEventArgs e) {

            PdfHitTestResult result = viewer.HitTest(e.GetPosition(viewer));
            string contentTypeText = result.IsSelected ? "Selected " : "Unselected ";

            switch (result.ContentType) {
                case PdfDocumentContentType.Text:
                contentTypeText = contentTypeText + "Text";
                break;
                case PdfDocumentContentType.Image:
                contentTypeText = contentTypeText + " Image";
                break;
                case PdfDocumentContentType.Annotation:
                contentTypeText = contentTypeText + "Annotation";
                break;
                default:
                contentTypeText = "The content is empty";
                break;
            }
            barButtonItem.Content = contentTypeText;
        }
    }
}
vb
Imports DevExpress.Pdf
Imports DevExpress.Xpf.PdfViewer
Imports System.Windows
Imports System.Windows.Input

Namespace DetermineContentType

    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
            viewer.OpenDocument("..\..\demo.pdf")
        End Sub

        Private Sub viewer_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

            Dim result As PdfHitTestResult = viewer.HitTest(e.GetPosition(viewer))
            Dim contentTypeText As String = If(result.IsSelected, "Selected ", "Unselected ")

            Select Case result.ContentType
                Case PdfDocumentContentType.Text
                contentTypeText = contentTypeText & "Text"
                Case PdfDocumentContentType.Image
                contentTypeText = contentTypeText & " Image"
                Case PdfDocumentContentType.Annotation
                contentTypeText = contentTypeText & "Annotation"
                Case Else
                contentTypeText = "The content is empty"
            End Select
            barButtonItem.Content = contentTypeText
        End Sub
    End Class
End Namespace
xml
<Window
        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"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
        xmlns:local="clr-namespace:DetermineContentType"
        xmlns:dxpdf="http://schemas.devexpress.com/winfx/2008/xaml/pdf" x:Class="DetermineContentType.MainWindow"
        mc:Ignorable="d"
        Title="PDF Viewer" Height="350" Width="525">
    <Grid>
        <dxpdf:PdfViewerControl x:Name="viewer" MouseMove="viewer_MouseMove" >
            <dxpdf:PdfViewerControl.CommandProvider>
                <dxpdf:PdfCommandProvider>
                    <dxpdf:PdfCommandProvider.RibbonActions>
                        <dxr:InsertRibbonPageGroupAction PageName="{x:Static dxpdf:DefaultPdfBarManagerItemNames.MainRibbonPage}" Index="4">
                            <dxr:RibbonPageGroup Caption="Content Type">
                                <dxb:BarButtonItem x:Name="barButtonItem"/>
                            </dxr:RibbonPageGroup>
                        </dxr:InsertRibbonPageGroupAction>
                    </dxpdf:PdfCommandProvider.RibbonActions>
                </dxpdf:PdfCommandProvider>
            </dxpdf:PdfViewerControl.CommandProvider>
        </dxpdf:PdfViewerControl>
    </Grid>
</Window>