How to set 'Distribute Rows' option of Table using .NET

I have table with many lines where every first columns are vertically merged by 3 lines. In short:

Title
1 2 3
Title
with
some
more
data
1 2 3

I need to make all lines equal height.

How can I get height of actual row height in Aspose? Result should be:

Title
1 2 3
Title
with
some
more
data
1 2 3

Hi there,

Thanks for your inquiry.
The height of a table row is controlled using height and height rule
properties. These can be set differently for each row in the table which
allows for wide control over the height of each row. Please read
following documentation link for your kind reference.
https://docs.aspose.com/words/net/applying-formatting/

Please let us know if you have any more queries.

Problem is, that I need to set “RowFormat.HeightRule” to “Exactly” and set height equal to each row.

Idea was - at first I set “Auto”, then chech the height of each row. Then take maximum and set it to each row with setting “Exactly”

Problem is, that it does not matter if row has 100 text lines - it still says 5 pixel (the initial height, with setting “Auto”) in backend code. If I open it in word later - height is much bigger than 5 px.

Is there any way to find real (calculated, expanded by text) height of the row/cell?

Hi there,

Thanks for your inquiry. Your requirement is about MS Word’s feature ‘Distribute Rows Evenly’. Unfortunately, Aspose.Words does not support
the requested feature at the moment. However, we have already logged this
feature request as WORDSNET-10671 in our issue tracking system.
You will be notified via this forum thread once this feature is
available.

Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "example.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int i = 1;
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    foreach (Row row in table.Rows)
    {
        row.RowFormat.HeightRule = HeightRule.Auto;
        row.FirstCell.LastParagraph.AppendChild(new Run(doc));
        builder.MoveTo(row.FirstCell.FirstChild);
        builder.StartBookmark("Bookmark" + i);
        builder.MoveTo(row.FirstCell.LastParagraph.LastChild);
        builder.EndBookmark("Bookmark" + i);
        i++;
    }
}
LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Double height = 1.0;
Double rowheight = 1.0;
var collection = doc.GetChildNodes(NodeType.Row, true);
foreach (Row row in collection)
{
    BookmarkStart bmStart = (BookmarkStart)row.FirstCell.GetChild(NodeType.BookmarkStart, 0, true);
    if (bmStart == null)
        continue;
    var renderObject = layoutCollector.GetEntity(bmStart);
    layoutEnumerator.Current = renderObject;
    RectangleF location1 = layoutEnumerator.Rectangle;
    renderObject = layoutCollector.GetEntity(bmStart.Bookmark.BookmarkEnd);
    layoutEnumerator.Current = renderObject;
    RectangleF location2 = layoutEnumerator.Rectangle;
    rowheight = location2.Y - location1.Y;
    if (rowheight > height)
        height = rowheight;
    else if (rowheight == 0)
        height = location1.Height;
}
foreach (Row row in collection)
{
    row.RowFormat.HeightRule = HeightRule.AtLeast;
    row.RowFormat.Height = height;
}
doc.Save(MyDir + "Out.docx");

It would be exactly what I need, but this does not work:

BookmarkStart bmStart = (BookmarkStart)row.FirstCell.GetChild(NodeType.BookmarkStart, 0, true);

this one always returns null. Here is example, which recreates similar sitution:

insertDemo();
MakeTableRowsEqual();
internal void insertDemo()
{
    Document document = Document;
    DocumentBuilder builder = new DocumentBuilder(document);

    Aspose.Words.Tables.Table table = builder.StartTable();

    //first row
    var cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(68);
    builder.Write("Very short text");

    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(10);

    builder.EndRow();

    //second row
    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(68);

    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(10);

    builder.EndRow();

    //third row
    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.First;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(68);
    builder.Write("Nulla facilisi. Fusce feugiat condimentum finibus. Proin ut velit eget nisi fringilla volutpat. Vestibulum et tortor ultrices, commodo dui eget, rhoncus velit. Nulla facilisi. Maecenas justo urna, finibus nec nisl id, congue tempus velit. Nam scelerisque quis massa vitae dignissim.");

    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(10);

    builder.EndRow();

    //fourth row
    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.Previous;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(68);

    cell = builder.InsertCell();
    cell.CellFormat.ClearFormatting();
    cell.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
    cell.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(10);

    builder.EndRow();

    builder.EndTable();
}

internal void MakeTableRowsEqual()
{
    Document doc = Document;
    DocumentBuilder builder = new DocumentBuilder(doc);
    int i = 1;

    foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
    {
        foreach (Row row in table.Rows)
        {
            row.RowFormat.HeightRule = HeightRule.Auto;
            row.FirstCell.LastParagraph.AppendChild(new Run(doc));
            builder.MoveTo(row.FirstCell.FirstChild);
            builder.StartBookmark("Bookmark" + i);
            builder.MoveTo(row.FirstCell.LastParagraph.LastChild);
            builder.EndBookmark("Bookmark" + i);
            i++;
        }
    }

    LayoutCollector layoutCollector = new LayoutCollector(doc);
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

    Double height = 1.0;
    Double rowheight = 1.0;
    var collection = doc.GetChildNodes(NodeType.Row, true);

    foreach (Row row in collection)
    {
        BookmarkStart bmStart = (BookmarkStart)row.FirstCell.GetChild(NodeType.BookmarkStart, 0, true);
        if (bmStart == null)
            continue;
        var renderObject = layoutCollector.GetEntity(bmStart);
        layoutEnumerator.Current = renderObject;

        RectangleF location1 = layoutEnumerator.Rectangle;

        renderObject = layoutCollector.GetEntity(bmStart.Bookmark.BookmarkEnd);
        layoutEnumerator.Current = renderObject;

        RectangleF location2 = layoutEnumerator.Rectangle;
        rowheight = location2.Y - location1.Y;

        if (rowheight > height)
            height = rowheight;
        else if (rowheight == 0)
            height = location1.Height;
    }

    foreach (Row row in collection)
    {
        row.RowFormat.HeightRule = HeightRule.AtLeast;
        row.RowFormat.Height = height;
    }
}

Hi,
Shared code was just a workaround. This feature will be available as soon as the issue WORDSNET-10671 is fixed. We are sorry for the inconvenience.
Best Regards,

Ok, thanks.

I see there was similar message on 2014-08. Is there posibility to get that feature in the nearest month? This is required in the report, which will be released soon

Hi,
This feature was postponed because there was only one request for this feature and our development team had more demanded features and issues on higher priority.
A request to share the ETA of this feature has been forwarded to our development team. We will update you as soon as our development team share the ETA.
Best Regards,

A post was split to a new topic: Get exact row height of table