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

  • 2 minutes to read

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.

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;
        }
    }
}