Rev 2094 | Rev 2106 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;using System.Collections;using System.Text;using System.ComponentModel;using System.Windows.Forms;using Word;namespace EA_DocGen{/// <summary>/// Summary description for TabularContent./// </summary>public class TabularContent{private Word.Application WordApp = null;private Word.Document WordDocument = null;private EA_Utilities EA_Utils = null;private Word.Range WordRange;private object startLocation;private object endLocation;public TabularContent(Word.Application wordApp, Word.Document wordDocument, EA_Utilities utilsRef){WordApp = wordApp;WordDocument = wordDocument;EA_Utils = utilsRef;//// TODO: Add constructor logic here//}public void AcceptWordAppAndDoc(Word.Application wordApp, Word.Document wordDocument){WordApp = wordApp;WordDocument = wordDocument;}public int Table_Create(string tableTitle, int numRows, int numCols){WordDocument.Content.InsertParagraphAfter();SelectInsertionPointAtEndOfDocument();Table_InsertCaption(tableTitle);startLocation = WordDocument.Content.End;WordDocument.Content.InsertParagraphAfter();endLocation = WordDocument.Content.End;WordRange = WordDocument.Range(ref startLocation, ref endLocation);object defaultTableBehaviour = Type.Missing;object autofitBehaviour = Type.Missing;Word.Table Table = WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );Table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;Table.Select();object tableTextStyle = EA_Constants.styleName_TableText;Table.Range.set_Style( ref tableTextStyle );return WordDocument.Tables.Count;}public int Table_CreateInvisible(int numRows, int numCols){WordDocument.Content.InsertParagraphAfter();SelectInsertionPointAtEndOfDocument();startLocation = WordDocument.Content.End;WordDocument.Content.InsertParagraphAfter();endLocation = WordDocument.Content.End;WordRange = WordDocument.Range(ref startLocation, ref endLocation);object defaultTableBehaviour = Type.Missing;object autofitBehaviour = Type.Missing;Word.Table Table = WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;Table.Select();object tableTextStyle = EA_Constants.styleName_TableText;Table.Range.set_Style( ref tableTextStyle );return WordDocument.Tables.Count;}public void Table_SetTableColumnTitle(Word.Table table, string title, int column){table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray10;table.Cell(1,column).Range.Text = title;table.Cell(1,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(1,column).Range.Font.Bold = 1;}public void Table_SetTableCellTitle(Word.Table table, string title, int row, int column){table.Cell(row,column).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;table.Cell(row,column).Range.Text = title;table.Cell(row,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(row,column).Range.Font.Bold = 1;}public int Table_InsertNewRowAfterThisRow(Word.Table table, int row){table.Rows[row].Select();object rowC = 1;WordApp.Selection.InsertRowsBelow(ref rowC);table.Rows[row+1].Shading.BackgroundPatternColor = Word.WdColor.wdColorWhite;return row+1;}public void Table_DeleteThisRow(Word.Table table, int row){table.Rows[row].Delete();}public void Table_DeleteRows(Word.Table table, int row, int count){for (int i=0; i<count; i++){table.Rows[row].Delete();}}public int Table_AppendSeperatorAndNewRowAfterThisRow(Word.Table table, int row){// add two more rows, the first being a seperatorint seperatorRow = Table_InsertNewRowAfterThisRow(table, row);int newRow = Table_InsertNewRowAfterThisRow(table, seperatorRow);// Make seperator row, one cell wide, and remove its left & right borderstable.Rows[seperatorRow].Select();WordApp.Selection.Cells.Merge();WordApp.Selection.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleNone;WordApp.Selection.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleNone;return newRow;}public void Table_MergeCellsInThisRow(Word.Table table, int row){table.Rows[row].Select();WordApp.Selection.Cells.Merge();}public void Table_InsertCaption(string captionText){object Label = "Table";object Title = Type.Missing;object TitleAutoText = Type.Missing;object Position = Word.WdCaptionPosition.wdCaptionPositionAbove;object ExcludeLabel = 0;WordApp.Selection.InsertCaption( ref Label, ref Title, ref TitleAutoText, ref Position, ref ExcludeLabel);WordApp.Selection.TypeText( ": " + captionText);WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;}private void SelectInsertionPointAtEndOfDocument(){object unit;object extend;unit = Word.WdUnits.wdStory;extend = Word.WdMovementType.wdMove;WordApp.Selection.EndKey(ref unit, ref extend);}public void processTableElement(EA.Element theElement, int recurse_level ){string [] EA_DocGenTable = null;string [] cells = null;string delimStr = "\r\n";char [] delim = delimStr.ToCharArray();string trimStr = " ";char [] trimChars = trimStr.ToCharArray();EA_DocGenTable = theElement.Notes.ToString().Split(delim,200);int columnCount = 0;int rowCount = 0;char seperator = ',';string tableTitle = "";bool gotSeperator = false;bool gotTitle = false;ArrayList colWidths = new ArrayList();int indent = 0;// Scan the notes content line by line looking for table parameters and counting// the number of table rows (which is not specified as an explicit table parameter).int s_ElementsConsumedByTableParams = 0;foreach(string s in EA_DocGenTable){if (s.Length > 0 && s != "\n" && s != "\r" ){if (gotTitle == false && s.StartsWith("title")){s_ElementsConsumedByTableParams++;tableTitle = EA_Utils.options.getOptionValue(s, tableTitle);gotTitle = true;if (tableTitle == ""){MessageBox.Show( "Table Element Serialisation Failed - Bad Title" );break;}}else if (columnCount == 0 && s.StartsWith("columns")){s_ElementsConsumedByTableParams++;columnCount = EA_Utils.options.getOptionValue(s, columnCount);if (columnCount == 0){MessageBox.Show( "Table Element Serialisation Failed - bad column count" );break;}}else if (gotSeperator == false && s.StartsWith("seperator")){s_ElementsConsumedByTableParams++;seperator = EA_Utils.options.getOptionValue(s, seperator);gotSeperator = true;}else if (s.StartsWith("widths")){s_ElementsConsumedByTableParams++;string optValStr = EA_Utils.options.getOptionValue(s, "");string width_delimStr = ",";char [] width_delim = width_delimStr.ToCharArray();string [] width_strings = optValStr.Split(width_delim, 50);foreach (string ws in width_strings){if (ws.Length > 0 && ws != "\n" && ws != "\r" ){colWidths.Add( System.Convert.ToDouble(ws) );}}}else if (s.StartsWith("indent")){s_ElementsConsumedByTableParams++;indent = EA_Utils.options.getOptionValue(s, 0);}else{rowCount++;}}}if (gotTitle == true && gotSeperator == true && columnCount != 0){if (rowCount < 2){MessageBox.Show( "Table Element Serialisation Failed - Insufficient Rows" );}else{// create the table in the word docint tableNum = Table_Create( tableTitle, rowCount, columnCount );Word.Table table = WordDocument.Tables[tableNum];object center = Word.WdParagraphAlignment.wdAlignParagraphCenter;int col = 1;foreach (double d in colWidths){if (col <= columnCount)table.Columns[col].SetWidth( WordApp.CentimetersToPoints((float)d), Word.WdRulerStyle.wdAdjustNone );col++;}if (indent > 0){table.Select();while (indent > 0){WordApp.Selection.Paragraphs.Indent();indent--;}}// scan the element notes again to extract the cell content and add it to the// table we just created.int row = 1;col = 0;foreach(string s in EA_DocGenTable){if (s.Length > 0 && s != "\n" && s != "\r" ){if (s_ElementsConsumedByTableParams > 0){s_ElementsConsumedByTableParams--;}else{delimStr = seperator.ToString();delim = delimStr.ToCharArray();cells = s.Split(delim,columnCount);if (cells.Length != columnCount){MessageBox.Show( "Table Element Serialisation Failed - Bad Row" );break;}else{// serialise table rowfor(col=1; col<=columnCount; col++){cells[col-1].TrimStart( trimChars );cells[col-1].TrimEnd( trimChars );table.Cell(row,col).Range.Text = cells[col-1];// special handling for heading rowif (row == 1){table.Cell(row,col).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(row,col).Range.Font.Bold = 1;}}row++;}}}}}}else{MessageBox.Show( "Table Element Serialisation Failed - Table Parameters Incomplete" );}}}}