Hi, Aspose Team,
I am inserting HTML snippets into an RTF template using the InsertDocument() code I found on this site. My doc could have:
“Some text in Arial 11 point… blah blah #address# More stuff in Arial 11”
The HTML is very simple:
123 main street
Then I replace #address# with the html.
After the replacement, the resulting RTF is all in Times Roman!
I will paste the code below.
Thanks in Advance!
Rich
public void InsertDocument(Node insertAfterNode, Document srcDoc, ParagraphAlignment alignment)
{
// Make sure that the node is either a pargraph or table.
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
(!insertAfterNode.NodeType.Equals(NodeType.Table)))
throw new ArgumentException("The destination node should be either a paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
ImportFormatMode.UseDestinationStyles);
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Let’s skip the node if it is a last empty paragarph in a section.
if (srcNode.NodeType.Equals(NodeType.Paragraph))
{
Paragraph para = (Paragraph)srcNode;
if (para.IsEndOfSection && !para.HasChildNodes)
continue;
}
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
if (newNode.GetType() == typeof(Paragraph))
{
((Paragraph)newNode).ParagraphFormat.Alignment = alignment;
}
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}