WpfPdfTextMarkupTool Class
Visual tool that allows to markup (highlight, strikeout, underline, squiggly underline) text using
PdfTextMarkupAnnotation on PDF page.
This C#/VB.NET code shows how to use PDF Text Markup tool to highlight text on PDF page.
''' <summary>
''' Sets the WpfPdfTextMarkupTool as current tool of specified image viewer.
''' </summary>
''' <param name="imageViewer">The image viewer.</param>
Public Shared Sub SetPdfTextMarkupTool(imageViewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer)
' create an instance of the PdfTextMarkupTool class
Dim textMarkupTool As New Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupTool()
' set text markup mode to "Highlight"
textMarkupTool.MarkupMode = Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupToolMode.Highlight
' set color of markup annotation to "Orange"
textMarkupTool.HighlightColor = System.Windows.Media.Colors.Orange
' subscribe to the MarkupAnnotationCreated event
AddHandler textMarkupTool.MarkupAnnotationCreated, AddressOf TextMarkupTool_MarkupAnnotationCreated
' get TextSelectionTool from image viewer
Dim textSelectionTool As Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool = GetTextSelectionTool(imageViewer)
' set the PdfTextMarkupTool+TextSelectionTool as visual tool of the image viewer
imageViewer.VisualTool = New Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCompositeVisualTool(textMarkupTool, textSelectionTool)
End Sub
''' <summary>
''' Returns the text selection tool for specified image viewer.
''' </summary>
''' <param name="imageViewer">The image viewer.</param>
''' <returns>The text selection tool.</returns>
Private Shared Function GetTextSelectionTool(imageViewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer) As Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool
' search a TextSelectionTool in visual tools of the image viewer
Dim textSelectionTool As Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool = Vintasoft.Imaging.Wpf.UI.VisualTools.WpfVisualTool.FindVisualTool(Of Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool)(imageViewer.VisualTool)
If textSelectionTool IsNot Nothing Then
Return textSelectionTool
End If
' create an instance of text selection tool
Return New Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool()
End Function
''' <summary>
''' Handles the MarkupAnnotationCreated event of the PdfTextMarkupTool visual tool.
''' </summary>
Private Shared Sub TextMarkupTool_MarkupAnnotationCreated(sender As Object, e As Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfMarkupAnnotationEventArgs)
' set user name as title of text markup annotation
e.MarkupAnnotation.Title = System.Environment.UserName
End Sub
/// <summary>
/// Sets the WpfPdfTextMarkupTool as current tool of specified image viewer.
/// </summary>
/// <param name="imageViewer">The image viewer.</param>
public static void SetPdfTextMarkupTool(Vintasoft.Imaging.Wpf.UI.WpfImageViewer imageViewer)
{
// create an instance of the PdfTextMarkupTool class
Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupTool textMarkupTool = new Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupTool();
// set text markup mode to "Highlight"
textMarkupTool.MarkupMode = Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupToolMode.Highlight;
// set color of markup annotation to "Orange"
textMarkupTool.HighlightColor = System.Windows.Media.Colors.Orange;
// subscribe to the MarkupAnnotationCreated event
textMarkupTool.MarkupAnnotationCreated += TextMarkupTool_MarkupAnnotationCreated;
// get TextSelectionTool from image viewer
Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool textSelectionTool = GetTextSelectionTool(imageViewer);
// set the PdfTextMarkupTool+TextSelectionTool as visual tool of the image viewer
imageViewer.VisualTool = new Vintasoft.Imaging.Wpf.UI.VisualTools.WpfCompositeVisualTool(textMarkupTool, textSelectionTool);
}
/// <summary>
/// Returns the text selection tool for specified image viewer.
/// </summary>
/// <param name="imageViewer">The image viewer.</param>
/// <returns>The text selection tool.</returns>
private static Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool GetTextSelectionTool(Vintasoft.Imaging.Wpf.UI.WpfImageViewer imageViewer)
{
// search a TextSelectionTool in visual tools of the image viewer
Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool textSelectionTool =
Vintasoft.Imaging.Wpf.UI.VisualTools.WpfVisualTool.FindVisualTool<Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool>(imageViewer.VisualTool);
if (textSelectionTool != null)
return textSelectionTool;
// create an instance of text selection tool
return new Vintasoft.Imaging.Wpf.UI.VisualTools.WpfTextSelectionTool();
}
/// <summary>
/// Handles the MarkupAnnotationCreated event of the PdfTextMarkupTool visual tool.
/// </summary>
private static void TextMarkupTool_MarkupAnnotationCreated(object sender, Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfMarkupAnnotationEventArgs e)
{
// set user name as title of text markup annotation
e.MarkupAnnotation.Title = System.Environment.UserName;
}
System.Object
 System.Windows.Threading.DispatcherObject
   System.Windows.DependencyObject
     System.Windows.Media.Visual
       System.Windows.UIElement
         System.Windows.FrameworkElement
           System.Windows.Controls.Control
             System.Windows.Controls.ContentControl
               Vintasoft.Imaging.Wpf.UI.VisualTools.WpfVisualTool
                 Vintasoft.Imaging.Pdf.Wpf.UI.WpfPdfTextMarkupTool
Target Platforms: .NET 11; .NET 10; .NET 9; .NET 8; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5