ComponentOne Document Library for WinForms
Text Search
PdfDocumentSource for WinForms > Features > Text Search

PDFDocumentSource allows you to implement text search in a PDF file by matching the search criteria and examining all the words stored in the file through C1TextSearchManager class, member of C1.Win.C1Document namespace. 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 file and the list of matches as search results.

To search text programmatically

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

Step 1: Setting up the application

  1. Add C1PdfDocumentSource, 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 file

  1. Switch to the code view and add the following namespace.
    Imports C1.Win.C1Document
    
    using C1.Win.C1Document;
    
  2. 2.Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf from the product sample.
  3. Add the following code to create an instance of C1TextSearchManager class and declare a variable, loadedFile, of string type.
    'C1TextSearchManager instance used by the search.
    Dim tsm As C1TextSearchManager
    
    'File name of the currently loaded document.
    Dim loadedFile As String = Nothing
    
    // 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.
    'Use sample file
    tbFile.Text = Path.GetFullPath("..\..\DefaultDocument.pdf")
    
    'Create and initialize the C1TextSearchManager
    tsm = New C1TextSearchManager(C1PdfDocumentSource1)
    AddHandler tsm.FoundPositionsChanged, AddressOf tsm_FoundPositionsChanged
    
    // 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 file.
    'Allow the user to choose a PDF file to search.
    If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
        tbFile.Text = OpenFileDialog1.FileName
    End If
    
    // 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.
    'Load the specified PDF file into c1PdfDocumentSource1, do the search:
    Try
        C1PdfDocumentSource1.LoadFromFile(tbFile.Text)
        loadedFile = tbFile.Text
    Catch ex As Exception
        MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.[Error])
        Return
    End Try
    
    'Clear the previously found positions, if any:
    ListView1.Items.Clear()
    
    'Init C1FindTextParams with values provided by the user:
    Dim ftp As New C1FindTextParams(tbFind.Text, True, False)
    
    'Do the search (FindStartAsync is also available):
    tsm.FindStart(0, True, ftp)
    
    // 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.
    '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 Sub tsm_FoundPositionsChanged(sender As Object, e As EventArgs)
        Dim n As Integer = tsm.FoundPositions.Count
        For i = ListView1.Items.Count To n - 1
            Dim fp As C1FoundPosition = tsm.FoundPositions(i)
            Dim Bounds = fp.GetBounds()
            Dim lvi = New ListViewItem(New String() {(i + 1).ToString(),
                      fp.GetPage().PageNo.ToString(),
                      String.Format("{0}, {1}, {2}, {3}",
                      CInt(Math.Round(Bounds.Left)),
                      CInt(Math.Round(Bounds.Top)),
                      CInt(Math.Round(Bounds.Width)),
                      CInt(Math.Round(Bounds.Height))),
                      fp.PositionInNearText.ToString(),
                      fp.NearText
                      })
            ListView1.Items.Add(lvi)
        Next
    End Sub
    
    // 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.
See Also