/
FieldActions.cs
96 lines (78 loc) · 3.27 KB
/
FieldActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
class FieldActions
{
public static Action<RichEditDocumentServer> InsertFieldAction = InsertField;
public static Action<RichEditDocumentServer> ModifyFieldCodeAction = ModifyFieldCode;
public static Action<RichEditDocumentServer> CreateFieldFromRangeAction = CreateFieldFromRange;
static void InsertField(RichEditDocumentServer wordProcessor)
{
#region #InsertField
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Create the "DATE" field.
document.Fields.Create(document.Range.Start, "DATE");
// Update all fields in the main document body.
document.Fields.Update();
// Finalize to edit the document.
document.EndUpdate();
#endregion #InsertField
}
static void ModifyFieldCode(RichEditDocumentServer wordProcessor)
{
#region #ModifyFieldCode
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Create the "DATE" field.
document.Fields.Create(document.CaretPosition, "DATE");
// Finalize to edit the document.
document.EndUpdate();
// Check all fields in the document.
for (int i = 0; i < document.Fields.Count; i++)
{
// Access a field code.
string fieldCode = document.GetText(document.Fields[i].CodeRange);
// Check whether a field code is "DATE".
if (fieldCode == "DATE")
{
// Set the document position to the end of the field code range.
DocumentPosition position = document.Fields[i].CodeRange.End;
// Specify the date and time format for the field.
document.InsertText(position, @" \@ ""M / d / yyyy HH: mm:ss""");
}
}
// Update all fields in the main document body.
document.Fields.Update();
#endregion #ModifyFieldCode
}
static void CreateFieldFromRange(RichEditDocumentServer wordProcessor)
{
#region #CreateFieldFromRange
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Append text to the document.
document.AppendText("SYMBOL 0x54 \\f Wingdings \\s 24");
// Finalize to edit the document.
document.EndUpdate();
// Convert inserted text to a field.
document.Fields.Create(document.Paragraphs[0].Range);
// Update all fields in the main document body.
document.Fields.Update();
#endregion #CreateFieldFromRange
}
}
}