Как найти и заменить любой текст в существующем документе DOCX с помощью C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Загрузите документ DOCX.
  3. Найдите и замените определенный текст.
  4. Сохраните документ обратно.

Здесь мы покажем Вам, как использовать методы поиска и замены.
Используя регулярные выражения, мы найдем - "Bean" (bean, BEAN, bEan, etc) и заменим на - "Joker".


Полный код

using System;
using System.IO;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

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

            FindAndReplace();
        }
        /// <summary>
        /// Find and replace a specific text in an existing DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-and-replace-text-in-docx-document-net-csharp-vb.php
        /// </remarks>
        public static void FindAndReplace()
        {
            // Path to the loadable document.
            string loadPath = @"..\..\..\example.docx";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Find "Bean" and Replace everywhere on "Joker"
            Regex regex = new Regex(@"Bean", RegexOptions.IgnoreCase);

            // Start:

            // Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            }

            // End:

            // The code above finds and replaces the content in the whole document.
            // Let us say, you want to replace a text inside shape blocks only:

            // 1. Comment the code above from the line "Start" to the "End".
            // 2. Uncomment this code:
            //foreach (Shape shp in dc.GetChildElements(true, ElementType.Shape).Reverse())
            //{
            //    foreach (ContentRange item in shp.Content.Find(regex).Reverse())
            //    {
            //        item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            //    }
            //}

            // Save the document as DOCX format.
            string savePath = Path.ChangeExtension(loadPath, ".replaced.docx");
            dc.Save(savePath, SaveOptions.DocxDefault);

            // Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions

Namespace Sample
    Friend Class Sample
        Shared Sub Main(ByVal args() As String)
            FindAndReplace()
        End Sub
        ''' Get your free 30-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Find and replace a specific text in an existing DOCX document.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-and-replace-text-in-docx-document-net-csharp-vb.php
        ''' </remarks>
        Public Shared Sub FindAndReplace()
            ' Path to the loadable document.
            Dim loadPath As String = "..\..\..\example.docx"

            Dim dc As DocumentCore = DocumentCore.Load(loadPath)

            ' Find "Bean" and Replace everywhere on "Joker"
            Dim regex As New Regex("Bean", RegexOptions.IgnoreCase)

            ' Start:

            ' Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            For Each item As ContentRange In dc.Content.Find(regex).Reverse()
                item.Replace("Joker", New CharacterFormat() With {
                .BackgroundColor = Color.Yellow,
                .FontName = "Arial",
                .Size = 16.0
                   })
            Next item

            ' End:

            ' The code above finds and replaces the content in the whole document.
            ' Let us say, you want to replace a text inside shape blocks only:

            ' 1. Comment the code above from the line "Start" to the "End".
            ' 2. Uncomment this code:
            'For Each shp As Shape In dc.GetChildElements(True, ElementType.Shape).Reverse()
            'For Each item As ContentRange In shp.Content.Find(regex).Reverse()
            'item.Replace("Joker", New CharacterFormat() With {
            '.BackgroundColor = Color.Yellow,
            '.FontName = "Arial",
            '.Size = 16.0
            '       })
            'Next item
            'Next shp

            ' Save the document as DOCX format.
            Dim savePath As String = Path.ChangeExtension(loadPath, ".replaced.docx")
            dc.Save(savePath, SaveOptions.DocxDefault)

            ' Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(loadPath) With {.UseShellExecute = True})
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(savePath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


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



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

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