views:

341

answers:

1

We're using Microsoft's RichTextBox to manipulate RTF text that we obtain from a 3rd party database. Typical actions we do are :

  1. Add additional text to the top of the RTF document
  2. Add additional Text to the bottom of the RTFdocument
  3. 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;
 }
+1  A: 

A RichTextBox on ASP.NET?

I am seriously confused and/or worried.

leppie
Well, this is part of a library that internally uses RichTextBox . Yes, its a windows forms control, but do you know of a better way of manipulating RTF without actually manipulating RTF tags? We used that approach before but ended up with endless bugs! This approach has worked very well for us.
Sprash