Hi
Thanks for your request. I think that you can use ReplaceEvaluator to achieve this. You can’t directly insert rtf string into the document. But you can insert document into another document. So you can try using the following code snippet.
public void Test108()
{
//Open template document
Document doc = new Document(@"Test108\in.doc");
//Create regex
Regex regex = new Regex("stringToReplace");
//Replace placeholder with rtf using ReplaceEvaluator
doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceEvaluator108), false);
//Save output document
doc.Save(@"Test108\out.doc");
}
private ReplaceAction ReplaceEvaluator108(object sender, ReplaceEvaluatorArgs e)
{
//Get MatchNode
Paragraph parentParagraph = e.MatchNode.ParentNode as Paragraph;
//Read RTF string from file. in your case you get this string from database
string rtfString = File.ReadAllText(@"Test108\test.txt");
//Get bytes from string, this is needed to create MemoryStream
byte[] rtfBytes = Encoding.UTF8.GetBytes(rtfString);
//Create memorystream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
//Create document from stream
Document rtfDoc = new Document(rtfStream);
//Insert rtd document into destination document
InsertDocument(parentParagraph, rtfDoc);
//Return Replace action (remove palaceholder)
return ReplaceAction.Replace;
}
Insert document method you can find here.
http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/insert-a-document-into-another-document.html
I hope this could help you.
Best regards.
Alexey Noskov
Developer/Technical Support
Aspose Auckland Team