I am trying to import a datatable to a word 2007 document using RichEditDocumentServer. I am using following code. How can i do following things.
- Is it possible to change the color of cell text based on cell data( if positive value then green else red)
2.how to provide the custom font name and size. - how to set the document page layout to landscape.
Thanks
Sam
private void AppendDataTable(Document document, DataTable dataTable) {
document.DefaultCharacterProperties.FontName = "Arial";
document.DefaultCharacterProperties.FontSize = 100;
document.DefaultCharacterProperties.Bold = true;
document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center;
document.DefaultCharacterProperties.ForeColor = Color.Red;
int dataTableRows = dataTable.Rows.Count;
int dataTableColumns = dataTable.Columns.Count;
List<string> columnsToDisplay = new List<string>();
for (int i = 0; i < dataTableColumns; i++) {
string name = dataTable.Columns[i].ColumnName;
// Skip PrimaryKey columns
if (!name.ToUpper().EndsWith("ID"))
columnsToDisplay.Add(name);
}
document.BeginUpdate();
Table table = document.Tables.Add(document.Range.End, dataTableRows + 1, columnsToDisplay.Count, AutoFitBehaviorType.AutoFitToContents);
table.TableLayout = TableLayoutType.Autofit;
table.Style.FontName = "Verdana";
table.Borders.InsideHorizontalBorder.LineColor = Color.LightBlue;
table.Borders.InsideVerticalBorder.LineColor = Color.LightBlue;
table.Borders.InsideHorizontalBorder.LineThickness = 0.5f;
table.Borders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Single;
table.Borders.InsideVerticalBorder.LineThickness = 0.5f;
table.Borders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Single;
table.LeftPadding = Units.InchesToDocumentsF(0.01f);
table.FirstRow.Height = Units.InchesToDocumentsF(0.5f);
table.FirstRow.HeightType = HeightType.Auto;
ParagraphProperties pp = document.BeginUpdateParagraphs(table.FirstRow.Range);
// pp.Style.FontName = "Arial";
pp.Alignment = ParagraphAlignment.Center;
document.EndUpdateParagraphs(pp);
CharacterProperties cp = document.BeginUpdateCharacters(table.FirstRow.Range);
cp.FontName = "Arial";
cp.FontSize = 100;
cp.ForeColor = Color.White;
document.EndUpdateCharacters(cp);
CharacterProperties cp1 = document.BeginUpdateCharacters(document.Range);
cp1.Bold = true;
cp1.ForeColor = Color.Red;
document.EndUpdateCharacters(cp1);
for (int i = 0; i < table.FirstRow.Cells.Count; i++) {
table.FirstRow.Cells[i].BackgroundColor = Color.SteelBlue;
table.FirstRow.Cells[i].VerticalAlignment = TableCellVerticalAlignment.Center;
table.FirstRow.Cells[i].WordWrap = false;
// table.FirstRow.Cells[i].Style.FontSize = 10;
}
// Fill table header with column names
for (int i = 0; i < columnsToDisplay.Count; i++) {
document.InsertText(table[0, i].Range.Start, columnsToDisplay[i]);
}
// Fill table body with data
table.ForEachCell(delegate(TableCell cell, int rowIndex, int cellIndex)
{
if (rowIndex > 0)
{
cell.WordWrap = false;
//dserver.Document.EndUpdateCharacters(cp);
document.InsertText(cell.Range.Start, dataTable.Rows[rowIndex - 1][columnsToDisplay[cellIndex]].ToString());
}
});
document.EndUpdate();
}