We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert RTF string into document using C#

Hello, I’m currently evaluating your Words server product. I have a legacy system where snippets of preformatted paragraphs are saved to the database in rtf format. The existing system using ms word ole to replace placeholder tags with the preformatted rtf snippets (i.e. fonts alignment, etc). For example:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fnil\fcharset2 Symbol;}}

\b\fs24CHEM-3 CHEMICAL SAFETY (I)\b0\par

The Material Safety Data Sheets of chemicals currently used in the workplace should be reviewed to ensure that proper safety measures and personal protective equipment are being used. If not, steps should be taken to follow outlines set forth by the manufacturer of the chemicals.

}

Range.Replace throws the special characters not allow exception and placing the snippet in a datatable and merging with mergefields just shows the rtf formatting as opposed to recogizing it as rtf markup and applying the markup to the text.

With over 200 existing word templates that use this approach for dynamically creating documents, I need a way of inserting the rtf encoded text and having it honored as ms word does. Preferably doing the Range.Replace.

Any suggestions?

thanks, Mark

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.

https://apireference.aspose.com/words/net/aspose.words/documentbuilder/methods/insertdocument

I hope this could help you.

Best regards.

thanks. that works