2010년 3월 8일 월요일

RichTextBox의 Text를 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 += "";
    // Include bold tag, if required
    if (isBold == true)
        html += "";
    
    // Include italic tag, if required
    if (isItalic == true)
        html += "";
    
    // 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 += "";
                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 += "";
    
        // Check for bold changes
        if (rtb.SelectionFont.Bold != isBold)
        {
           if (rtb.SelectionFont.Bold == false)
               html += "";
           else
               html += "";
        }

        // Check for italic changes
        if (rtb.SelectionFont.Italic != isItalic)
        {
               if (rtb.SelectionFont.Italic == false)
                   html += "";
               else
                   html += "";
        }

        // 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 += ""; //";
    // Restore original RichTextBox selection
    rtb.Select(originStart, originLength);
    
    //' Return HTML
    return html;
}

댓글 없음:

댓글 쓰기