Wrong Cell Rectangle Coordinates/ Aspose Word

sc.jpg (671.8 KB)

As it can be noticed in the image above, the width coordinates are the same for each cell in the word document, when it shouldn’t be as different columns have different width. Is there anything wrong with my code (see image), or it’s something that has to do with Aspose.

Also how can I retrieve the Cell coordinates, not the FirstParagraph as I have done in my code. Is there a way or a workaround?

Link to image -> https://freeimage.host/i/drnv5b

@marin.collaku You can use code like the following to achieve what you need:

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

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

// Get coordinates of cells in the table.
foreach (Row r in table.Rows)
{
    foreach (Cell c in r.Cells)
    {
        // Move LayoutEnumerator to cell entity.
        enumerator.Current = collector.GetEntity(c.FirstParagraph);
        while (enumerator.Type != LayoutEntityType.Cell)
            enumerator.MoveParent();

        Console.WriteLine(enumerator.Rectangle);

        // To make sure cells bounds are calculated properly draw rectangles on top pf the table.
        // NOTE: This is a simple example for demonstration purposes only.
        Shape rect = new Shape(doc, ShapeType.Rectangle);
        rect.WrapType = WrapType.None;
        rect.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        rect.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        rect.Top = enumerator.Rectangle.Top;
        rect.Left = enumerator.Rectangle.Left;
        rect.Width = enumerator.Rectangle.Width;
        rect.Height = enumerator.Rectangle.Height;
        rect.Stroke.Color = Color.Red;
        rect.Stroke.Weight = 2;

        doc.FirstSection.Body.LastParagraph.AppendChild(rect);
    }
}

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

in.docx (12.6 KB)
out.docx (10.3 KB)

1 Like

Thank you! It worked for me

1 Like