2009. 11. 6. 13:15
.NET Framework
다음은 RichTextBox의 Text를 HTML로 변환하는 함수의 코드입니다.
이 소스코드 역시 해당 함수를 사용해서 HTML로 변환하였습니다.
출처 : http://blog.jeidee.net/661
이 소스코드 역시 해당 함수를 사용해서 HTML로 변환하였습니다.
public string ConvertRtbToHTML(RichTextBox rtb)
{
if (rtb.Text.Length == 0) return null;
string html = "";
string color;
string strChar;
bool isBold;
bool isItalic;
string fontName;
short size;
int originStart = 0;
int originLength = rtb.Text.Length;
int count =0;
rtb.Select(0, 1);
color = ColorTranslator.ToHtml(rtb.SelectionColor);
isBold = rtb.SelectionFont.Bold;
isItalic = rtb.SelectionFont.Italic;
fontName = rtb.SelectionFont.FontFamily.Name;
size = (short)rtb.SelectionFont.Size;
// Include first 'style' parameters in the HTML
html += "<span style=\"font-family: " + fontName +
"; font-size: " + size + "pt; color: "
+ color + "\">";
// Include bold tag, if required
if (isBold == true)
html += "<b>";
// Include italic tag, if required
if (isItalic == true)
html += "<i>";
// Finally, add our first character
html += rtb.Text.Substring(0, 1);
// Loop around all remaining characters
for(count = 2 ;count <= rtb.Text.Length;count++)
{
// Select current character
rtb.Select(count - 1, 1);
strChar = rtb.Text.Substring(count - 1, 1);
switch (strChar)
{
case "\n":
// If this is a line break, add HTML tag
html += "<br>";
break;
case "\t":
html += " ";
strChar = "";
break;
case " ":
html += " ";
strChar = "";
break;
case "<":
html += "<";
strChar = "";
break;
case ">":
html += ">";
strChar = "";
break;
case "&":
html += "&";
strChar = "";
break;
}
// Check/implement any changes in style
if (rtb.SelectionColor.ToKnownColor().ToString() != color ||
rtb.SelectionFont.FontFamily.Name != fontName || rtb.SelectionFont.Size != size)
html += "</span><span style=\"font-family: "
+ rtb.SelectionFont.FontFamily.Name +
"; font-size: " + rtb.SelectionFont.Size +
"pt; color: " +
rtb.SelectionColor.ToKnownColor().ToString() + "\">";
// Check for bold changes
if (rtb.SelectionFont.Bold != isBold)
{
if (rtb.SelectionFont.Bold == false)
html += "</b>";
else
html += "<b>";
}
// Check for italic changes
if (rtb.SelectionFont.Italic != isItalic)
{
if (rtb.SelectionFont.Italic == false)
html += "</i>";
else
html += "<i>";
}
// Add the actual character
html += strChar; //box.Text.Substring(intCount-1, 1);
// Update variables with current style
color = rtb.SelectionColor.ToKnownColor().ToString();
isBold = rtb.SelectionFont.Bold;
isItalic = rtb.SelectionFont.Italic;
fontName = rtb.SelectionFont.FontFamily.Name;
size = (short)rtb.SelectionFont.Size;
}
// Close off any open bold/italic tags
if (isBold == true) html += "";
if (isItalic == true) html += "";
// Terminate outstanding HTML tags
html += "</span>"; //</html>";
// Restore original RichTextBox selection
rtb.Select(originStart, originLength);
//' Return HTML
return html;
}
{
if (rtb.Text.Length == 0) return null;
string html = "";
string color;
string strChar;
bool isBold;
bool isItalic;
string fontName;
short size;
int originStart = 0;
int originLength = rtb.Text.Length;
int count =0;
rtb.Select(0, 1);
color = ColorTranslator.ToHtml(rtb.SelectionColor);
isBold = rtb.SelectionFont.Bold;
isItalic = rtb.SelectionFont.Italic;
fontName = rtb.SelectionFont.FontFamily.Name;
size = (short)rtb.SelectionFont.Size;
// Include first 'style' parameters in the HTML
html += "<span style=\"font-family: " + fontName +
"; font-size: " + size + "pt; color: "
+ color + "\">";
// Include bold tag, if required
if (isBold == true)
html += "<b>";
// Include italic tag, if required
if (isItalic == true)
html += "<i>";
// Finally, add our first character
html += rtb.Text.Substring(0, 1);
// Loop around all remaining characters
for(count = 2 ;count <= rtb.Text.Length;count++)
{
// Select current character
rtb.Select(count - 1, 1);
strChar = rtb.Text.Substring(count - 1, 1);
switch (strChar)
{
case "\n":
// If this is a line break, add HTML tag
html += "<br>";
break;
case "\t":
html += " ";
strChar = "";
break;
case " ":
html += " ";
strChar = "";
break;
case "<":
html += "<";
strChar = "";
break;
case ">":
html += ">";
strChar = "";
break;
case "&":
html += "&";
strChar = "";
break;
}
// Check/implement any changes in style
if (rtb.SelectionColor.ToKnownColor().ToString() != color ||
rtb.SelectionFont.FontFamily.Name != fontName || rtb.SelectionFont.Size != size)
html += "</span><span style=\"font-family: "
+ rtb.SelectionFont.FontFamily.Name +
"; font-size: " + rtb.SelectionFont.Size +
"pt; color: " +
rtb.SelectionColor.ToKnownColor().ToString() + "\">";
// Check for bold changes
if (rtb.SelectionFont.Bold != isBold)
{
if (rtb.SelectionFont.Bold == false)
html += "</b>";
else
html += "<b>";
}
// Check for italic changes
if (rtb.SelectionFont.Italic != isItalic)
{
if (rtb.SelectionFont.Italic == false)
html += "</i>";
else
html += "<i>";
}
// Add the actual character
html += strChar; //box.Text.Substring(intCount-1, 1);
// Update variables with current style
color = rtb.SelectionColor.ToKnownColor().ToString();
isBold = rtb.SelectionFont.Bold;
isItalic = rtb.SelectionFont.Italic;
fontName = rtb.SelectionFont.FontFamily.Name;
size = (short)rtb.SelectionFont.Size;
}
// Close off any open bold/italic tags
if (isBold == true) html += "";
if (isItalic == true) html += "";
// Terminate outstanding HTML tags
html += "</span>"; //</html>";
// Restore original RichTextBox selection
rtb.Select(originStart, originLength);
//' Return HTML
return html;
}
출처 : http://blog.jeidee.net/661