-
Notifications
You must be signed in to change notification settings - Fork 9
/
FormattingActions.cs
184 lines (140 loc) · 7.19 KB
/
FormattingActions.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Drawing;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
class FormattingActions
{
public static Action<RichEditDocumentServer> FormatTextAction = FormatText;
public static Action<RichEditDocumentServer> ChangeSpacingAction = ChangeSpacing;
public static Action<RichEditDocumentServer> ResetCharacterFormattingAction = ResetCharacterFormatting;
public static Action<RichEditDocumentServer> FormatParagraphAction = FormatParagraph;
public static Action<RichEditDocumentServer> ResetParagraphFormattingAction = ResetParagraphFormatting;
static void FormatText(RichEditDocumentServer wordProcessor)
{
#region #FormatText
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Append text to the document.
document.AppendText("Normal\nFormatted\nNormal");
// Finalize to edit the document.
document.EndUpdate();
// Access the range of the document's second paragraph.
DocumentRange range = document.Paragraphs[1].Range;
// Start to modify character formatting of the target range.
CharacterProperties cp = document.BeginUpdateCharacters(range);
// Specify character formatting options.
cp.FontName = "Comic Sans MS";
cp.FontSize = 18;
cp.ForeColor = Color.Blue;
cp.BackColor = Color.Snow;
cp.Underline = UnderlineType.DoubleWave;
cp.UnderlineColor = Color.Red;
// Finalize to modify character formatting.
document.EndUpdateCharacters(cp);
#endregion #FormatText
}
static void ChangeSpacing(RichEditDocumentServer wordProcessor)
{
#region #ChangeCharacterSpacing
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Append text to the document.
document.AppendText("Normal\nFormatted\nNormal");
// Finalize to edit the document.
document.EndUpdate();
// Access the range of the document's second paragraph.
DocumentRange range = document.Paragraphs[1].Range;
// Start to modify character formatting of the target range.
CharacterProperties cp = document.BeginUpdateCharacters(range);
// Change character spacing and scaling.
cp.Scale = 150;
cp.Spacing = -2;
// Raise the text by 2 points.
cp.Position = 2;
// Finalize to modify character formatting.
document.EndUpdateCharacters(cp);
#endregion #ChangeCharacterSpacing
}
static void ResetCharacterFormatting(RichEditDocumentServer wordProcessor)
{
#region #ResetCharacterFormatting
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Access the range of the document's first paragraph.
DocumentRange range = document.Paragraphs[0].Range;
// Start to modify character formatting of the target range.
CharacterProperties cp = document.BeginUpdateCharacters(range);
// Set the font size and font name of the target range's characters to default values.
// Other character properties remain intact.
cp.Reset(CharacterPropertiesMask.FontSize | CharacterPropertiesMask.FontName | CharacterPropertiesMask.FontNameAscii);
// Finalize to modify character formatting.
document.EndUpdateCharacters(cp);
#endregion #ResetCharacterFormatting
}
static void FormatParagraph(RichEditDocumentServer wordProcessor)
{
#region #FormatParagraph
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Append text to the document.
document.AppendText("Modified Paragraph\nNormal\nNormal");
// Finalize to edit the document.
document.EndUpdate();
// Access the first paragraph range.
DocumentRange range = document.Paragraphs[0].Range;
// Start to edit the paragraph.
ParagraphProperties pp = document.BeginUpdateParagraphs(range);
// Specify the paragraph's alignment.
pp.Alignment = ParagraphAlignment.Center;
// Specify the paragraph's line spacing.
pp.LineSpacingType = ParagraphLineSpacing.Multiple;
pp.LineSpacingMultiplier = 3;
// Set the paragraph’s left indent to 0.5 document unit.
// Default unit is 1/300 of an inch (a document unit).
pp.LeftIndent = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.5f);
// Start to modify tab stops in the paragraph.
TabInfoCollection tbiColl = pp.BeginUpdateTabs(true);
// Create a new tab stop for the paragraph.
TabInfo tbi = new DevExpress.XtraRichEdit.API.Native.TabInfo();
// Specify the tab stop's alignment type.
tbi.Alignment = TabAlignmentType.Center;
// Set the tab stop position to 1.5 document unit.
tbi.Position = DevExpress.Office.Utils.Units.InchesToDocumentsF(1.5f);
// Add the tab stop to the collection of tab stops.
tbiColl.Add(tbi);
// Finalize to modify tab stops in the paragraph.
pp.EndUpdateTabs(tbiColl);
// Finalize to edit the paragraph.
document.EndUpdateParagraphs(pp);
#endregion #FormatParagraph
}
static void ResetParagraphFormatting(RichEditDocumentServer wordProcessor)
{
#region #ResetParagraphFormatting
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Access the range of the document's first paragraph.
DocumentRange range = document.Paragraphs[0].Range;
// Start to edit the paragraph.
ParagraphProperties cp = document.BeginUpdateParagraphs(range);
// Set alignmment and first line indent of the target paragraph to default values.
// Other paragraph properties remain intact.
cp.Reset(ParagraphPropertiesMask.Alignment | ParagraphPropertiesMask.FirstLineIndent);
// Finalize to edit the paragraph.
document.EndUpdateParagraphs(cp);
#endregion #ResetParagraphFormatting
}
}
}