-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
272 lines (223 loc) · 11.7 KB
/
Program.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
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.Office.Utils;
using System.Drawing;
namespace TablesSimpleExample
{
class Program
{
static void Main(string[] args)
{
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
CreateTable(wordProcessor.Document);
SetColumnWidth(wordProcessor.Document.Tables[0]);
WrapTextAroundTable(wordProcessor.Document);
MergeAndSplit(wordProcessor.Document);
FillData(wordProcessor.Document);
FormatData(wordProcessor.Document);
CustomizeTable(wordProcessor.Document);
TableStyle(wordProcessor.Document);
AdjustTableRows(wordProcessor.Document);
//DeleteElements(wordProcessor.Document);
wordProcessor.SaveDocument("DocumentWithTables.docx", DocumentFormat.OpenXml);
}
System.Diagnostics.Process.Start("DocumentWithTables.docx");
}
private static void CreateTable(Document document)
{
//Create a new table and specify its layout type
Table table = document.Tables.Create(document.Range.Start, 2, 2);
//Add new rows to the table
TableRow newRowBefore = table.Rows.InsertBefore(0);
TableRow newRowAfter = table.Rows.InsertAfter(0);
//Add a new column to the table
TableCell newLastColumn = table.Rows[0].Cells.Append();
}
private static void SetColumnWidth(Table table)
{
//Set the width of the first column
table.Rows[0].FirstCell.PreferredWidthType = WidthType.Fixed;
table.Rows[0].FirstCell.PreferredWidth = Units.InchesToDocumentsF(0.8f);
//Set the second column width and cell height
table[0, 1].PreferredWidthType = WidthType.Fixed;
table[0, 1].PreferredWidth = Units.InchesToDocumentsF(5f);
table[0, 1].HeightType = HeightType.Exact;
table[0, 1].Height = Units.InchesToDocumentsF(0.5f);
//Set the third column width
table.Rows[0].LastCell.PreferredWidthType = WidthType.Fixed;
table.Rows[0].LastCell.PreferredWidth = Units.InchesToDocumentsF(0.8f);
}
private static void MergeAndSplit(Document document)
{
Table table = document.Tables[0];
table.BeginUpdate();
//Split cell into 8
table[3, 1].Split(4, 2);
//Merge cells
table.MergeCells(table[4, 1], table[4, 2]);
table.MergeCells(table[6, 1], table[6, 2]);
table.EndUpdate();
}
private static void FillData(Document document)
{
Table table = document.Tables[0];
//Insert the header data
document.InsertSingleLineText(table.Rows[0].Cells[1].Range.Start, "Active Customers");
document.InsertSingleLineText(table[2, 0].Range.Start, "Photo");
document.InsertSingleLineText(table[2, 1].Range.Start, "Customer Info");
document.InsertSingleLineText(table[2, 2].Range.Start, "Rentals");
//Insert the customer photo
document.Images.Insert(table[3, 0].Range.Start, DocumentImageSource.FromFile("photo.png"));
//Insert the customer info
document.InsertText(table[3, 1].Range.Start, "Ryan Anita W");
document.InsertText(table[3, 2].Range.Start, "Intermediate");
document.InsertText(table[4, 1].Range.Start, "3/28/1984");
document.InsertText(table[5, 1].Range.Start, "[email protected]");
document.InsertText(table[5, 2].Range.Start, "(555)421-0059");
document.InsertText(table[6, 1].Range.Start, "5119 Beryl Dr, San Antonio, TX 78212");
document.InsertSingleLineText(table[3, 3].Range.Start, "18");
}
private static void FormatData(Document document)
{
Table table = document.Tables[0];
//Apply formatting to the "Active Customers" cell
CharacterProperties properties = document.BeginUpdateCharacters(table[0, 1].ContentRange);
properties.FontName = "Segoe UI";
properties.FontSize = 16;
document.EndUpdateCharacters(properties);
ParagraphProperties alignment = document.BeginUpdateParagraphs(table[0, 1].ContentRange);
alignment.Alignment = ParagraphAlignment.Center;
document.EndUpdateParagraphs(alignment);
table[0, 1].VerticalAlignment = TableCellVerticalAlignment.Center;
//Apply formatting to the header cells
CharacterProperties headerRowProperties = document.BeginUpdateCharacters(table.Rows[2].Range);
headerRowProperties.FontName = "Segoe UI";
headerRowProperties.FontSize = 11;
headerRowProperties.ForeColor = Color.FromArgb(212, 236, 183);
document.EndUpdateCharacters(headerRowProperties);
ParagraphProperties headerRowParagraphProperties = document.BeginUpdateParagraphs(table.Rows[2].Range);
headerRowParagraphProperties.Alignment = ParagraphAlignment.Center;
document.EndUpdateParagraphs(headerRowParagraphProperties);
//Apply formatting to the customer info cells
DocumentRange targetRange = document.CreateRange(table[3, 1].Range.Start, table[6, 2].Range.Start.ToInt() - table[3, 1].Range.Start.ToInt());
CharacterProperties infoProperties = document.BeginUpdateCharacters(targetRange);
infoProperties.FontSize = 8;
infoProperties.FontName = "Segoe UI";
infoProperties.ForeColor = Color.FromArgb(111, 116, 106);
document.EndUpdateCharacters(infoProperties);
//Format "Rentals" cells
CharacterProperties rentalFormat = document.BeginUpdateCharacters(table[3, 3].Range);
rentalFormat.FontSize = 28;
rentalFormat.Bold = true;
document.EndUpdateCharacters(rentalFormat);
ParagraphProperties rentalAlignment = document.BeginUpdateParagraphs(table[3, 3].Range);
rentalAlignment.Alignment = ParagraphAlignment.Center;
document.EndUpdateParagraphs(rentalAlignment);
table[3, 3].VerticalAlignment = TableCellVerticalAlignment.Center;
}
private static void CustomizeTable(Document document)
{
Table table = document.Tables[0];
table.BeginUpdate();
//Call the ChangeCellBorderColor method for every cell in the first two rows
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count; j++)
{
ChangeCellBorderColor(table[i, j]);
}
}
//Specify the background color for the third row
TableRow targetRow = table.Rows[2];
targetRow.Cells[0].BackgroundColor = Color.FromArgb(99, 122, 110);
targetRow.Cells[1].BackgroundColor = Color.FromArgb(99, 122, 110);
targetRow.Cells[2].BackgroundColor = Color.FromArgb(99, 122, 110);
table.EndUpdate();
}
private static void ChangeCellBorderColor(TableCell cell)
{
//Specify the border style and the background color for the header cells
cell.Borders.Bottom.LineStyle = BorderLineStyle.None;
cell.Borders.Left.LineStyle = BorderLineStyle.None;
cell.Borders.Right.LineStyle = BorderLineStyle.None;
cell.Borders.Top.LineStyle = BorderLineStyle.None;
cell.BackgroundColor = Color.Transparent;
}
private static void TableStyle(Document document)
{
document.BeginUpdate();
//Create a new table style
TableStyle tStyleMain = document.TableStyles.CreateNew();
//Specify style options
tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = BorderLineStyle.Single;
tStyleMain.TableBorders.InsideHorizontalBorder.LineColor = Color.White;
tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = BorderLineStyle.Single;
tStyleMain.TableBorders.InsideVerticalBorder.LineColor = Color.White;
tStyleMain.CellBackgroundColor = Color.FromArgb(227, 238, 220);
tStyleMain.Name = "MyTableStyle";
//Add the style to the document collection
document.TableStyles.Add(tStyleMain);
//Create conditional styles (styles for specific table elements)
TableConditionalStyle myNewStyleForOddRows = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddRowBanding);
myNewStyleForOddRows.CellBackgroundColor = Color.FromArgb(196, 220, 182);
TableConditionalStyle myNewStyleForBottomRightCell = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.BottomRightCell);
myNewStyleForBottomRightCell.CellBackgroundColor = Color.FromArgb(188, 214, 201);
document.EndUpdate();
// Apply a previously defined style to the table
document.Tables[0].Style = tStyleMain;
}
private static void WrapTextAroundTable(Document document)
{
Table table = document.Tables[0];
table.BeginUpdate();
//Wrap text around the table
table.TextWrappingType = TableTextWrappingType.Around;
//Specify vertical alignment:
table.RelativeVerticalPosition = TableRelativeVerticalPosition.Paragraph;
table.VerticalAlignment = TableVerticalAlignment.None;
table.OffsetYRelative = Units.InchesToDocumentsF(2f);
//Specify horizontal alignment:
table.RelativeHorizontalPosition = TableRelativeHorizontalPosition.Margin;
table.HorizontalAlignment = TableHorizontalAlignment.Center;
table.OffsetXRelative = Units.InchesToDocumentsF(2f);
//Set distance between the text and the table:
table.MarginBottom = Units.InchesToDocumentsF(0.3f);
table.MarginLeft = Units.InchesToDocumentsF(0.3f);
table.MarginTop = Units.InchesToDocumentsF(0.3f);
table.MarginRight = Units.InchesToDocumentsF(0.3f);
table.EndUpdate();
}
private static void AdjustTableRows(Document document)
{
Table table = document.Tables[0];
table.BeginUpdate();
//Repeat first three rows as header:
table.Rows[0].RepeatAsHeaderRow = true;
table.Rows[1].RepeatAsHeaderRow = true;
table.Rows[2].RepeatAsHeaderRow = true;
//Break last row across pages:
table.LastRow.BreakAcrossPages = true;
table.EndUpdate();
}
private static void DeleteElements(Document document)
{
Table tbl = document.Tables[0];
tbl.BeginUpdate();
//Delete a cell
tbl.Cell(1, 1).Delete();
//Delete a row
tbl.Rows[0].Delete();
tbl.EndUpdate();
//Delete the entire table
//document.Tables.Remove(tbl);
//Call the declared method using ForEachRow method and the corresponding delegate
tbl.ForEachRow(new TableRowProcessorDelegate(DeleteCells));
}
//Declare a method that deletes the second cell in every table row
public static void DeleteCells(TableRow row, int i)
{
row.Cells[1].Delete();
}
}
}