I’m using the following code to make sure that every Table in a document is set to Arial font.
foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
if (run.ParentParagraph.IsInCell)
{
run.Font.Name = "Arial";
}
}
This works fine.
However, I’ve now been asked to set the first 8 tables to 12pt and all the rest to 10pt. Is it possible to evaluate which table a Run belongs to? Or would there be a more efficient way of achieving this?
Hi
Thanks for your request. You can try inserting a Bookmark in a table, which you would like to find. Then you can use the following code to find the table:
Document doc = new Document("in.doc");
// Get bookmark
Bookmark bk = doc.Range.Bookmarks["bookmark"];
if (bk != null)
{
// Get table, where bookmark is located.
Node table = bk.BookmarkStart.GetAncestor(NodeType.Table);
if (table != null)
// Work with table.
}
Hi Mark,
Please see the code below which should achieve what you are looking for.
// All tables in the document.
NodeCollection tableNodes = doc.GetChildNodes(NodeType.Table, true);
foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
Table parentTable = (Table) run.GetAncestor(NodeType.Table);
// Find and compare the ID of the table which contains this run.
if (tableNodes.IndexOf(parentTable) <8)
run.Font.Size = 12;
else
run.Font.Size = 10;
}