Как найти страницы с указанным текстом с помощью C# и .NET


    В этом примере кода мы выясним, на каких страницах документа слово "Invoice" находится.

Полный код

using SautinSoft.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            FindPagesSpecifiedText();       
        }
        /// <summary>
        /// How to find out on which pages of the document the required word is located.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/from-customers-find-pages-with-specified-text-net-csharp-vb.php
        /// </remarks>
        public static void FindPagesSpecifiedText()
        {
            // The path for input files or directory.
            string inpFile = @"..\..\..\example.docx";

            // What we need to search.
            var searchText = "Invoice";
            int quantity = 0;
            
            // Load our documument in Document's engine.
            DocumentCore dc = DocumentCore.Load(inpFile);
            
            // Regex https://en.wikipedia.org/wiki/Regular_expression
            Regex regex = new Regex(searchText, RegexOptions.IgnoreCase);

            // Document paginator allows you to calculate of pages.
            DocumentPaginator dp = dc.GetPaginator();
            
            // We will search "searchText" on each pages (enumeration).
            for (int page = 0; page < dp.Pages.Count; page++)
            {
                foreach (ContentRange item in dp.Pages[page].Content.Find(regex).Reverse())
                {
                    Console.WriteLine($"I see the [{searchText}] on the page # {page + 1}");
                    quantity++;
                }
            }
            Console.WriteLine();
            Console.WriteLine($"I met [{searchText}] {quantity} times.  Please click on any button");
            Console.ReadKey();
        }
    }
}

Download

Option Infer On

Imports SautinSoft.Document
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Threading.Tasks
Imports System.Xml.Linq

Namespace Sample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			FindPagesSpecifiedText()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' How to find out on which pages of the document the required word is located.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/from-customers-find-pages-with-specified-text-net-csharp-vb.php
		''' </remarks>
		Public Shared Sub FindPagesSpecifiedText()
			' The path for input files or directory.
			Dim inpFile As String = "..\..\..\example.docx"

			' What we need to search.
			Dim searchText = "Invoice"
			Dim quantity As Integer = 0

			' Load our documument in Document's engine.
			Dim dc As DocumentCore = DocumentCore.Load(inpFile)

			' Regex https://en.wikipedia.org/wiki/Regular_expression
			Dim regex As New Regex(searchText, RegexOptions.IgnoreCase)

			' Document paginator allows you to calculate of pages.
			Dim dp As DocumentPaginator = dc.GetPaginator()

			' We will search "searchText" on each pages (enumeration).
			For page As Integer = 0 To dp.Pages.Count - 1
				For Each item As ContentRange In dp.Pages(page).Content.Find(regex).Reverse()
					Console.WriteLine($"I see the [{searchText}] on the page # {page + 1}")
					quantity += 1
				Next item
			Next page
			Console.WriteLine()
			Console.WriteLine($"I met [{searchText}] {quantity} times.  Please click on any button")
			Console.ReadKey()
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.