Switch from percent to inches

Hi,

in Word, I can switch the table width from % to inches by a dialog click
and the prefered width in points is automatically calculated.

How can I do it with Aspose ?

Regards,

@Nachti You can auto fit the table with AutoFitBehavior.FixedColumnWidths to force Aspose.Words recalculate widths of table and cells in absolute units:

Document doc = new Document("C:\\Temp\\in.docx");

foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
    t.AutoFit(AutoFitBehavior.FixedColumnWidths);

doc.Save("C:\\Temp\\out.docx");

Sorry but it does not resolve the issue.
After applying your code, the preferred width is disabled but we want to change the width from percent to inches, not disable it. See here the examples: files.zip (380.9 KB)

Check the table property in each document for the table.

Regards,
@Nachti

@Nachti Do you mean preferred width of the table? As I can see after executing the code preferred widthы of individual cells are set properly. You can modify code like this to also set preferred width of the table:

Document doc = new Document("C:\\Temp\\in.docx");

foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{
    t.AutoFit(AutoFitBehavior.FixedColumnWidths);

    // Calculate widht of the table.
    double tableWidth = 0;
    foreach (Cell c in t.FirstRow.Cells)
        tableWidth += c.CellFormat.PreferredWidth.Value;

    // Set prefered width of the table.
    t.PreferredWidth = PreferredWidth.FromPoints(tableWidth);
}

doc.Save("C:\\Temp\\out.docx");

@alexey.noskov

Great. I want to avoid unnecessary work.
How can I determine if table is set already to PreferredWidth in inch ?

Kind regards,
@Nachti

@Nachti You can check the PreferredWidth.Type:

if (table.PreferredWidth.Type == PreferredWidthType.Points)
{
    // Table have preferred with in absolute units.
    // ....
}

Hi @alexey.noskov,

thanks. That works well.

How can I calculate the exact row height when ‘Specifiy height’ was disabled before ?

rowFormat.Height = ?;
rowFormat.HeightRule = HeightRule.Exactly;

In Word, I can simply enable ‘Specifiy height’ and the height is calcualted automatically.

Kind regards,
@Nachti

@Nachti Rows height depends on the row content and is calculated by MS Word on the fly. You can use LayoutCollector and LayoutEnumerator classes to calculate actual row height. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Get the table.
Table table = doc.FirstSection.Body.Tables[0];

// Calculate actual bounds of the first row.
enumerator.Current = collector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
// Move enumerator to the row.
while (enumerator.Type != LayoutEntityType.Row)
    enumerator.MoveParent();

Console.WriteLine(enumerator.Rectangle);