Back to Devexpress

FieldCollection.Create(DocumentRange) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-fieldcollection-dot-create-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentrange-x29.md

latest6.3 KB
Original Source

FieldCollection.Create(DocumentRange) Method

Creates a field from the specified range and adds it to the field collection.

Namespace : DevExpress.XtraRichEdit.API.Native

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

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
Field Create(
    DocumentRange range
)
vb
Function Create(
    range As DocumentRange
) As Field

Parameters

NameTypeDescription
rangeDocumentRange

A DocumentRange object specifying a range to be transformed to a field.

|

Returns

TypeDescription
Field

A Field object specifying the newly created field.

|

Example

The code sample below inserts text and converts it to the SYMBOL field.

View Example

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

//...
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    Document document = wordProcessor.Document;

    // Start to edit the document.
    document.BeginUpdate();

    // Append text.
    document.AppendText("SYMBOL 0x54 \\f Wingdings \\s 24");

    // Convert inserted text to a field.
    document.Fields.Create(document.Paragraphs[0].Range);

    // Update all fields.
    document.Fields.Update();

    // Finalize to edit the document.
    document.EndUpdate();

    // Save the result
    document.SaveDocument("Result.docx", DocumentFormat.Docx);
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

'...
Using wordProcessor As New RichEditDocumentServer()
    Dim document As Document = wordProcessor.Document

    ' Start to edit the document.
    document.BeginUpdate()

    ' Append text.
    document.AppendText("SYMBOL 0x54 \f Wingdings \s 24")

    ' Convert inserted text to a field.
    document.Fields.Create(document.Paragraphs(0).Range)

    ' Update all fields.
    document.Fields.Update()

    ' Finalize to edit the document.
    document.EndUpdate()

    ' Save the result
    document.SaveDocument("Result.docx", DocumentFormat.Docx)
End Using

The following code snippets (auto-collected from DevExpress Examples) contain references to the Create(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.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Field.cs#L66

csharp
//Convert the inserted text to the field
document.Fields.Create(document.Paragraphs[0].Range);
document.Fields.Update();

wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/DocumentFieldActions.cs#L56

csharp
document.EndUpdate();
document.Fields.Create(document.Paragraphs[0].Range);
document.Fields.Update();

word-document-api-examples/CS/CodeExamples/FieldActions.cs#L89

csharp
// Convert inserted text to a field.
document.Fields.Create(document.Paragraphs[0].Range);

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Field.vb#L54

vb
'Convert the inserted text to the field
document.Fields.Create(document.Paragraphs(CInt((0))).Range)
document.Fields.Update()

wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/DocumentFieldActions.vb#L49

vb
document.EndUpdate()
document.Fields.Create(document.Paragraphs(0).Range)
document.Fields.Update()

word-document-api-examples/VB/CodeExamples/FieldActions.vb#L75

vb
' Convert inserted text to a field.
document.Fields.Create(document.Paragraphs(CInt((0))).Range)
' Update all fields in the main document body.

See Also

Fields in Rich Text Documents

Field Codes

FieldCollection Interface

FieldCollection Members

DevExpress.XtraRichEdit.API.Native Namespace