Free Support Forum - aspose.com

Insert Table at run-time

Hello there,

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…].

Thank you!

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. In your code, you try to insert a table into a paragraph, which is not allowed.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.documentbuilder.insertnode.html

You should insert your table after or before a paragraph. For example, see the following code:

Table objTable = new Table(builder.Document);

objTable.AppendChild(new Row(builder.Document));

builder.CurrentParagraph.ParentNode.InsertAfter(objTable, builder.CurrentParagraph);

Best regards.

Thanks for your help Alexey!

Now I am getting another error: Parent cannot be null.
Error is occurring at the line marked in red color.

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);

foreach (string imageCode in imageCodes)
{
Cell objCell = new Cell(builder.Document);
builder.MoveTo(objCell); // throws error

builder.InsertField(string.Format(“MERGEFIELD Image:Image{0}”,
imageCode), string.Format("«Image:Image{0}»", imageCode));
objTable.Rows[0].Cells.Add(objCell);
}
}

I want to insert the Mergefields inside table cells. Please guide.

Also, how can I set the table width equal to width of the page, so that all the cells in table row span equally across the page width.

Thank you!






Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.documentbuilder.moveto.html

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:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.tables.cell.ensureminimum.html )

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.

Best regards.

The issues you have found earlier (filed as WORDSNET-581) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(24)