I want to use DocumentBuilder to add some HTML in these numbers. So I use MoveToMergeField to move the builder to these nodes. But when I use InsertHtml the inserted html goes into the next paragraph as it has paragraph tags in it (text
). I don’t want it to go to the next paragraph. Is there a way to do it without modifying the HTML, I can’t change the HTML in any case?
Thanks in advance
Hi
Thanks for your request. I think, in your case, you can insert your HTML into a separate paragraph, then merge all generated paragraph into one and then copy content form the temporary document into your document. I created a simple code example for you:
[Test]
public void Test001()
{
Document doc = new Document(@"Test001\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move DocumentBuilder to mergefield
builder.MoveToMergeField("test");
// Insert HTML.
InsertHtml(builder, "<p>This is the first paragraph</p><p>This is another paragraph</p>");
doc.Save(@"Test001\out.doc");
}
private static void InsertHtml(DocumentBuilder builder, string html)
{
// Create a temporary ocumentBuidler.
DocumentBuilder tmpBuilder = new DocumentBuilder();
tmpBuilder.InsertHtml(html);
// Merge content of all paragraphs in the temp document into one.
Paragraph firstParagraph = tmpBuilder.Document.FirstSection.Body.FirstParagraph;
while (firstParagraph.NextSibling != null && firstParagraph.NextSibling.NodeType == NodeType.Paragraph)
{
Paragraph nextParagraph = (Paragraph) firstParagraph.NextSibling;
while (nextParagraph.HasChildNodes)
firstParagraph.AppendChild(nextParagraph.FirstChild);
nextParagraph.Remove();
}
// Insert content of the first paragraph of temp document into the destination document.
foreach(Node childNode in firstParagraph.ChildNodes)
builder.InsertNode(builder.Document.ImportNode(childNode, true, ImportFormatMode.KeepSourceFormatting));
}
Hope this helps.
Best regards,