I have a code like following in my source template: <span style="font-size: 11pt; font-family: "Calibri","sans-serif"; color: rgb(31, 73, 125);">[PHOTOTABLE: A, B, C, D]
While doing mail merge, I need to replace this code with a 1 row table of Images and this row may have a minimum of 2 photos, or a maximum of 4.
For examples, please see the attached document.
I was trying to insert table at run time as:
// Regex regex = new Regex(@"[(?\S+)]", RegexOptions.IgnoreCase);
// objDocument.Range.Replace(regex, new ReplaceEvaluator(GetReplaceEvaluator), false);
//In GetReplaceEvaluator()
Node currentNode = e.MatchNode;
builder.MoveTo(currentNode);
if (name.StartsWith("PHOTOTABLE:"))
{
string[] imageCodes = name.Substring(11).Split(new char[] {','});
Table objTable = new Table(builder.Document);
objTable.AppendChild(new Row(builder.Document));
builder.InsertNode(objTable); // throws error
foreach (string imageCode in imageCodes)
{
Cell objCell = new Cell(builder.Document);
builder.MoveTo(objCell);
builder.InsertField(string.Format("MERGEFIELD Image:Image{0}", imageCode), string.Format("«Image:Image{0}»", imageCode));
objTable.Rows[0].Cells.Add(objCell);
}
}
But, when I try insertng table like this, error occurs: {“Cannot insert a node of this type at this location.”}
Probably I am missing something.
Please guide me the way to insert table at run-time like above or any other method to replace this placeholder [PHOTOTABLE…].
Thanks for your inquiry. You can move DocumentBuilder cursor only to the node, which is already inserted into the document. In your code, you try to move DocumentBuilder cursor to the node, which hangs in the air.
You should first insert your cell into the table, which is already inserted into the document and only then, you will be able to move document builder to it.
Also, if you look into the documentation, you will see that you can use DocuemntBuilder.MoveTo method to move cursor to an inline node or to the end of a paragraph, but in your case you try to move cursor to a cell.
So you need to insert a paragraph in this cell and them move cursor to this paragraph. (You can use Cell.EnsureMinimum method to insert paragraph into an empty cell:
Currently, there is no way to specify width of a whole table; you can only specify width of a particular cell only. Your request has been linked to the appropriate issue. You will be notified as soon as this feature is available.