Back to Devexpress

FieldCollection.Create(DocumentPosition, String) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-fieldcollection-dot-create-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentposition-system-dot-string-x29.md

latest8.3 KB
Original Source

FieldCollection.Create(DocumentPosition, String) Method

Adds a field specified by its code 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(
    DocumentPosition start,
    string code
)
vb
Function Create(
    start As DocumentPosition,
    code As String
) As Field

Parameters

NameTypeDescription
startDocumentPosition

A DocumentPosition at which the field is inserted.

| | code | String |

A string specifying the field’s code.

|

Returns

TypeDescription
Field

A Field object specifying the newly created field.

|

Example

The code snippet below uses the SYMBOL field to add a check mark (✔) to the document:

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

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

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

    // Create the "SYMBOL" field.
    document.Fields.Create(document.Range.Start, "SYMBOL 252 \\f Wingdings \\s 28");

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

    // Update all fields in the main document body.
    document.Fields.Update();

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

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

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

    ' Create the "SYMBOL" field.
    document.Fields.Create(document.Range.Start, "SYMBOL 252 \f Wingdings \s 28")

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

    ' Update all fields in the main document body.
    document.Fields.Update()

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the Create(DocumentPosition, String) 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/DocumentProperties.cs#L20

csharp
document.Fields.Create(document.AppendText("\nAUTHOR: ").End, "AUTHOR");
document.Fields.Create(document.AppendText("\nTITLE: ").End, "TITLE");

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

csharp
document.BeginUpdate();
document.Fields.Create(document.CaretPosition, "DATE");
document.Fields.Update();

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

csharp
// Create the "DATE" field.
document.Fields.Create(document.Range.Start, "DATE");

winforms-richedit-table-of-contents-practical-guide/CS/Form1.cs#L63

csharp
SearchForTOCEntries(delegate(DocumentPosition location, int level) {
    Document.Fields.Create(location, string.Format("TC \"{0}\" \\f {1} \\l {2}",
        Document.GetText(Document.Paragraphs.Get(location).Range), "defaultGroup", level));

word-processing-table-of-contents-practical-guide/CS/Program.cs#L84

csharp
SearchForTOCEntries(document, delegate (DocumentPosition location, int level) {
    document.Fields.Create(location, string.Format("TC \"{0}\" \\f {1} \\l {2}",
        document.GetText(document.Paragraphs.Get(location).Range), "defaultGroup", level));

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

vb
'Create a DATE field at the caret position
document.Fields.Create(caretPosition, "DATE")
document.Fields.Update()

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

vb
document.BeginUpdate()
document.Fields.Create(document.CaretPosition, "DATE")
document.Fields.Update()

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

vb
' Create the "DATE" field.
document.Fields.Create(document.Range.Start, "DATE")
' Update all fields in the main document body.

winforms-richedit-table-of-contents-practical-guide/VB/Form1.vb#L66

vb
Private Sub AddTCFields()
    SearchForTOCEntries(Sub(ByVal location, ByVal level) Document.Fields.Create(location, String.Format("TC ""{0}"" \f {1} \l {2}", Document.GetText(Document.Paragraphs.Get(location).Range), "defaultGroup", level)))
End Sub

word-processing-table-of-contents-practical-guide/VB/Program.vb#L80

vb
SearchForTOCEntries(document, Function(ByVal location As DocumentPosition, ByVal level As Integer)
                                  document.Fields.Create(location, String.Format("TC ""{0}"" \f {1} \l {2}",
                                  document.GetText(document.Paragraphs.[Get](location).Range), "defaultGroup", level))

See Also

Fields in Rich Text Documents

Field Codes

FieldCollection Interface

FieldCollection Members

DevExpress.XtraRichEdit.API.Native Namespace