GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / SearchText Method / SearchText(String,Int32,Boolean,Boolean,Single,Single,Single,Single) Method
The text expression to search for.
The occurrence (rank) of the searched expression on the current page. Set the occurrence to 1 if you are searching for the first occurrence, set it to 2 for the second etc.

Rank equal to 0 is not accepted, it will always be converted to 1. Please note that the occurrence (rank) is always related to the current page.

Set this parameter to true if you want to apply case-sensitive search, otherwise set it to false.
Set this parameter to true if you want to search for the whole words only, otherwise set it to false.
Output parameter. If the searched expression has been found, this is the horizontal (X) coordinate of the top left point of its surrounding bounding box, in inches.
Output parameter. If the searched expression has been found, this is the vertical (Y) coordinate of the top left point of its surrounding bounding box, in inches.
Output parameter. If the searched expression has been found, this is the width of the bounding box surrounding the expression, in inches.
Output parameter. If the searched expression has been found, this is the height of the bounding box surrounding the expression, in inches.
Example





In This Topic
SearchText(String,Int32,Boolean,Boolean,Single,Single,Single,Single) Method
In This Topic
Searches for an occurrence of a given text expression within the current page of the loaded PDF document according to the parameters you have specified. This method returns the bounding box (rectangle) surrounding the searched expression defined by its top left coordinates and by its width and height in inches, if the expression has been found.

This method uses the InvariantCulture comparison when searching. It means, that characters are comparing using culture-sensitive sort rules and the invariant culture, in other words this method respects accents when searching.

Syntax
'Declaration
 
Public Overloads Function SearchText( _
   ByVal Text As String, _
   ByVal Occurrence As Integer, _
   ByVal CaseSensitive As Boolean, _
   ByVal WholeWords As Boolean, _
   ByRef Left As Single, _
   ByRef Top As Single, _
   ByRef Width As Single, _
   ByRef Height As Single _
) As Boolean
public bool SearchText( 
   string Text,
   int Occurrence,
   bool CaseSensitive,
   bool WholeWords,
   ref float Left,
   ref float Top,
   ref float Width,
   ref float Height
)
public function SearchText( 
    Text: String;
    Occurrence: Integer;
    CaseSensitive: Boolean;
    WholeWords: Boolean;
   var  Left: Single;
   var  Top: Single;
   var  Width: Single;
   var  Height: Single
): Boolean; 
public function SearchText( 
   Text : String,
   Occurrence : int,
   CaseSensitive : boolean,
   WholeWords : boolean,
   Left : float,
   Top : float,
   Width : float,
   Height : float
) : boolean;
public: bool SearchText( 
   string* Text,
   int Occurrence,
   bool CaseSensitive,
   bool WholeWords,
   ref float Left,
   ref float Top,
   ref float Width,
   ref float Height
) 
public:
bool SearchText( 
   String^ Text,
   int Occurrence,
   bool CaseSensitive,
   bool WholeWords,
   float% Left,
   float% Top,
   float% Width,
   float% Height
) 

Parameters

Text
The text expression to search for.
Occurrence
The occurrence (rank) of the searched expression on the current page. Set the occurrence to 1 if you are searching for the first occurrence, set it to 2 for the second etc.

Rank equal to 0 is not accepted, it will always be converted to 1. Please note that the occurrence (rank) is always related to the current page.

CaseSensitive
Set this parameter to true if you want to apply case-sensitive search, otherwise set it to false.
WholeWords
Set this parameter to true if you want to search for the whole words only, otherwise set it to false.
Left
Output parameter. If the searched expression has been found, this is the horizontal (X) coordinate of the top left point of its surrounding bounding box, in inches.
Top
Output parameter. If the searched expression has been found, this is the vertical (Y) coordinate of the top left point of its surrounding bounding box, in inches.
Width
Output parameter. If the searched expression has been found, this is the width of the bounding box surrounding the expression, in inches.
Height
Output parameter. If the searched expression has been found, this is the height of the bounding box surrounding the expression, in inches.

Return Value

true if the given text expression has been found on the current page according to the specified parameters, otherwise false. The GetStat method can be subsequently used to determine if this method has been successful.
Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GetStat method to identify the specific reason for the method's failure, if any.

