PreferredWidth Class

Represents a value and its unit of measure that is used to specify the preferred width of a table or a cell.

Inheritance Hierarchy

SystemObject
  Aspose.Words.TablesPreferredWidth

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

Syntax

public sealed class PreferredWidth

The PreferredWidth type exposes the following members.

Properties

  NameDescription
Public propertyCode exampleType
Gets the unit of measure used for this preferred width value.
Public propertyCode exampleValue
Gets the preferred width value. The unit of measure is specified in the Type property.

Methods

  NameDescription
Public methodCode exampleEquals(Object)
Determines whether the specified object is equal in value to the current object.
(Overrides ObjectEquals(Object).)
Public methodCode exampleEquals(PreferredWidth)
Determines whether the specified PreferredWidth is equal in value to the current PreferredWidth.
Public methodStatic memberCode exampleFromPercent
A creation method that returns a new instance that represents a preferred width specified as a percentage.
Public methodStatic memberCode exampleFromPoints
A creation method that returns a new instance that represents a preferred width specified using a number of points.
Public methodCode exampleGetHashCode
Serves as a hash function for this type.
(Overrides ObjectGetHashCode.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleToString
Returns a user-friendly string that displays the value of this object.
(Overrides ObjectToString.)

Fields

  NameDescription
Public fieldStatic memberCode exampleAuto
Returns an instance that represents the "preferred width is not specified" value.

Remarks

Preferred width can be specified as a percentage, number of points or a special "none/auto" value.

The instances of this class are immutable.

Examples

Shows how to set a table to auto fit to 50% of the page width.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a table with a width that takes up half the page width
Table table = builder.StartTable();

// Insert a few cells
builder.InsertCell();
table.PreferredWidth = PreferredWidth.FromPercent(50);
builder.Writeln("Cell #1");

builder.InsertCell();
builder.Writeln("Cell #2");

builder.InsertCell();
builder.Writeln("Cell #3");

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTableWithPreferredWidth.docx");
Shows how to set the different preferred width settings.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a table row made up of three cells which have different preferred widths
Table table = builder.StartTable();

// Insert an absolute sized cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightYellow;
builder.Writeln("Cell at 40 points width");

PreferredWidth width = builder.CellFormat.PreferredWidth;
Console.WriteLine($"Width \"{width.GetHashCode()}\": {width.ToString()}");

// Insert a relative (percent) sized cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(20);
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;
builder.Writeln("Cell at 20% width");

// Each cell had its own PreferredWidth
Assert.False(builder.CellFormat.PreferredWidth.Equals(width));

width = builder.CellFormat.PreferredWidth;
Console.WriteLine($"Width \"{width.GetHashCode()}\": {width.ToString()}");

// Insert a auto sized cell
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.Auto;
builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGreen;
builder.Writeln(
    "Cell automatically sized. The size of this cell is calculated from the table preferred width.");
builder.Writeln("In this case the cell will fill up the rest of the available space.");

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

See Also