Document Library for WinForms | ComponentOne
PdfDocumentSource / Text Search
In This Topic
    Text Search
    In This Topic

    PDFDocumentSource allows you to implement text search in a PDF document by matching the search criteria and examining all the words stored in the file through C1TextSearchManager class. The class provides various methods, such as FindStart to find the first occurrence, FindNext to find the next occurrence, and FindPrevious to find the previous occurrence of the searched text. You can use C1FindTextParams(string text, bool wholeWord, bool matchCase) method to initialize a new instance of C1FindTextParams class, with the following parameters:

    The following image shows the word searched in a PDF document and the list of matches as search results.

    Snapshot of PdfDocSource Winforms app showing text search feature

    In this sample code, we use the FindStart method on the C1TextSearchManager to find the instances of searched text.

    Step 1: Setting up the application

    1. Add PdfDocumentSource, OpenFileDialog, ListView, two TextBox, and two Button controls to the Form.
    2. Right-click ListView and select Properties from the context menu.
    3. In the Properties window, click the ellipsis button next to the Columns property and add the following five columns in the ColumnHeader Collection Editor.
      Name Text Width
      chnum # 50
      chpage Page 60
      chbounds Bounds 100
      chPosInNearText Position In Near Text 60
      chNearText Near Text 350
    4. Click OK to close the ColumnHeader Collection Editor.
    5. Navigate to the View property and select Details from the drop-down list.

    Step 2: Browse and search text in a PDF document

    1. Switch to the code view and add the following namespace.
      C#
      Copy Code
      using C1.Win.C1Document;
      
    2. Add a PDF document to the project.
    3. Add the following code to create an instance of the C1TextSearchManager class and declare a variable, loadedFile, of string type.
      C#
      Copy Code
      // C1TextSearchManager instance used by the search       
      C1TextSearchManager tsm;
      // File name of the currently loaded document
      private string loadedFile = null;
      
    4. Add the following code below the InitializeComponent() method.
      C#
      Copy Code
      // Use sample file:
      tbFile.Text = Path.GetFullPath(@"..\..\DefaultDocument.pdf");
      // Create and initialize the C1TextSearchManager:
      tsm = new C1TextSearchManager(c1PdfDocumentSource1);
      tsm.FoundPositionsChanged += tsm_FoundPositionsChanged;
      
    5. Add the following code to the click event of btnFile to open the dialog box for browsing and opening a PDF document.
      C#
      Copy Code
      private void btnFile_Click(object sender, EventArgs e)
          {           
             // Allow the user to choose a PDF file to search.
             if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
             tbFile.Text = openFileDialog1.FileName;         
          }
      
    6. Add the following code to the click event of btnFind to start the text search.
      C#
      Copy Code
      private void btnFind_Click(object sender, EventArgs e)
          {
                  // Load the specified PDF file into c1PdfDocumentSource1, do the search:
                  try
                  {
                      c1PdfDocumentSource1.LoadFromFile(tbFile.Text);
                      loadedFile = tbFile.Text;
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK,
                                      MessageBoxIcon.Error);
                      return;
                  }
                  // Clear the previously found positions, if any:
                  lvFoundPositions.Items.Clear();
                  // Init C1FindTextParams with values provided by the user:
                  C1FindTextParams ftp = new C1FindTextParams(tbFind.Text, true, false);
                  // Do the search (FindStartAsync is also available):
                  tsm.FindStart(0, true, ftp);
           }
      
    7. Add the following event to update the list of found positions in the UI.
      C#
      Copy Code
      // Called when the FoundPositions collection on the C1TextSearchManager
      //  has changed (i.e. some new instances of the search text were found).
      // Use this to update the list of the found positions in the UI.
      private void tsm_FoundPositionsChanged(object sender, EventArgs e)
      {
          int n = tsm.FoundPositions.Count;
          for (int i = lvFoundPositions.Items.Count; i < n; i++)
          {
              C1FoundPosition fp = tsm.FoundPositions[i];
              var bounds = fp.GetBounds();
              ListViewItem lvi = new ListViewItem(new string[]
                  {
                  (i + 1).ToString(),
                  fp.GetPage().PageNo.ToString(),
                  string.Format("{0}, {1}, {2}, {3}",
                  (int)Math.Round(bounds.Left),
                  (int)Math.Round(bounds.Top),
                  (int)Math.Round(bounds.Width),
                  (int)Math.Round(bounds.Height)),
                  fp.PositionInNearText.ToString(),
                  fp.NearText,
                  });
              lvFoundPositions.Items.Add(lvi);
          }
      }
      

    Step 3: Build and run the project

    1. Press Ctrl+Shift+B to build the project.
    2. Press F5 to run the application.