Delete Content

Complete code

using System.IO;
using System.Linq;
using SautinSoft.Document;

namespace Sample
    class Sample
        static void Main(string[] args)

        // Open a document and delete some content.
        public static void DeleteContent()
            string loadPath = @"..\..\example.docx";
            string savePath = Path.ChangeExtension(loadPath, ".result.docx");

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Remove the text "This" from all paragraphs in 1st section.
            foreach (Paragraph par in dc.Sections[0].GetChildElements(true, ElementType.Paragraph))
                var findText = par.Content.Find("This");

                if (findText != null)
                    foreach (ContentRange cr in findText)

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });


            Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
    End Sub

    ' Open a document and delete some content.
    Public Sub DeleteContent()
        Dim loadPath As String = "..\example.docx"
        Dim savePath As String = Path.ChangeExtension(loadPath, ".result.docx")

        Dim dc As DocumentCore = DocumentCore.Load(loadPath)

        ' Remove the text "This" from all paragraphs in 1st section.
        For Each par As Paragraph In dc.Sections(0).GetChildElements(True, ElementType.Paragraph)
            Dim findText = par.Content.Find("This")

            If findText IsNot Nothing Then
                For Each cr As ContentRange In findText
                Next cr
            End If
        Next par

        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 Module


© SautinSoft 2020