| 2094 |
ghuddy |
1 |
using System;
|
| 2104 |
ghuddy |
2 |
using System.Collections;
|
|
|
3 |
using System.Text;
|
|
|
4 |
using System.ComponentModel;
|
|
|
5 |
using System.Windows.Forms;
|
| 2094 |
ghuddy |
6 |
using Word;
|
|
|
7 |
|
|
|
8 |
namespace EA_DocGen
|
|
|
9 |
{
|
|
|
10 |
/// <summary>
|
|
|
11 |
/// Summary description for TabularContent.
|
|
|
12 |
/// </summary>
|
|
|
13 |
public class TabularContent
|
|
|
14 |
{
|
| 2106 |
ghuddy |
15 |
private static Word.Range WordRange;
|
|
|
16 |
private static object startLocation;
|
|
|
17 |
private static object endLocation;
|
| 2094 |
ghuddy |
18 |
|
| 2106 |
ghuddy |
19 |
public static void initialise()
|
| 2094 |
ghuddy |
20 |
{
|
|
|
21 |
}
|
|
|
22 |
|
| 2116 |
ghuddy |
23 |
/// <summary>
|
|
|
24 |
/// This is an attempt to create a table similar in style to those made by the ERG doc template macros.
|
|
|
25 |
/// It is not exaclty that same though, and further work could be done to align it more precisely.
|
|
|
26 |
/// </summary>
|
|
|
27 |
/// <param name="tableTitle"></param>
|
|
|
28 |
/// <param name="numRows"></param>
|
|
|
29 |
/// <param name="numCols"></param>
|
|
|
30 |
/// <returns></returns>
|
| 2106 |
ghuddy |
31 |
public static int Table_Create(string tableTitle, bool borders, int numRows, int numCols)
|
| 2094 |
ghuddy |
32 |
{
|
| 2106 |
ghuddy |
33 |
createWordDoc.WordDocument.Content.InsertParagraphAfter();
|
| 2094 |
ghuddy |
34 |
SelectInsertionPointAtEndOfDocument();
|
|
|
35 |
|
|
|
36 |
Table_InsertCaption(tableTitle);
|
|
|
37 |
|
| 2106 |
ghuddy |
38 |
startLocation = createWordDoc.WordDocument.Content.End;
|
|
|
39 |
createWordDoc.WordDocument.Content.InsertParagraphAfter();
|
|
|
40 |
endLocation = createWordDoc.WordDocument.Content.End;
|
|
|
41 |
WordRange = createWordDoc.WordDocument.Range(ref startLocation, ref endLocation);
|
| 2094 |
ghuddy |
42 |
|
|
|
43 |
object defaultTableBehaviour = Type.Missing;
|
|
|
44 |
object autofitBehaviour = Type.Missing;
|
| 2106 |
ghuddy |
45 |
Word.Table Table = createWordDoc.WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );
|
| 2094 |
ghuddy |
46 |
|
| 2106 |
ghuddy |
47 |
if (borders)
|
|
|
48 |
{
|
|
|
49 |
Table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
|
|
|
50 |
Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
|
|
|
51 |
Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
|
|
|
52 |
}
|
|
|
53 |
else
|
|
|
54 |
{
|
|
|
55 |
Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
|
|
|
56 |
Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
|
|
|
57 |
}
|
| 2094 |
ghuddy |
58 |
|
|
|
59 |
Table.Select();
|
|
|
60 |
object tableTextStyle = EA_Constants.styleName_TableText;
|
|
|
61 |
Table.Range.set_Style( ref tableTextStyle );
|
|
|
62 |
|
| 2106 |
ghuddy |
63 |
return createWordDoc.WordDocument.Tables.Count;
|
| 2094 |
ghuddy |
64 |
}
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
|
| 2106 |
ghuddy |
68 |
public static void Table_SetTableColumnTitle(Word.Table table, string title, int column)
|
| 2094 |
ghuddy |
69 |
{
|
|
|
70 |
table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray10;
|
|
|
71 |
table.Cell(1,column).Range.Text = title;
|
|
|
72 |
table.Cell(1,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
|
|
73 |
table.Cell(1,column).Range.Font.Bold = 1;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
| 2106 |
ghuddy |
77 |
public static void Table_SetTableCellTitle(Word.Table table, string title, int row, int column)
|
| 2094 |
ghuddy |
78 |
{
|
|
|
79 |
table.Cell(row,column).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
|
|
|
80 |
table.Cell(row,column).Range.Text = title;
|
|
|
81 |
table.Cell(row,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
|
|
82 |
table.Cell(row,column).Range.Font.Bold = 1;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
|
| 2106 |
ghuddy |
86 |
public static int Table_InsertNewRowAfterThisRow(Word.Table table, int row)
|
| 2094 |
ghuddy |
87 |
{
|
|
|
88 |
table.Rows[row].Select();
|
|
|
89 |
object rowC = 1;
|
| 2106 |
ghuddy |
90 |
createWordDoc.WordApp.Selection.InsertRowsBelow(ref rowC);
|
| 2094 |
ghuddy |
91 |
table.Rows[row+1].Shading.BackgroundPatternColor = Word.WdColor.wdColorWhite;
|
|
|
92 |
return row+1;
|
|
|
93 |
}
|
|
|
94 |
|
| 2106 |
ghuddy |
95 |
public static void Table_DeleteThisRow(Word.Table table, int row)
|
| 2104 |
ghuddy |
96 |
{
|
|
|
97 |
table.Rows[row].Delete();
|
|
|
98 |
}
|
| 2094 |
ghuddy |
99 |
|
| 2106 |
ghuddy |
100 |
public static void Table_DeleteRows(Word.Table table, int row, int count)
|
| 2104 |
ghuddy |
101 |
{
|
|
|
102 |
for (int i=0; i<count; i++)
|
|
|
103 |
{
|
|
|
104 |
table.Rows[row].Delete();
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
| 2106 |
ghuddy |
108 |
public static int Table_AppendSeperatorAndNewRowAfterThisRow(Word.Table table, int row)
|
| 2094 |
ghuddy |
109 |
{
|
|
|
110 |
// add two more rows, the first being a seperator
|
|
|
111 |
int seperatorRow = Table_InsertNewRowAfterThisRow(table, row);
|
|
|
112 |
int newRow = Table_InsertNewRowAfterThisRow(table, seperatorRow);
|
|
|
113 |
|
|
|
114 |
// Make seperator row, one cell wide, and remove its left & right borders
|
|
|
115 |
table.Rows[seperatorRow].Select();
|
| 2106 |
ghuddy |
116 |
createWordDoc.WordApp.Selection.Cells.Merge();
|
|
|
117 |
createWordDoc.WordApp.Selection.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleNone;
|
|
|
118 |
createWordDoc.WordApp.Selection.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleNone;
|
| 2094 |
ghuddy |
119 |
|
|
|
120 |
return newRow;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
|
| 2106 |
ghuddy |
124 |
public static void Table_MergeCellsInThisRow(Word.Table table, int row)
|
| 2094 |
ghuddy |
125 |
{
|
|
|
126 |
table.Rows[row].Select();
|
| 2106 |
ghuddy |
127 |
createWordDoc.WordApp.Selection.Cells.Merge();
|
| 2094 |
ghuddy |
128 |
}
|
|
|
129 |
|
|
|
130 |
|
| 2106 |
ghuddy |
131 |
public static void Table_InsertCaption(string captionText)
|
| 2094 |
ghuddy |
132 |
{
|
| 2106 |
ghuddy |
133 |
if (captionText.Length > 0)
|
|
|
134 |
{
|
|
|
135 |
object Label = "Table";
|
|
|
136 |
object Title = Type.Missing;
|
|
|
137 |
object TitleAutoText = Type.Missing;
|
|
|
138 |
object Position = Word.WdCaptionPosition.wdCaptionPositionAbove;
|
|
|
139 |
object ExcludeLabel = 0;
|
|
|
140 |
createWordDoc.WordApp.Selection.InsertCaption( ref Label, ref Title, ref TitleAutoText, ref Position, ref ExcludeLabel);
|
|
|
141 |
createWordDoc.WordApp.Selection.TypeText( ": " + captionText);
|
|
|
142 |
createWordDoc.WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
|
|
143 |
}
|
| 2094 |
ghuddy |
144 |
}
|
|
|
145 |
|
|
|
146 |
|
| 2106 |
ghuddy |
147 |
private static void SelectInsertionPointAtEndOfDocument()
|
| 2094 |
ghuddy |
148 |
{
|
|
|
149 |
object unit;
|
|
|
150 |
object extend;
|
|
|
151 |
|
|
|
152 |
unit = Word.WdUnits.wdStory;
|
|
|
153 |
extend = Word.WdMovementType.wdMove;
|
| 2106 |
ghuddy |
154 |
createWordDoc.WordApp.Selection.EndKey(ref unit, ref extend);
|
| 2094 |
ghuddy |
155 |
}
|
|
|
156 |
|
| 2104 |
ghuddy |
157 |
|
| 2124 |
ghuddy |
158 |
/// <summary>
|
|
|
159 |
/// Processes an EA_DocGen table element, taking the notes that it contains, and forming a
|
|
|
160 |
/// table in the generated document with them. The notes contain instructions on how to format
|
|
|
161 |
/// the table, as well as the cell content. This must all be parsed and tokenised to extract the
|
|
|
162 |
/// options and data.
|
|
|
163 |
/// </summary>
|
|
|
164 |
/// <param name="theElement"></param>
|
|
|
165 |
/// <param name="recurse_level"></param>
|
| 2106 |
ghuddy |
166 |
public static void processTableElement(EA.Element theElement, int recurse_level )
|
| 2104 |
ghuddy |
167 |
{
|
|
|
168 |
string [] EA_DocGenTable = null;
|
|
|
169 |
string [] cells = null;
|
|
|
170 |
|
|
|
171 |
string delimStr = "\r\n";
|
|
|
172 |
char [] delim = delimStr.ToCharArray();
|
|
|
173 |
|
|
|
174 |
string trimStr = " ";
|
|
|
175 |
char [] trimChars = trimStr.ToCharArray();
|
|
|
176 |
|
|
|
177 |
EA_DocGenTable = theElement.Notes.ToString().Split(delim,200);
|
|
|
178 |
|
|
|
179 |
int columnCount = 0;
|
|
|
180 |
int rowCount = 0;
|
|
|
181 |
char seperator = ',';
|
|
|
182 |
string tableTitle = "";
|
| 2106 |
ghuddy |
183 |
bool borders=true;
|
|
|
184 |
|
| 2104 |
ghuddy |
185 |
bool gotSeperator = false;
|
|
|
186 |
bool gotTitle = false;
|
| 2106 |
ghuddy |
187 |
bool gotBorders = false;
|
|
|
188 |
bool gotWidths = false;
|
|
|
189 |
bool gotIndent = false;
|
| 2104 |
ghuddy |
190 |
|
|
|
191 |
ArrayList colWidths = new ArrayList();
|
|
|
192 |
int indent = 0;
|
|
|
193 |
|
|
|
194 |
// Scan the notes content line by line looking for table parameters and counting
|
|
|
195 |
// the number of table rows (which is not specified as an explicit table parameter).
|
|
|
196 |
int s_ElementsConsumedByTableParams = 0;
|
|
|
197 |
foreach(string s in EA_DocGenTable)
|
|
|
198 |
{
|
|
|
199 |
if (s.Length > 0 && s != "\n" && s != "\r" )
|
|
|
200 |
{
|
| 2106 |
ghuddy |
201 |
if (gotTitle == false && s.StartsWith("title="))
|
| 2104 |
ghuddy |
202 |
{
|
|
|
203 |
s_ElementsConsumedByTableParams++;
|
| 2106 |
ghuddy |
204 |
tableTitle = EA_DocGenOptions.getOptionValue(s, tableTitle);
|
| 2104 |
ghuddy |
205 |
gotTitle = true;
|
| 2106 |
ghuddy |
206 |
//if (tableTitle == "")
|
|
|
207 |
//{
|
|
|
208 |
// MessageBox.Show( "Table Element Serialisation Failed - Bad Title" );
|
|
|
209 |
// break;
|
|
|
210 |
//}
|
| 2104 |
ghuddy |
211 |
}
|
|
|
212 |
|
| 2106 |
ghuddy |
213 |
else if (columnCount == 0 && s.StartsWith("columns="))
|
| 2104 |
ghuddy |
214 |
{
|
|
|
215 |
s_ElementsConsumedByTableParams++;
|
| 2106 |
ghuddy |
216 |
columnCount = EA_DocGenOptions.getOptionValue(s, columnCount);
|
| 2104 |
ghuddy |
217 |
if (columnCount == 0)
|
|
|
218 |
{
|
|
|
219 |
MessageBox.Show( "Table Element Serialisation Failed - bad column count" );
|
|
|
220 |
break;
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
| 2106 |
ghuddy |
224 |
else if (gotSeperator == false && s.StartsWith("seperator="))
|
| 2104 |
ghuddy |
225 |
{
|
|
|
226 |
s_ElementsConsumedByTableParams++;
|
| 2106 |
ghuddy |
227 |
seperator = EA_DocGenOptions.getOptionValue(s, seperator);
|
| 2104 |
ghuddy |
228 |
gotSeperator = true;
|
|
|
229 |
}
|
|
|
230 |
|
| 2106 |
ghuddy |
231 |
else if (gotWidths == false && s.StartsWith("widths="))
|
| 2104 |
ghuddy |
232 |
{
|
|
|
233 |
s_ElementsConsumedByTableParams++;
|
| 2106 |
ghuddy |
234 |
gotWidths = true;
|
|
|
235 |
string optValStr = EA_DocGenOptions.getOptionValue(s, "");
|
| 2104 |
ghuddy |
236 |
|
|
|
237 |
string width_delimStr = ",";
|
|
|
238 |
char [] width_delim = width_delimStr.ToCharArray();
|
|
|
239 |
|
|
|
240 |
string [] width_strings = optValStr.Split(width_delim, 50);
|
|
|
241 |
|
|
|
242 |
foreach (string ws in width_strings)
|
|
|
243 |
{
|
|
|
244 |
if (ws.Length > 0 && ws != "\n" && ws != "\r" )
|
|
|
245 |
{
|
|
|
246 |
colWidths.Add( System.Convert.ToDouble(ws) );
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
}
|
| 2106 |
ghuddy |
250 |
else if (gotIndent == false && s.StartsWith("indent="))
|
| 2104 |
ghuddy |
251 |
{
|
|
|
252 |
s_ElementsConsumedByTableParams++;
|
| 2106 |
ghuddy |
253 |
gotIndent = true;
|
|
|
254 |
indent = EA_DocGenOptions.getOptionValue(s, 0);
|
| 2104 |
ghuddy |
255 |
}
|
| 2106 |
ghuddy |
256 |
else if (gotBorders == false && s.StartsWith("borders="))
|
|
|
257 |
{
|
|
|
258 |
s_ElementsConsumedByTableParams++;
|
|
|
259 |
gotBorders = true;
|
|
|
260 |
borders = EA_DocGenOptions.getOptionValue(s, borders);
|
|
|
261 |
}
|
| 2104 |
ghuddy |
262 |
else
|
|
|
263 |
{
|
|
|
264 |
rowCount++;
|
|
|
265 |
}
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
| 2106 |
ghuddy |
269 |
if (columnCount > 0 && rowCount > 0)
|
| 2104 |
ghuddy |
270 |
{
|
|
|
271 |
if (rowCount < 2)
|
|
|
272 |
{
|
|
|
273 |
MessageBox.Show( "Table Element Serialisation Failed - Insufficient Rows" );
|
|
|
274 |
}
|
|
|
275 |
else
|
|
|
276 |
{
|
|
|
277 |
// create the table in the word doc
|
| 2106 |
ghuddy |
278 |
int tableNum = Table_Create( tableTitle, borders, rowCount, columnCount );
|
|
|
279 |
Word.Table table = createWordDoc.WordDocument.Tables[tableNum];
|
| 2104 |
ghuddy |
280 |
object center = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
|
|
281 |
|
|
|
282 |
int col = 1;
|
|
|
283 |
foreach (double d in colWidths)
|
|
|
284 |
{
|
|
|
285 |
if (col <= columnCount)
|
| 2106 |
ghuddy |
286 |
table.Columns[col].SetWidth( createWordDoc.WordApp.CentimetersToPoints((float)d), Word.WdRulerStyle.wdAdjustNone );
|
| 2104 |
ghuddy |
287 |
col++;
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
if (indent > 0)
|
|
|
291 |
{
|
|
|
292 |
table.Select();
|
|
|
293 |
while (indent > 0)
|
|
|
294 |
{
|
| 2106 |
ghuddy |
295 |
createWordDoc.WordApp.Selection.Paragraphs.Indent();
|
| 2104 |
ghuddy |
296 |
indent--;
|
|
|
297 |
}
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
// scan the element notes again to extract the cell content and add it to the
|
|
|
301 |
// table we just created.
|
|
|
302 |
int row = 1;
|
|
|
303 |
col = 0;
|
|
|
304 |
foreach(string s in EA_DocGenTable)
|
|
|
305 |
{
|
|
|
306 |
if (s.Length > 0 && s != "\n" && s != "\r" )
|
|
|
307 |
{
|
|
|
308 |
if (s_ElementsConsumedByTableParams > 0)
|
|
|
309 |
{
|
|
|
310 |
s_ElementsConsumedByTableParams--;
|
|
|
311 |
}
|
|
|
312 |
else
|
|
|
313 |
{
|
|
|
314 |
delimStr = seperator.ToString();
|
|
|
315 |
delim = delimStr.ToCharArray();
|
|
|
316 |
cells = s.Split(delim,columnCount);
|
|
|
317 |
|
|
|
318 |
if (cells.Length != columnCount)
|
|
|
319 |
{
|
|
|
320 |
MessageBox.Show( "Table Element Serialisation Failed - Bad Row" );
|
|
|
321 |
break;
|
|
|
322 |
}
|
|
|
323 |
else
|
|
|
324 |
{
|
|
|
325 |
// serialise table row
|
|
|
326 |
for(col=1; col<=columnCount; col++)
|
|
|
327 |
{
|
|
|
328 |
cells[col-1].TrimStart( trimChars );
|
|
|
329 |
cells[col-1].TrimEnd( trimChars );
|
|
|
330 |
|
|
|
331 |
table.Cell(row,col).Range.Text = cells[col-1];
|
|
|
332 |
|
|
|
333 |
// special handling for heading row
|
|
|
334 |
if (row == 1)
|
|
|
335 |
{
|
|
|
336 |
table.Cell(row,col).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
|
|
|
337 |
table.Cell(row,col).Range.Font.Bold = 1;
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
row++;
|
|
|
341 |
}
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
}
|
|
|
346 |
}
|
|
|
347 |
else
|
|
|
348 |
{
|
|
|
349 |
MessageBox.Show( "Table Element Serialisation Failed - Table Parameters Incomplete" );
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
| 2094 |
ghuddy |
353 |
}
|
|
|
354 |
}
|