The DocumentViewer is a great control for showing XPS documents in a WPF application. You can zoom and search the document, but the control does not provide a public API for its Find functionality. Microsoft is going to fix this in a future release, until then you can use the following workaround.
The DocumentViewer uses a template which contains a ContentControl named 'PART_FindToolBarHost'.
You can access it by using the following code:
object toolbar = documentViewer.Template.FindName("PART_FindToolBarHost", documentViewer);
The Content property of the ContentControl is an instance of the MS.Internal.Documents.FindToolBar class, which is a control from PresentationUI.dll. But since this class is not public you cannot cast it and access its properties directly. Therefore you have to use reflection to set the desired search term.
The FindToolBar control contains a TextBox with the name 'FindTextBox'. You have to set its Text property:
// Set Text FieldInfo findTextBoxFieldInfo = findToolbar.GetType().GetField("FindTextBox", BindingFlags.NonPublic | BindingFlags.Instance); TextBox findTextBox = (TextBox)findTextBoxFieldInfo.GetValue(findToolbar); findTextBox.Text = "Lorem"; // Raise ClickEvent for Highlighting FieldInfo findNextButtonFieldInfo = findToolbar.GetType().GetField("FindNextButton", BindingFlags.NonPublic | BindingFlags.Instance); Button findNextButton = (Button)findNextButtonFieldInfo.GetValue(findToolbar); findNextButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
My demo project looks like this: