Free Support Forum - aspose.com

How to know the total number columns of a table in a Word doc?

My question is a bit a strange.
I create a table in Word (eg: NxM) and this table has some cells merged with others (columnspan).
I would like to know the M value of the original table without columnspan.

When I use Aspose Words, every row only contains the number of cells that there are. But I would like to know the number of columns of the original Word table without columnspan.

Is there a way to know this information?

Thank you,
Samuel

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. This is by design (by Microsoft Word design). Rows in a table in a Microsoft Word document are completely independent. I mean each row can have any number of cells of any width.

As a workaround you can try using the following code to get maximum count of cells in the table row.

//Get Table

Table tab = doc.FirstSection.Body.Tables[0];

int columnCount = 0;

//Loop through all rows in table

foreach (Row row in tab.Rows)

{

if (row.Cells.Count > columnCount)

columnCount = row.Cells.Count;

}

Best regards.

Here, I show you a concrete case of my problem.

The initial table created in Word (2x6):
----------------------------------------------------
| | | | | | |
----------------------------------------------------
| | | | | | |
----------------------------------------------------

After have been merging cells in Word application I have this table.
----------------------------------------------------
| | | |
----------------------------------------------------
| | | | | |
----------------------------------------------------

When I try to know the initial number of columns with Aspose Words, I obtain 5, but really the original table has 6 columns.
This is the special case that I´m trying to resolve, the way to know the initial number of columns.

Thank you for your help,
Samuel


Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Actually the Word table has no concept of columns. So you can’t get column count. The Word table is a collection of independent rows that can have any number of cells of any width.

Best regards.

Ok, thank you again.

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your interesting question.

The only steady way to determine the total number of columns in a table is counting all column positions. Every row has RowFormat.LeftIndent property. Every cell has CellFormat.Width. Note that rows can be even unaligned on the left and right. So you should consider traversing all rows and all cells in rows storing left position of every cell. Left position of first cell in a row will be RowFormat.LeftIndent. Next one is greater by first cell’s CellFormat.Width. And so on. The last accumulated value should be the end of whole row – right side of the last cell. Since you need only to count different positions you can collect them all in one hash table with null values. After the loop is finished number of collected values will be number of columns plus one.

This idea is used inside Aspose.Words when we export tables to HTML or PDF. So it definitely works.

See these pages in documentation:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.tables.rowformatmembers.html

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.tables.cellformatmembers.html

Regards,

Thanks again, the Word tables will not be a problem anymore.

Thank you for your dedication,
Samuel