Click or drag to resize

ContentRangeDelete Method

Deletes the document content specified with the current ContentRange.

Namespace: SautinSoft.Document
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.4.24
Syntax
public void Delete()
Example

See Developer Guide: Open a document and delete some content

Open a document and delete some content using C#
using System.IO;
using System.Linq;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {

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

            DeleteContent();
        }

        /// <summary>
        /// Open a document and delete some content.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/delete-content-net-csharp-vb.php
        /// </remarks>
        public static void DeleteContent()
        {
            string loadPath = @"..\..\..\example.docx";
            string savePath = "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)
                        cr.Delete();
                }
            }
            dc.Save(savePath);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
        }
    }
}
Open a document and delete some content using VB.Net
Option Infer On
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document

Module Sample
    Sub Main()
        DeleteContent()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Open a document and delete some content.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/delete-content-net-csharp-vb.php
    ''' </remarks>
    Sub DeleteContent()
        Dim loadPath As String = "..\..\..\example.docx"
        Dim savePath As String = "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
                    cr.Delete()
                Next cr
            End If
        Next par
        dc.Save(savePath)
        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
See Also