CellFormat Class

Represents all formatting for a table cell.

Inheritance Hierarchy

SystemObject
  Aspose.Words.TablesCellFormat

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0

Syntax

public class CellFormat

The CellFormat type exposes the following members.

Properties

  NameDescription
Public propertyCode exampleBorders
Gets collection of borders of the cell.
Public propertyCode exampleBottomPadding
Returns or sets the amount of space (in points) to add below the contents of cell.
Public propertyCode exampleFitText
If true, fits text in the cell, compressing each paragraph to the width of the cell.
Public propertyCode exampleHorizontalMerge
Specifies how the cell is merged horizontally with other cells in the row.
Public propertyCode exampleLeftPadding
Returns or sets the amount of space (in points) to add to the left of the contents of cell.
Public propertyCode exampleOrientation
Returns or sets the orientation of text in a table cell.
Public propertyCode examplePreferredWidth
Returns or sets the preferred width of the cell.
Public propertyCode exampleRightPadding
Returns or sets the amount of space (in points) to add to the right of the contents of cell.
Public propertyCode exampleShading
Returns a Shading object that refers to the shading formatting for the cell.
Public propertyCode exampleTopPadding
Returns or sets the amount of space (in points) to add above the contents of cell.
Public propertyCode exampleVerticalAlignment
Returns or sets the vertical alignment of text in the cell.
Public propertyCode exampleVerticalMerge
Specifies how the cell is merged with other cells vertically.
Public propertyCode exampleWidth
Gets the width of the cell in points.
Public propertyCode exampleWrapText
If true, wrap text for the cell.

Methods

  NameDescription
Public methodCode exampleClearFormatting
Resets to default cell formatting. Does not change the width of the cell.
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleSetPaddings
Sets the amount of space (in points) to add to the left/top/right/bottom of the contents of cell.
Public methodToString (Inherited from Object.)

Examples

Shows how to modify formatting of a table cell.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);

// Retrieve the first cell in the table
Cell firstCell = table.FirstRow.FirstCell;

// Modify some row level properties
firstCell.CellFormat.Width = 30; // in points
firstCell.CellFormat.Orientation = TextOrientation.Downward;
firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;

doc.Save(ArtifactsDir + "Table.CellFormat.docx");
Shows how to modify the format of rows and cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
builder.InsertCell();
builder.Write("City");
builder.InsertCell();
builder.Write("Country");
builder.EndRow();
builder.InsertCell();
builder.Write("London");
builder.InsertCell();
builder.Write("U.K.");
builder.EndTable();

// The appearance of rows and individual cells can be edited using the respective formatting objects
RowFormat rowFormat = table.FirstRow.RowFormat;
rowFormat.Height = 25;
rowFormat.Borders[BorderType.Bottom].Color = Color.Red;

CellFormat cellFormat = table.LastRow.FirstCell.CellFormat;
cellFormat.Width = 100;
cellFormat.Shading.BackgroundPatternColor = Color.Orange;

doc.Save(ArtifactsDir + "Table.RowCellFormat.docx");
Shows how to build a nice bordered table.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Start building a table
builder.StartTable();

// Set the appropriate paragraph, cell, and row formatting. The formatting properties are preserved
// until they are explicitly modified so there's no need to set them for each row or cell
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.CellFormat.ClearFormatting();
builder.CellFormat.Width = 150;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.Shading.BackgroundPatternColor = Color.GreenYellow;
builder.CellFormat.WrapText = false;
builder.CellFormat.FitText = true;

builder.RowFormat.ClearFormatting();
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Height = 50;
builder.RowFormat.Borders.LineStyle = LineStyle.Engrave3D;
builder.RowFormat.Borders.Color = Color.Orange;

builder.InsertCell();
builder.Write("Row 1, Col 1");

builder.InsertCell();
builder.Write("Row 1, Col 2");

builder.EndRow();

// Remove the shading (clear background)
builder.CellFormat.Shading.ClearFormatting();

builder.InsertCell();
builder.Write("Row 2, Col 1");

builder.InsertCell();
builder.Write("Row 2, Col 2");

builder.EndRow();

builder.InsertCell();

// Make the row height bigger so that a vertically oriented text could fit into cells
builder.RowFormat.Height = 150;
builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Write("Row 3, Col 1");

builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Write("Row 3, Col 2");

builder.EndRow();

builder.EndTable();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTable.docx");

See Also