VintaSoft Imaging .NET SDK 14.0: Документация для .NET разработчика
Vintasoft.Imaging.Text Namespace / TextRegion Class / FindText Methods / FindText(String,Boolean,Int32,Boolean) Method
Синтаксис Example Требования Смотрите также
В этом разделе
    FindText(String,Boolean,Int32,Boolean) Метод (TextRegion)
    В этом разделе
    Находит текст в этой текстовой области.
    Синтаксис
    'Declaration
    
    Public Overloads Function FindText( _
    ByVal text
    Текст для поиска.
    As System.String, _
    ByVal ignoreCase
    Значение, указывающее, следует ли игнорировать чувствительность к регистру.
    As Boolean, _
    ByRef startIndex
    Начальная позиция в текстовой области отсчитывается от нуля.
    As System.Int32, _
    ByVal searchUp
    Значение, указывающее, нужно ли искать текст от текущей позиции в текстовой области до начала текстовой области.
    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
    Текст для поиска.
    ignoreCase
    Значение, указывающее, следует ли игнорировать чувствительность к регистру.
    startIndex
    Начальная позиция в текстовой области отсчитывается от нуля.
    searchUp
    Значение, указывающее, нужно ли искать текст от текущей позиции в текстовой области до начала текстовой области.

    Return Value

    TextRegion, если текст найден; в противном случае null.
    Пример

    Вот C#/VB.NET код, который демонстрирует, как искать текстовую строку на PDF странице.

    
    ''' <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();
    }
    
    

    Требования

    Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    Смотрите также