Skip to main content

How to: Find Text Using a Keyboard Shortcut

  • 2 minutes to read

This example shows how to execute the Find Next action by pressing the F3 shortcut, after an end-user has specified text search options in the Find Text dialog.

To do this, handle the PdfViewer.KeyDown event. If the F3 key is pressed, call the overloaded PdfViewer.FindText method and pass the text to search and search parameters obtained from the Find Text dialog to this method.

The text search settings (e.g, search text, whole words, case sensitive) applied by an end-user in the Find Text dialog can be accessed using the PdfViewer.FindDialogOptions property.

using DevExpress.Pdf;
using DevExpress.XtraPdfViewer;
using System;
using System.Windows.Forms;

namespace FindTextUsingShortcut {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            pdfViewer1.LoadDocument(@"..\..\Report.pdf");
        }

        private void pdfViewer1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyData == Keys.F3) {
                PdfTextSearchParameters parameters = new PdfTextSearchParameters();
                PdfFindDialogOptions options = pdfViewer1.FindDialogOptions;
                parameters.CaseSensitive = options.CaseSensitive;
                parameters.WholeWords = options.WholeWords;
                pdfViewer1.FindText(options.Text, parameters);
            }
        }
    }
}