We're using Microsoft's RichTextBox to manipulate RTF text that we obtain from a 3rd party database. Typical actions we do are :
- Add additional text to the top of the RTF document
- Add additional Text to the bottom of the RTFdocument
- Merge 2 RTF documents.
We are noticing that the at the end of the operations the page margins are no longer the same as those obtained from the database.
An example of the code to add text to the top of the document is:
public static string AddHeader(string rtfDoc, string textToAdd)
{
rtfBoxNew = new RichTextBox();
rtfBoxNew.Rtf = (IsRTF(rtfDoc))? rtfDoc : TexttoRtf(rtfDoc);//IsRTF checks if the text is RTF,
rtfBoxNew.Select(0,0);
rtfBoxNew.SelectionProtected = false;
rtfBoxNew.SelectedText = textToAdd + "\n";
rtfBoxNew.SelectAll();
//RTFHelper SelectedRTF puts a null character
string result = rtfBoxNew.SelectedRtf.TrimEnd('\0');
return result;
}
public static string TexttoRtf(string text)
{
RichTextBox rtfTemp = null;
string result = string.Empty;
try
{
rtfTemp = new RichTextBox();
rtfTemp.Text = text;
rtfTemp.SelectAll();
using (Font monospacedFont = new Font("Courier New", 10))
{
rtfTemp.SelectionFont = monospacedFont;
}
result = rtfTemp.SelectedRtf.TrimEnd('\0');
}
catch()
{
//exception handling code
}
return result;
}