Hi,
I am experiencing some very strange behaviour when trying to insert html into a content control, inside a Table Cell.
The first issue, which can be seen in Input1.docx, occurs when I have five content controls in five different cells. I attempt to replace the contents of the content controls with simple html ("<h1>Header 1</h1>"
). It does put “Header 1” in the table five times, but not in the locations where the placeholders were.
The second issue, when I prepend the second cell with some text (see Input2.docx) removes the Content Control from the middle cell and retults in two cells having the html inserted twice, when I would expect each of the five cells to have the html inserted once only.
There are event stranger results when I try to insert the html twice in succession (see commented code).
When I do this from Microsoft Word using Insert > Object > Text from file they all go in the right place.
Is there a work around as this is urgent.
Thanks,
I have manager to get around it by creating a new document, inserting the html into that, then using the InsertDocument method from here: https://docs.aspose.com/words/java/insert-and-append-documents/
Would be nice to be able to insert html though
Hi there,
Thanks for your inquiry. Please clone the paragraph before inserting it into contents control as shown in following code example to get the required output. You may create new instance of Paragraph in the foreach loop and insert it into content control. Hope this helps you.
var htmlString = "<h1>Header 1</h1>";
var doc = new Document(MyDir + @"Input1.docx");
var builder = new DocumentBuilder(doc);
var para = new Paragraph(doc);
var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (var sdt in sdts.Cast<StructuredDocumentTag>().ToList())
{
sdt.IsShowingPlaceholderText = false;
sdt.Multiline = true;
if (sdt.ParentNode.NodeType == NodeType.Paragraph)
{
para = (Paragraph)sdt.ParentNode;
para.RemoveAllChildren();
}
else
{
sdt.RemoveAllChildren();
sdt.AppendChild(para.Clone(true));
builder.MoveTo(sdt.FirstChild);
builder.InsertHtml(htmlString);
}
}
doc.Save(MyDir + "Out.docx");