Search code examples
c#rtf.net-rtf-writer

.Net RTF Writer: How can I add pre-formatted RTF text?


I am using this RTF Writer library to generate an RTF document. It allows me to add images, tables and text to meet the majority of my requirements.

However, I also want to add some text that is already in RTF format. This test needs to be added while keeping the formatting, which means I do not want to convert it to plain text first before adding it.

Currently, the formatted RTF includes all the document information, but I can remove that part using a RichTextBox to ensure that only the relevant part of the document is kept. This is the part I want to add to my generated document.

Here is an example of the pre-formatted RTF text:

{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\uc1\pard\f0\fs17 here is some RTF}

Normally, when we add text using .Net RTF Writer we can do the following:

var par = doc.addParagraph();
par.setText("Here is some text");

Is there something I can do to change this to allow adding the pre-formatted RTF text?


Side Note: If this is not possible, I am open to suggestions to alternate libraries (free for commercial use) that will allow me to do what I need. Obviously any suggestion for these are better suited as a comment rather than an answer.


Solution

  • It appears that this is not possible using the RTF Writer Library. It seems to have no support for loading, parsing or merging other RTF data.


    For what it's worth, my requirements have changed so I no longer need to do this. However, I was on the verge of making my own RTF parser and merging tool. Merging RTF is not as simple as copy/paste due to the way that the RTF format handles styles. Styles such as fonts and colours are defined in header tables which makes the merging more complicated.

    Anyone wanting to try something for themselves may find some useful information: Here, here and here.