سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

You can create a new richtextbox, copy and paste into it the already existing document, followed by the preformatted text.

There is an alternative approach: generate HTML (and CSS, if needed) and convert it to RTF using PD4ML.NET. HTML code should be less confusing than RTF markup.

A good idea is to create an HTML template (with a logo, a footer etc) and populate it from your code with dynamic data. In the case you avoid a need to program an entire document layout and it also makes layout adjustments much simpler.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top