VintaSoft Imaging .NET SDK 14.0: Documentation for .NET developer
Vintasoft.Imaging.Text Namespace / TextRegion Class / FindText Methods / FindText(String,Boolean,Int32,Boolean) Method
Syntax Example Requirements SeeAlso
In This Topic
    FindText(String,Boolean,Int32,Boolean) Method (TextRegion)
    In This Topic
    Finds the text in this text region.
    Syntax
    'Declaration
    
    Public Overloads Function FindText( _
    ByVal text
    Text to search.
    As System.String, _
    ByVal ignoreCase
    A value indicating whether the case sensitivity should be ignored.
    As Boolean, _
    ByRef startIndex
    Zero-based start position in text region.
    As System.Int32, _
    ByVal searchUp
    A value indicating whether the text must be searched from current position in text region to the beginning of text region.
    As Boolean _
    ) As TextRegion
    public TextRegion FindText(
    System.String text,
    bool ignoreCase,
    ref System.Int32 startIndex,
    bool searchUp
    )
    public: TextRegion* FindText(
    System.String text,
    bool ignoreCase,
    ref System.Int32 startIndex,
    bool searchUp
    )
    public:
    TextRegion^ FindText(
    System.String text,
    bool ignoreCase,
    System.Int32% startIndex,
    bool searchUp
    )

    Parameters

    text
    Text to search.
    ignoreCase
    A value indicating whether the case sensitivity should be ignored.
    startIndex
    Zero-based start position in text region.
    searchUp
    A value indicating whether the text must be searched from current position in text region to the beginning of text region.

    Return Value

    TextRegion if text is found; otherwise, null.
    Example

    This C#/VB.NET code shows how to search a text string on PDF page.

    
    ''' <summary>
    ''' Outputs the information about specified word in content of PDF document.
    ''' </summary>
    ''' <param name="document">PDF document where word should be searched.</param>
    ''' <param name="word">Word to search.</param>
    Public Sub SearchWordInTextOfPdfDocument(document As Vintasoft.Imaging.Pdf.PdfDocument, word As String)
        System.Console.WriteLine("Searching the word in text of PDF document is started.")
    
        For i As Integer = 0 To document.Pages.Count - 1
            Dim textRegions As Vintasoft.Imaging.Text.TextRegion() = SimpleTextSearchOnPdfPage(document.Pages(i), word)
            If textRegions IsNot Nothing Then
                For j As Integer = 0 To textRegions.Length - 1
                    System.Console.WriteLine(String.Format("- Text={0}, Rectangle={1}", textRegions(j).TextContent, textRegions(j).Rectangle))
                Next
            End If
        Next
    
        System.Console.WriteLine("Searching the word in text of PDF document is finished.")
    End Sub
    
    ''' <summary>
    ''' Searches a text string on PDF page.
    ''' </summary>
    ''' <param name="page">PDF page where text should be searched.</param>
    ''' <param name="text">Text to search.</param>
    ''' <returns>An array of text regions on PDF page where text was found.</returns>
    Public Function SimpleTextSearchOnPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, text As String) As Vintasoft.Imaging.Text.TextRegion()
        Dim textRegions As New System.Collections.Generic.List(Of Vintasoft.Imaging.Text.TextRegion)()
    
        Dim textRegion As Vintasoft.Imaging.Text.TextRegion = Nothing
        Dim startIndex As Integer = 0
        Do
            ' search text
            textRegion = page.TextRegion.FindText(text, startIndex, False)
            ' if found text is not empty
            If textRegion IsNot Nothing Then
                ' add result
                textRegions.Add(textRegion)
                ' shitf start index
                startIndex += textRegion.TextContent.Length
            End If
        Loop While textRegion IsNot Nothing
    
        Return textRegions.ToArray()
    End Function
    
    
    
    /// <summary>
    /// Outputs the information about specified word in content of PDF document.
    /// </summary>
    /// <param name="document">PDF document where word should be searched.</param>
    /// <param name="word">Word to search.</param>
    public void SearchWordInTextOfPdfDocument(Vintasoft.Imaging.Pdf.PdfDocument document, string word)
    {
        System.Console.WriteLine("Searching the word in text of PDF document is started.");
    
        for (int i = 0; i < document.Pages.Count; i++)
        {
            Vintasoft.Imaging.Text.TextRegion[] textRegions = 
                SimpleTextSearchOnPdfPage(document.Pages[i], word);
            if (textRegions != null)
            {
                for (int j = 0; j < textRegions.Length; j++)
                {
                    System.Console.WriteLine(string.Format("- Text={0}, Rectangle={1}",
                        textRegions[j].TextContent,
                        textRegions[j].Rectangle));
                }
            }
        }
    
        System.Console.WriteLine("Searching the word in text of PDF document is finished.");
    }
    
    /// <summary>
    /// Searches a text string on PDF page.
    /// </summary>
    /// <param name="page">PDF page where text should be searched.</param>
    /// <param name="text">Text to search.</param>
    /// <returns>An array of text regions on PDF page where text was found.</returns>
    public Vintasoft.Imaging.Text.TextRegion[] SimpleTextSearchOnPdfPage(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page, string text)
    {
        System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion> textRegions = 
            new System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion>();
    
        Vintasoft.Imaging.Text.TextRegion textRegion = null;
        int startIndex = 0;
        do
        {
            // search text
            textRegion = page.TextRegion.FindText(text, ref startIndex, false);
            // if found text is not empty
            if (textRegion != null)
            {
                // add result
                textRegions.Add(textRegion);
                // shitf start index
                startIndex += textRegion.TextContent.Length;
            }
        } while (textRegion != null);
    
        return textRegions.ToArray();
    }
    
    

    Requirements

    Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also