Free Support Forum -

Get cell position from cell ID

Is it possible to retrieve a cell position (index) if I know the cell ID?

I would like to know the position of a cell in a table or row, knowing the ID of the cell.

Does the cell have a property that keeps track of which position in a row it is placed at?

Dear dpouls1,

Thank you for considering Aspose.

In fact, you can access the cell directly if you know it’s ID. For example:
Cell cell1 = row1.Cells[“Cell1”];

If you want to get the index of the cell, you can use Cells.IndexOf method.

BTW, I will soon add a new method GetObjectByID in Pdf class. Please refer to thread How can I get identify an XML header node as an object in code?. Will this help you?

It helps me partially. The IndexOf only works if the cells have different names:

Dim CellA As Aspose.Pdf.Cell = CellTableRow.Cells.Add(“cell1”)
Dim CellB As Aspose.Pdf.Cell = CellTableRow.Cells.Add(“cell2”)
Dim CellC As Aspose.Pdf.Cell = CellTableRow.Cells.Add(“cell3”)
Dim a As Integer = CellTableRow.Cells.IndexOf(CellTableRow.Cells(“Text”))

I don’t know how many cells with be in the table, therefore I’ve build it as below.

Dim CellTableRow As Aspose.Pdf.Row = CellTable.Rows.Add()
Dim Cell1 As Aspose.Pdf.Cell = Nothing
’For each new cell:
Cell1 = CellTableRow.Cells.Add()
Cell1 = CellTableRow.Cells.Add()
Cell1 = CellTableRow.Cells.Add()
Cell1 = CellTableRow.Cells.Add()
Cell1 = CellTableRow.Cells.Add()

I need to know the index of the last Cell containing CellText, but the number of previous cells is variable.

Am I doing it wrong? Is there a more efficient or correct way to create / reference the cells?

Dear dpouls1,

Thank you for considering Aspose.

There are two solutions.

1. Define a ID for the Cell containing CellText. Then you can use CellTableRow.Cells[ID] to get the cell object and then get it index.

2. If you know the Cell containing CellText is the last cell in the row, then it’s index is CellTableRow.Cells.Count - 1.

That is exactly what I need.

Thanks - keep up the great work.