exempel på användning
------------------------------
Compona.Text.RTFBuilder r=new Compona.Text.RTFBuilder ();
r.Append ("hej");
r.Bold = true;
r.Italic =true;
r.BackColor = Color.Magenta;
r.FontName ="Times New Roman";
r.Append (" du glade");
r.AppendHyperlink (" testa testa","http://www.compona.com");
r.Bold =false;
r.Append (" tag en spade");
r.BackColor = Color.Transparent;
r.Append ("\n och gräv ner");
r.ForeColor =Color.Red;
r.FontName ="courier";
r.FontSize = 55;
r.Append (" dig");
r.ForeColor = Color.Green;
r.Italic =false;
r.Underline =true;
r.FontSize = 22;
r.Append (" WAZAAAHAHAHAHHH!!!");
this.richTextBox1.Rtf = r.GetRTF ();
Själva klassen
------------------------------
using System;
using System.Drawing;
using System.Text;
using System.Collections;
namespace Compona.Text
{
public class Word
{
public string Text="";
public Color ForeColor =Color.Black;
public Color BackColor =Color.Transparent;
public bool Bold =false;
public bool Italic =false;
public bool Underline =false;
public bool Strikeout =false;
public float FontSize =0;
public string FontName ="";
}
public class Hyperlink:Word
{
public string URL="";
}
public class ImageObject:Word
{
//to be implemented
public System.Drawing.Image Image=null;
}
public class RTFBuilder
{
private ArrayList words=new ArrayList ();
private Hashtable colors=new Hashtable ();
private Hashtable fonts=new Hashtable ();
public RTFBuilder()
{
}
public void Append(string Text)
{
Word word=new Word ();
this.words.Add (word);
word.Text =Text;
UpdateCurrent();
}
public void AppendHyperlink(string Text,string URL)
{
Hyperlink word=new Hyperlink ();
this.words.Add (word);
word.Text =Text;
word.URL =URL;
UpdateCurrent();
}
public void AppendHyperlink(string Text,string URL,bool FormatAsLink)
{
Hyperlink word=new Hyperlink ();
this.words.Add (word);
word.Text =Text;
word.URL =URL;
UpdateCurrent();
}
private void UpdateCurrent()
{
Word word=words [words.Count-1] as Word;
word.Bold =this.Bold;
word.Italic =this.Italic;
word.Underline =this.Underline;
word.Strikeout =this.Strikeout;
word.ForeColor =this.ForeColor;
word.BackColor =this.BackColor;
word.FontSize =this.FontSize;
word.FontName =this.FontName;
}
private bool _Bold;
public bool Bold
{
get
{
return _Bold;
}
set
{
_Bold = value;
}
}
private bool _Italic;
public bool Italic
{
get
{
return _Italic;
}
set
{
_Italic = value;
}
}
private bool _Underline;
public bool Underline
{
get
{
return _Underline;
}
set
{
_Underline = value;
}
}
private bool _Strikeout;
public bool Strikeout
{
get
{
return _Strikeout;
}
set
{
_Strikeout = value;
}
}
private Color _ForeColor;
public Color ForeColor
{
get
{
return _ForeColor;
}
set
{
_ForeColor = value;
}
}
private Color _BackColor;
public Color BackColor
{
get
{
return _BackColor;
}
set
{
_BackColor = value;
}
}
private float _FontSize=22f;
public float FontSize
{
get
{
return _FontSize;
}
set
{
_FontSize = value;
}
}
private string _FontName="arial";
public string FontName
{
get
{
return _FontName;
}
set
{
_FontName = value;
}
}
public string GetRTF()
{
//build color and font lookup
foreach(Word word in words)
{
if (word.BackColor != Color.Empty)
colors[word.BackColor]=123;
if (word.ForeColor != Color.Empty)
colors[word.ForeColor]=123;
fonts[word.FontName]=123;
}
ArrayList colorlookup=new ArrayList ();
foreach(Color color in colors.Keys )
colorlookup.Add (color);
ArrayList fontlookup=new ArrayList ();
foreach(string font in fonts.Keys )
fontlookup.Add (font);
//end build color and font lookup
//build header
System.Text.StringBuilder sb=new System.Text.StringBuilder();
sb.Append (@"{\rtf1\ansi\ansicpg1252\deff0\deflang1053");
//build font table
sb.Append (@"{\fonttbl");
int i=0;
foreach (string font in fonts.Keys)
{
sb.Append (@"{\f"+ i.ToString ()+@"\fmodern\fprq1\fcharset0 " + font + @";}");
i++;
}
sb.Append (@"}");
sb.Append (@"{\colortbl ;");
foreach (Color c in colors.Keys)
{
sb.AppendFormat("\\red{0}\\green{1}\\blue{2};",c.R,c.G,c.B);
}
sb.Append (@";}");
sb.Append (@"\viewkind4\uc1\pard\f0\fs20");
//end build header
//build textbody
foreach (Word word in words)
{
if (word is Hyperlink)
{
Hyperlink linkword=word as Hyperlink;
int clrindexfg = colorlookup.IndexOf (word.ForeColor);
clrindexfg ++;
int clrindexbg = colorlookup.IndexOf (word.BackColor);
clrindexbg ++;
int fontindex = fontlookup.IndexOf (word.FontName);
sb.Append (@"{\field{\*\fldinst{HYPERLINK " +"\"" + linkword.URL +"\"" +@"}}{\fldrslt");
sb.Append ("{");
//apply font name
sb.Append ("\\f" + fontindex.ToString ());
//apply font size
sb.Append ("\\fs" + word.FontSize);
//apply fg color
sb.Append ("\\cf" + clrindexfg.ToString ());
//apply bgcolor
if (word.BackColor != Color.Empty)
{
sb.Append ("\\highlight" + clrindexbg.ToString ());
}
sb.Append (" ");
if (word.Bold)
sb.Append (@"\b ");
if (word.Underline)
sb.Append (@"\ul ");
if (word.Italic)
sb.Append (@"\i ");
string wordtext=word.Text;
//apply escaping
wordtext=wordtext.Replace (@"\",@"\\").Replace (@"}",@"\}").Replace (@"{",@"\{");
wordtext=wordtext.Replace ("\n",@"\par");
sb.Append (wordtext);
if (word.Bold)
sb.Append (@"\b0 ");
if (word.Underline)
sb.Append (@"\ul0 ");
if (word.Italic)
sb.Append (@"\i0 ");
sb.Append ("}");
sb.Append (@"}}"+Environment.NewLine);
}
else
{
#region standard text
int clrindexfg = colorlookup.IndexOf (word.ForeColor);
clrindexfg ++;
int clrindexbg = colorlookup.IndexOf (word.BackColor);
clrindexbg ++;
int fontindex = fontlookup.IndexOf (word.FontName);
sb.Append ("{");
//apply font name
sb.Append ("\\f" + fontindex.ToString ());
//apply font size
sb.Append ("\\fs" + word.FontSize);
//apply fg color
sb.Append ("\\cf" + clrindexfg.ToString ());
//apply bgcolor
if (word.BackColor != Color.Empty)
{
sb.Append ("\\highlight" + clrindexbg.ToString ());
}
sb.Append (" ");
if (word.Bold)
sb.Append (@"\b ");
if (word.Underline)
sb.Append (@"\ul ");
if (word.Italic)
sb.Append (@"\i ");
string wordtext=word.Text;
//apply escaping
wordtext=wordtext.Replace (@"\",@"\\").Replace (@"}",@"\}").Replace (@"{",@"\{");
wordtext=wordtext.Replace ("\n",@"\par");
sb.Append (wordtext);
if (word.Bold)
sb.Append (@"\b0 ");
if (word.Underline)
sb.Append (@"\ul0 ");
if (word.Italic)
sb.Append (@"\i0 ");
sb.Append ("}"+Environment.NewLine);
#endregion
}
}
sb.Append ("}"); //end rtf
return sb.ToString ();
}
}
}