FindText(Regex,Int32,Boolean) Метод (TextRegion)
В этом разделе
Находит текст с использованием регулярного выражения в этой текстовой области.
Синтаксис
Parameters
- regex
- Регулярное выражение для поиска.
- startIndex
- Начальная позиция в текстовой области отсчитывается от нуля.
- searchUp
- Значение, указывающее, нужно ли искать текст от текущей позиции в текстовой области до начала текстовой области.
Return Value
TextRegion, если текст найден; в противном случае
null.
Пример
Вот C#/VB.NET код, который демонстрирует, как искать текст, определенный с помощью регулярного выражения, на PDF странице.
''' <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();
}
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также