Be aware that this method respects accents when searching. You can use the overloaded SearchText(String,Int32,Boolean,Boolean,Boolean,Single,Single,Single,Single) method for searching not respecting accents.

Example
How to search for all occurrences of a specified text expression across the whole PDF document.
Dim caption As String = "Example: SearchText"
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.LoadFromFile("test.pdf", False)
If status = GdPictureStatus.OK Then
    Dim text_file As New System.IO.StreamWriter("search_text.txt")
    Dim pageCount As Integer = gdpicturePDF.GetPageCount()
    status = gdpicturePDF.GetStat()
    If status = GdPictureStatus.OK Then
        Dim textToFind As String = "text"
        Dim message As String = Nothing
        Dim occurrence As Integer = 1
        Dim found As Boolean = False
        Dim posLeft As Single = 0, posTop As Single = 0, posWidth As Single = 0, posHeight As Single = 0
        For i As Integer = 1 To pageCount
            status = gdpicturePDF.SelectPage(i)
            If status = GdPictureStatus.OK Then
                message = "Page: " + i.ToString() + " Status: " + status.ToString()
                text_file.WriteLine(message)
                'The occurrence of the searched text is related to the current page.
                occurrence = 1
                Do
                    found = gdpicturePDF.SearchText(textToFind, occurrence, False, False, posLeft, posTop, posWidth, posHeight)
                    status = gdpicturePDF.GetStat()
                    If status = GdPictureStatus.OK AndAlso found Then
                        message = "Occurrence: " + occurrence.ToString() + " Position: (" + posLeft.ToString() + ";" + posTop.ToString() + ";" + posWidth.ToString() + ";" + posHeight.ToString() + ")"
                        text_file.WriteLine(message)
                    End If
                    occurrence += 1
                Loop While found = True
            Else
                MessageBox.Show("The SelectPage() method has failed with the status: " + status.ToString(), caption)
            End If
        Next
    Else
        MessageBox.Show("The GetPageCount() method has failed with the status: " + status.ToString(), caption)
    End If
    text_file.Close()
Else
    MessageBox.Show("The file can't be loaded.", caption)
End If
MessageBox.Show("Searching finished.", caption)
gdpicturePDF.Dispose()
string caption = "Example: SearchText";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.LoadFromFile("test.pdf", false);
if (status == GdPictureStatus.OK)
{
    System.IO.StreamWriter text_file = new System.IO.StreamWriter("search_text.txt");
    int pageCount = gdpicturePDF.GetPageCount();
    status = gdpicturePDF.GetStat();
    if (status == GdPictureStatus.OK)
    {
        string textToFind = "text";
        string message = null;
        int occurrence = 1;
        bool found = false;
        float posLeft = 0, posTop = 0, posWidth = 0, posHeight = 0;
        for (int i = 1; i <= pageCount; i++)
        {
            status = gdpicturePDF.SelectPage(i);
            if (status == GdPictureStatus.OK)
            {
                message = "Page: " + i.ToString() + " Status: " + status.ToString();
                text_file.WriteLine(message);
                //The occurrence of the searched text is related to the current page.
                occurrence = 1;
                do
                {
                    found = gdpicturePDF.SearchText(textToFind, occurrence, false, false, ref posLeft, ref posTop, ref posWidth, ref posHeight);
                    status = gdpicturePDF.GetStat();
                    if (status == GdPictureStatus.OK && found)
                    {
                        message = "Occurrence: " + occurrence.ToString() + " Position: (" + posLeft.ToString() + ";" + posTop.ToString() + ";" + posWidth.ToString() + ";" + posHeight.ToString() + ")";
                        text_file.WriteLine(message);
                    }
                    occurrence++;
                } while (found == true);
            }
            else
            {
                MessageBox.Show("The SelectPage() method has failed with the status: " + status.ToString(), caption);
            }
        }
    }
    else
    {
        MessageBox.Show("The GetPageCount() method has failed with the status: " + status.ToString(), caption);
    }
    text_file.Close();
}
else
{
    MessageBox.Show("The file can't be loaded.", caption);
}
MessageBox.Show("Searching finished.", caption);
gdpicturePDF.Dispose();
See Also