如何在Windows 10 UWP中将HTML转换为RTF,反之亦然?

时间:2016-04-29 10:38:14

标签: c# windows-10-universal

我正在使用Windows 10 UWP中的RichEditBox Control来允许用户使用一些格式化选项。我从服务器获取HTML并且我想将其转换为RTF以在RichEditBox中显示它。同样,我想在用户完成编辑后再将其转换为HTML。我做了搜索,但我得到了样品,但他们没有帮助。我发现其中一个是为RichTextBlock工作,而不是RichEditBox

HTML to RTF

我也尝试过以下网址(RTF到HTML),但它没有提供正确的输出

RTF to HTML

请有人建议如何通过RichEditBox实现我的要求,因为它只是允许格式化选项的控件,如Bold,Italic,Bullet点等。

1 个答案:

答案 0 :(得分:1)

我找到了在this link中转换的基本概念,并创建了以下方法将rtf转换为html。它正确转换文本格式选项。

以下是将rtf转换为html的方法,您可以根据需要进行编辑

 public static string ConvertToHtml(RichEditBox richEditBox)
    {
        string text, strColour, strFntName, strHTML;
        richEditBox.Document.GetText(TextGetOptions.None, out text);
        ITextRange txtRange = richEditBox.Document.GetRange(0, text.Length);
        strHTML = "<html>";

        int lngOriginalStart = txtRange.StartPosition;
        int lngOriginalLength = txtRange.EndPosition;
        float shtSize = 11;

        // txtRange.SetRange(txtRange.StartPosition, txtRange.EndPosition);

        bool bOpened = false, liOpened = false, numbLiOpened = false, iOpened = false, uOpened = false, bulletOpened = false, numberingOpened = false;

        for (int i = 0; i < text.Length; i++)
        {
            txtRange.SetRange(i, i + 1);

            if (i == 0)
            {
                strColour = txtRange.CharacterFormat.ForegroundColor.ToString();
                shtSize = txtRange.CharacterFormat.Size;
                strFntName = txtRange.CharacterFormat.Name;
                strHTML += "<span style=\"font-family:" + strFntName + "; font-size: " + shtSize + "pt; color: #" + strColour.Substring(3) + "\">";
            }


            if (txtRange.CharacterFormat.Size != shtSize)
            {
                shtSize = txtRange.CharacterFormat.Size;
                strHTML += "</span><span style=\"font-family: " + txtRange.CharacterFormat.Name + "; font-size: " + txtRange.CharacterFormat.Size + "pt; color: #" + txtRange.CharacterFormat.ForegroundColor.ToString().Substring(3) + "\">";
            }

            if (txtRange.Character == Convert.ToChar(13))
            {
                strHTML += "<br/>";
            }

            #region bullet
            if (txtRange.ParagraphFormat.ListType == MarkerType.Bullet)
            {
                if (!bulletOpened)
                {
                    strHTML += "<ul>";
                    bulletOpened = true;
                }

                if (!liOpened)
                {
                    strHTML += "<li>";
                    liOpened = true;
                }

                if (txtRange.Character == Convert.ToChar(13))
                {
                    strHTML += "</li>";
                    liOpened = false;
                }
            }
            else
            {
                if (bulletOpened)
                {
                    strHTML += "</ul>";
                    bulletOpened = false;
                }
            }

            #endregion

            #region numbering
            if (txtRange.ParagraphFormat.ListType == MarkerType.LowercaseRoman)
            {
                if (!numberingOpened)
                {
                    strHTML += "<ol type=\"i\">";
                    numberingOpened = true;
                }

                if (!numbLiOpened)
                {
                    strHTML += "<li>";
                    numbLiOpened = true;
                }

                if (txtRange.Character == Convert.ToChar(13))
                {
                    strHTML += "</li>";
                    numbLiOpened = false;
                }
            }
            else
            {
                if (numberingOpened)
                {
                    strHTML += "</ol>";
                    numberingOpened = false;
                }
            }

            #endregion

            #region bold
            if (txtRange.CharacterFormat.Bold == FormatEffect.On)
            {
                if (!bOpened)
                {
                    strHTML += "<b>";
                    bOpened = true;
                }
            }
            else
            {
                if (bOpened)
                {
                    strHTML += "</b>";
                    bOpened = false;
                }
            }
            #endregion

            #region italic
            if (txtRange.CharacterFormat.Italic == FormatEffect.On)
            {
                if (!iOpened)
                {
                    strHTML += "<i>";
                    iOpened = true;
                }
            }
            else
            {
                if (iOpened)
                {
                    strHTML += "</i>";
                    iOpened = false;
                }
            }
            #endregion

            #region underline
            if (txtRange.CharacterFormat.Underline == UnderlineType.Single)
            {
                if (!uOpened)
                {
                    strHTML += "<u>";
                    uOpened = true;
                }
            }
            else
            {
                if (uOpened)
                {
                    strHTML += "</u>";
                    uOpened = false;
                }
            }
            #endregion

            strHTML += txtRange.Character;
        }


        strHTML += "</span></html>";

        return strHTML;
    }

我还没有启动html到rtf的反转换,一旦我完成了,我也会发布这个答案。

希望这有帮助。

谢谢,

Noorul。