/
Table.cs
289 lines (268 loc) · 13.4 KB
/
Table.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Drawing;
using System.IO;
namespace RichEditAPISample.CodeExamples
{
class TableActions
{
static void CreateTable(Document document)
{
#region #CreateTable
// Insert new table.
Table tbl = document.Tables.Create(document.Range.Start, 1, 3, AutoFitBehaviorType.AutoFitToWindow);
// Create a table header.
document.InsertText(tbl[0, 0].Range.Start, "Name");
document.InsertText(tbl[0, 1].Range.Start, "Size");
document.InsertText(tbl[0, 2].Range.Start, "DateTime");
// Insert table data.
DirectoryInfo dirinfo = new DirectoryInfo("C:\\");
try
{
tbl.BeginUpdate();
foreach (FileInfo fi in dirinfo.GetFiles())
{
TableRow row = tbl.Rows.Append();
TableCell cell = row.FirstCell;
string fileName = fi.Name;
string fileLength = String.Format("{0:N0}", fi.Length);
string fileLastTime = String.Format("{0:g}", fi.LastWriteTime);
document.InsertSingleLineText(cell.Range.Start, fileName);
document.InsertSingleLineText(cell.Next.Range.Start, fileLength);
document.InsertSingleLineText(cell.Next.Next.Range.Start, fileLastTime);
}
// Center the table header.
foreach (Paragraph p in document.Paragraphs.Get(tbl.FirstRow.Range))
{
p.Alignment = ParagraphAlignment.Center;
}
}
finally
{
tbl.EndUpdate();
}
#endregion #CreateTable
}
static void CreateFixedTable(Document document)
{
#region #CreateFixedTable
Table table = document.Tables.Create(document.Range.Start, 3, 4);
table.TableAlignment = TableRowAlignment.Center;
table.TableLayout = TableLayoutType.Fixed;
table.PreferredWidthType = WidthType.Fixed;
table.PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(4f);
table.Rows[1].HeightType = HeightType.Exact;
table.Rows[1].Height = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.8f);
table[1, 2].PreferredWidthType = WidthType.Fixed;
table[1, 2].PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(1.5f);
#endregion #CreateFixedTable
}
static void ChangeTableColor(Document document)
{
#region #ChangeTableColor
// Create a table.
Table table = document.Tables.Create(document.Range.Start, 3, 5, AutoFitBehaviorType.AutoFitToWindow);
table.BeginUpdate();
// Provide the space between table cells.
// The distance between cells will be 4 mm.
document.Unit = DevExpress.Office.DocumentUnit.Millimeter;
table.TableCellSpacing = 2;
// Change the color of empty space between cells.
table.TableBackgroundColor = Color.Violet;
//Change cell background color.
table.ForEachCell(new TableCellProcessorDelegate(TableHelper.ChangeCellColor));
table.ForEachCell(new TableCellProcessorDelegate(TableHelper.ChangeCellBorderColor));
table.EndUpdate();
#endregion #ChangeTableColor
}
#region #@ChangeTableColor
class TableHelper
{
public static void ChangeCellColor(TableCell cell, int i, int j)
{
cell.BackgroundColor = System.Drawing.Color.Yellow;
}
public static void ChangeCellBorderColor(TableCell cell, int i, int j)
{
cell.Borders.Bottom.LineColor = System.Drawing.Color.Red;
cell.Borders.Left.LineColor = System.Drawing.Color.Red;
cell.Borders.Right.LineColor = System.Drawing.Color.Red;
cell.Borders.Top.LineColor = System.Drawing.Color.Red;
}
}
#endregion #@ChangeTableColor
static void CreateAndApplyTableStyle(Document document)
{
#region #CreateAndApplyTableStyle
document.BeginUpdate();
// Create a new table style.
TableStyle tStyleMain = document.TableStyles.CreateNew();
// Specify style characteristics.
tStyleMain.AllCaps = true;
tStyleMain.FontName = "Segoe Condensed";
tStyleMain.FontSize = 14;
tStyleMain.Alignment = ParagraphAlignment.Center;
tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Dotted;
tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Dotted;
tStyleMain.TableBorders.Top.LineThickness = 1.5f;
tStyleMain.TableBorders.Top.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Left.LineThickness = 1.5f;
tStyleMain.TableBorders.Left.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Bottom.LineThickness = 1.5f;
tStyleMain.TableBorders.Bottom.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Right.LineThickness = 1.5f;
tStyleMain.TableBorders.Right.LineStyle = TableBorderLineStyle.Double;
tStyleMain.CellBackgroundColor = System.Drawing.Color.LightBlue;
tStyleMain.TableLayout = TableLayoutType.Fixed;
tStyleMain.Name = "MyTableStyle";
//Add the style to the document.
document.TableStyles.Add(tStyleMain);
document.EndUpdate();
document.BeginUpdate();
// Create a table.
Table table = document.Tables.Create(document.Range.Start, 3, 3);
table.TableLayout = TableLayoutType.Fixed;
table.PreferredWidthType = WidthType.Fixed;
table.PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(4.5f);
table[1, 1].PreferredWidthType = WidthType.Fixed;
table[1, 1].PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(1.5f);
// Apply a previously defined style.
table.Style = tStyleMain;
document.EndUpdate();
document.InsertText(table[1, 1].Range.Start, "STYLED");
#endregion #CreateAndApplyTableStyle
}
static void UseConditionalStyle(Document document)
{
#region #UseConditionalStyle
document.LoadDocument("Documents//TableStyles.docx", DocumentFormat.OpenXml);
document.BeginUpdate();
// Create a new style that is based on the 'Grid Table 5 Dark Accent 1' style defined in the loaded document.
TableStyle myNewStyle = document.TableStyles.CreateNew();
myNewStyle.Parent = document.TableStyles["Grid Table 5 Dark Accent 1"];
// Create conditional styles (styles for table elements)
TableConditionalStyle myNewStyleForFirstRow =
myNewStyle.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.FirstRow);
myNewStyleForFirstRow.CellBackgroundColor = Color.PaleVioletRed;
TableConditionalStyle myNewStyleForFirstColumn =
myNewStyle.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.FirstColumn);
myNewStyleForFirstColumn.CellBackgroundColor = Color.PaleVioletRed;
TableConditionalStyle myNewStyleForOddColumns =
myNewStyle.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddColumnBanding);
myNewStyleForOddColumns.CellBackgroundColor = System.Windows.Forms.ControlPaint.Light(Color.PaleVioletRed);
TableConditionalStyle myNewStyleForEvenColumns =
myNewStyle.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.EvenColumnBanding);
myNewStyleForEvenColumns.CellBackgroundColor = System.Windows.Forms.ControlPaint.LightLight(Color.PaleVioletRed);
document.TableStyles.Add(myNewStyle);
// Create a new table and apply a new style.
Table table = document.Tables.Create(document.Range.End, 4, 4, AutoFitBehaviorType.AutoFitToWindow);
table.Style = myNewStyle;
// Specify which conditonal styles are in effect.
table.TableLook = TableLookTypes.ApplyFirstRow | TableLookTypes.ApplyFirstColumn;
document.EndUpdate();
#endregion #UseConditionalStyle
}
static void ChangeColumnAppearance(Document document)
{
#region #ChangeColumnAppearance
Table table = document.Tables.Create(document.Range.Start, 3, 10);
table.BeginUpdate();
//Change cell background color and vertical alignment in the third column.
table.ForEachRow(new TableRowProcessorDelegate(ChangeColumnAppearanceHelper.ChangeColumnColor));
table.EndUpdate();
#endregion #ChangeColumnAppearance
}
#region #@ChangeColumnAppearance
class ChangeColumnAppearanceHelper
{
public static void ChangeColumnColor(TableRow row, int rowIndex)
{
row[2].BackgroundColor = System.Drawing.Color.LightCyan;
row[2].VerticalAlignment = TableCellVerticalAlignment.Center;
}
}
#endregion #@ChangeColumnAppearance
static void UseTableCellProcessor(Document document)
{
#region #UseTableCellProcessor
Table table = document.Tables.Create(document.Range.Start, 8, 8);
table.BeginUpdate();
table.ForEachCell(new TableCellProcessorDelegate(UseTableCellProcessorHelper.MakeMultiplicationCell));
table.EndUpdate();
#endregion #UseTableCellProcessor
}
#region #@UseTableCellProcessor
class UseTableCellProcessorHelper
{
public static void MakeMultiplicationCell(TableCell cell, int i, int j)
{
SubDocument doc = cell.Range.BeginUpdateDocument();
doc.InsertText(cell.Range.Start,
String.Format("{0}*{1} = {2}", i + 2, j + 2, (i + 2) * (j + 2)));
cell.Range.EndUpdateDocument(doc);
}
}
#endregion #@UseTableCellProcessor
static void MergeCells(Document document)
{
#region #MergeCells
Table table = document.Tables.Create(document.Range.Start, 6, 8);
table.BeginUpdate();
table.MergeCells(table[2, 1], table[5, 1]);
table.MergeCells(table[2, 3], table[2, 7]);
table.EndUpdate();
#endregion #MergeCells
}
static void SplitCells(Document document)
{
#region #SplitCells
Table table = document.Tables.Create(document.Range.Start, 3, 3, AutoFitBehaviorType.FixedColumnWidth, 350);
//split a cell to three:
table.Cell(2, 1).Split(1, 3);
#endregion #SplitCells
}
static void DeleteTableElements(Document document)
{
#region #DeleteTableElements
Table tbl = document.Tables.Create(document.Range.Start, 3, 3, AutoFitBehaviorType.AutoFitToWindow);
tbl.BeginUpdate();
//Delete a cell:
tbl.Cell(1, 1).Delete();
//Delete a row:
tbl.Rows[2].Delete();
tbl.EndUpdate();
#endregion #DeleteTableElements
}
static void DeleteTable(Document document)
{
#region #DeleteTable
Table tbl = document.Tables.Create(document.Range.Start, 3, 4);
//To delete the table, uncomment the method below:
// document.Tables.Remove(tbl);
#endregion #DeleteTable
}
static void WrapTextAroundTable(Document document)
{
#region #WrapTextAroundTable
document.LoadDocument("Documents//Grimm.docx");
Table table = document.Tables.Create(document.Paragraphs[4].Range.Start, 3, 3, AutoFitBehaviorType.AutoFitToContents);
table.BeginUpdate();
table.TextWrappingType = TableTextWrappingType.Around;
//Specify vertical alignment:
table.RelativeVerticalPosition = TableRelativeVerticalPosition.Paragraph;
table.VerticalAlignment = TableVerticalAlignment.None;
table.OffsetYRelative = DevExpress.Office.Utils.Units.InchesToDocumentsF(2f);
//Specify horizontal alignment:
table.RelativeHorizontalPosition = TableRelativeHorizontalPosition.Margin;
table.HorizontalAlignment = TableHorizontalAlignment.Center;
//Set distance between the text and the table:
table.MarginBottom = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.3f);
table.MarginLeft = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.3f);
table.MarginTop = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.3f);
table.MarginRight = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.3f);
table.EndUpdate();
#endregion #WrapTextAroundTable
}
}
}