PreferredWidthFromPercent Method

A creation method that returns a new instance that represents a preferred width specified as a percentage.

Namespace:  Aspose.Words.Tables
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public static PreferredWidth FromPercent(
	double percent
)

Parameters

percent
Type: SystemDouble
The value must be from 0 to 100.

Return Value

Type: PreferredWidth
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.doc");
Examples
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