VintaSoft Imaging .NET SDK 15.2: Documentation for .NET developer
Vintasoft.Imaging.Text Namespace / TextRegion Class / FindText Methods / FindText(Regex,Int32,Boolean) Method
Syntax Example Requirements SeeAlso
In This Topic
FindText(Regex,Int32,Boolean) Method (TextRegion)
In This Topic
Finds the text using regular expression in this text region.
Syntax
'Declaration

Public Overloads Function FindText( _
ByVal regex
Regular expression to search.
As System.Text.RegularExpressions.Regex, _
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

Parameters

regex
Regular expression to search.
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, defined with regular expression, on PDF page.


''' <summary>
''' Outputs the information about digits in content of PDF document.
''' </summary>
''' <param name="document">PDF document where digits should be searched.</param>
Public Sub SearchDigitsInTextOfPdfDocument(document As Vintasoft.Imaging.Pdf.PdfDocument)
    System.Console.WriteLine("Searching the digits in text of PDF document is started.")

    For i As Integer = 0 To document.Pages.Count - 1
        Dim textRegions As Vintasoft.Imaging.Text.TextRegion() = SimpleDigitsSearchOnPdfPage(document.Pages(i), New System.Text.RegularExpressions.Regex("\d+"))
        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 digits in text of PDF document is finished.")
End Sub

''' <summary>
''' Searches a text, defined with regular expression, on PDF page.
''' </summary>
''' <param name="page">PDF page where text should be searched.</param>
''' <param name="regex">Regular expression which defines the searching text.</param>
''' <returns>An array of text regions on PDF page where text was found.</returns>
Public Function SimpleDigitsSearchOnPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, regex As System.Text.RegularExpressions.Regex) As Vintasoft.Imaging.Text.TextRegion()
    Dim textRegions As New System.Collections.Generic.List(Of Vintasoft.Imaging.Text.TextRegion)()
    Dim textSearchEngine As Vintasoft.Imaging.Text.TextSearchEngine = Vintasoft.Imaging.Text.TextSearchEngine.Create(regex)

    Dim textRegion As Vintasoft.Imaging.Text.TextRegion = Nothing
    Dim startIndex As Integer = 0
    Do
        ' search text
        textRegion = page.TextRegion.FindText(textSearchEngine, 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 digits in content of PDF document.
/// </summary>
/// <param name="document">PDF document where digits should be searched.</param>
public void SearchDigitsInTextOfPdfDocument(Vintasoft.Imaging.Pdf.PdfDocument document)
{
    System.Console.WriteLine("Searching the digits in text of PDF document is started.");

    for (int i = 0; i < document.Pages.Count; i++)
    {
        Vintasoft.Imaging.Text.TextRegion[] textRegions = 
            SimpleDigitsSearchOnPdfPage(document.Pages[i], new System.Text.RegularExpressions.Regex(@"\d+"));
        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 digits in text of PDF document is finished.");
}

/// <summary>
/// Searches a text, defined with regular expression, on PDF page.
/// </summary>
/// <param name="page">PDF page where text should be searched.</param>
/// <param name="regex">Regular expression which defines the searching text.</param>
/// <returns>An array of text regions on PDF page where text was found.</returns>
public Vintasoft.Imaging.Text.TextRegion[] SimpleDigitsSearchOnPdfPage(
    Vintasoft.Imaging.Pdf.Tree.PdfPage page, 
    System.Text.RegularExpressions.Regex regex)
{
    System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion> textRegions = 
        new System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion>();
    Vintasoft.Imaging.Text.TextSearchEngine textSearchEngine = 
        Vintasoft.Imaging.Text.TextSearchEngine.Create(regex);

    Vintasoft.Imaging.Text.TextRegion textRegion = null;
    int startIndex = 0;
    do
    {
        // search text
        textRegion = page.TextRegion.FindText(textSearchEngine, 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: .NET 11; .NET 10; .NET 9; .NET 8; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also