Back to Devexpress

SubDocument.Delete(DocumentRange) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-dot-delete-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentrange-x29.md

latest7.9 KB
Original Source

SubDocument.Delete(DocumentRange) Method

Removes content from the specified range.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
void Delete(
    DocumentRange range
)
vb
Sub Delete(
    range As DocumentRange
)

Parameters

NameTypeDescription
rangeDocumentRange

A range with the content to delete.

|

Remarks

Use the Delete method to remove document content (text, images, etc.) located in the specified range.

The code sample below removes specific words from a document:

csharp
using DevExpress.XtraRichEdit.API.Native;

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("FirstLook.docx");
    RemoveSpecificWords(wordProcessor.Document);
}

static void RemoveSpecificWords(Document document)
{
    string word = "Ounce";
    DocumentRange[] found = document.FindAll
      (word, SearchOptions.WholeWord);
    foreach (DocumentRange range in found)
    {
        document.Delete(range);
    }
}
vb
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor As New RichEditDocumentServer()
    wordProcessor.LoadDocument("FirstLook.docx")
    RemoveSpecificWords(wordProcessor.Document)
End Using

static void RemoveSpecificWords(Document document)
    Dim word As String = "Ounce"
    Dim found() As DocumentRange = document.FindAll
      (word, SearchOptions.WholeWord)
    For Each range As DocumentRange In found
      document.Delete(range)
    Next range
End Sub

Delete Table Cell Content

If the DocumentRange passed as the method parameter contains only table cells without the text surrounding the table, the Delete method clears the only the cells content. If you want do delete a table row or other table elements, utilize the following methods instead:

Delete Nested Fields

To delete a nested field, set the parent field’s ShowCodes property to true. Use the Field.Parent property to access the parent field.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Delete(DocumentRange) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

word-processing-merge-documents-with-different-headers-and-footers/CS/Helpers/SectionsMerger.cs#L31

csharp
SubDocument target = targetSection.BeginUpdateHeader(headerFooterType);
target.Delete(target.Range);
target.InsertDocumentContent(target.Range.Start, source.Range, InsertOptions.KeepSourceFormatting);

word-document-api-examples/CS/CodeExamples/BasicActions.cs#L64

csharp
// Delete the first empty paragraph.
tempWordProcessor.Document.Delete(tempWordProcessor.Document.Paragraphs.First().Range);
// Save the document page as an RTF file.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Notes.cs#L53

csharp
//Clear the range:
footnote.Delete(noteTextRange);

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/NotesActions.cs#L53

csharp
//Clear the range:
footnote.Delete(noteTextRange);

winforms-richeditcontrol-common-api/CS/RichEditAPISample/CodeExamples/RichEditControlActions.cs#L482

csharp
DocumentRange rangeToRemove = doc.CreateRange(range.Start, selLength);
doc.Delete(rangeToRemove);
range.EndUpdateDocument(doc);

word-processing-merge-documents-with-different-headers-and-footers/VB/Helpers/SectionsMerger.vb#L30

vb
Dim target As SubDocument = targetSection.BeginUpdateHeader(headerFooterType)
target.Delete(target.Range)
target.InsertDocumentContent(target.Range.Start, source.Range, InsertOptions.KeepSourceFormatting)

word-document-api-examples/VB/CodeExamples/BasicActions.vb#L61

vb
' Delete the first empty paragraph.
tempWordProcessor.Document.Delete(System.Linq.Enumerable.First(Of DevExpress.XtraRichEdit.API.Native.Paragraph)(tempWordProcessor.Document.Paragraphs).Range)
' Save the document page as an RTF file.

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Notes.vb#L40

vb
'Clear the range:
footnote.Delete(noteTextRange)
'Append a new text:

wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/NotesActions.vb#L47

vb
'Clear the range:
footnote.Delete(noteTextRange)

winforms-richeditcontrol-common-api/VB/RichEditAPISample/CodeExamples/RichEditControlActions.vb#L500

vb
Dim rangeToRemove As DocumentRange = doc.CreateRange(range.Start, selLength)
doc.Delete(rangeToRemove)
range.EndUpdateDocument(doc)

See Also

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace