Ticket T114644
Visible to All Users

How to set custom color/Font on each TableCell

created 11 years ago

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.

  1. 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.
  2. 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();
        }

Answers approved by DevExpress Support

created 11 years ago

Hello,
I have created a sample project based on your code. I added the following lines to your code to change a specific cell's font settings depending on cell value:

C#
CharacterProperties cpCell = document.BeginUpdateCharacters(cell.Range); if(cellValue > 0) { cpCell.ForeColor = Color.Green; cpCell.Bold = true; cpCell.FontName = "Times New Roman"; } else { cpCell.ForeColor = Color.Red; cpCell.Bold = false; cpCell.FontName = "Courier New"; } document.EndUpdateCharacters(cpCell);

Using the following code line I changed page orientation to landscape:

C#
Section currentSection = document.GetSection(document.CaretPosition); currentSection.Page.Landscape = true;

Please check the attached sample and let me know whether or not it meets your requirements.

    Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

    Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.