Free Support Forum - aspose.com

Getting Table- row and cell index

Hi,

We recently migrate our app to Aspose.Words.

We have a table with several rows, the first one is a header row, and on the second row, first col we have a formfield that in Ms Word, was used to place the cursor.

My question is: based on the formfield is it possible to know the table, row and cell index?

Problem: the end-user sometimes alters the templates. But always keeps the formfield. Só this is the best reference to the start column.

Thank you,

JP

Hi

Thanks for your request. Sure you can, you can use code like the following to achieve this:

// open source document.

Document doc = new Document(@"Test001\in.doc");

// Get formfield.

FormField field = doc.Range.FormFields["test"];

// Get table where the formfield is located.

Table table = (Table)field.GetAncestor(NodeType.Table);

// Get row whre it is located.

Row row = (Row)field.GetAncestor(NodeType.Row);

// And get cell.

Cell cell = (Cell)field.GetAncestor(NodeType.Cell);

// Get indexes of table, row and cell.

// table index withint the document.

int tableIndex = doc.GetChildNodes(NodeType.Table, true).IndexOf(table);

// Row index within the table.

int rowIndex = table.Rows.IndexOf(row);

// Cell index within the row.

int cellIndex = row.IndexOf(cell);

Hope this helps.

Best regards,