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.
In this sample code, we use the FindStart method on the C1TextSearchManager to find the instances of searched text.
Name | Text | Width |
---|---|---|
chnum | # | 50 |
chpage | Page | 60 |
chbounds | Bounds | 100 |
chPosInNearText | Position In Near Text | 60 |
chNearText | Near Text | 350 |
C# |
Copy Code
|
---|---|
using C1.Win.C1Document;
|
C# |
Copy Code
|
---|---|
// C1TextSearchManager instance used by the search C1TextSearchManager tsm; // File name of the currently loaded document private string loadedFile = null; |
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; |
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; } |
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); } |
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); } } |