Free Support Forum - aspose.com

Getting the row that a bookmark/form field is contained in

Is there any way a function could be added to retrieve the row number in which a particular bookmark or form field is contained?

Yes. Here is an example:

private void TestGetBookmarkRow()

{

string filename = Application.StartupPath + @"\Doc1.doc";

Document doc = new Document(filename);

string bookmarkName = "ff1";

int rownum = GetBookmarkRow(doc, bookmarkName);

MessageBox.Show(String.Format("Bookmark {0} belongs to row #{1}", bookmarkName, rownum));

}

// Returns -1 if bookmark not found or bookmark start node does not belong to a row.

private int GetBookmarkRow(Document doc, string bookmarkName)

{

Bookmark bookmark = doc.Range.Bookmarks[bookmarkName];

if (bookmark==null) return -1;

Row row = (Row)doc.Range.Bookmarks[bookmarkName].BookmarkStart.GetAncestor(typeof(Aspose.Words.Row));

if (row==null) return -1;

return row.ParentTable.Rows.IndexOf(row);

}

Thank you! Big Smile [:D]