2

I am working on RichEditBox Control in Windows 10 UWP to allow a user with some formatting options.I am getting a HTML from the server and I want to convert that into RTF to display it in RichEditBox. Similarly, I want to convert it again to HTML when the user has completed editing. I did a search but I got samples but they are not helping. One of them that I found is working for RichTextBlock, not RichEditBox

HTML to RTF

I also tried below URL(RTF to HTML) but it doesn't give proper output

RTF to HTML

Please, someone, suggest how to achieve my requirement with RichEditBox as it is only control that allows formatting options as Bold,Italic,Bullet points etc.

Kinjan Bhavsar
  • 1,439
  • 28
  • 58

1 Answers1

1

I found the base concept to convert in this link , and I created the following method to convert rtf to html . it converts the text format options correctly.

Here is the method which converts rtf to html , u can edit as per ur need

 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;
    }

And I am yet to start the reverse conversion for html to rtf, once i done with that, i will post that answer too.

Hope this helps.

Thanks,

Noorul.

Noorul
  • 873
  • 2
  • 9
  • 26
  • Thanks. Will surely give it a try. – Kinjan Bhavsar Sep 29 '16 at 10:02
  • No but will try and if it works will mark answer as accepted. Also, how to handle bullet points and nymbers list? Did you tried that? – Kinjan Bhavsar Oct 03 '16 at 05:11
  • Yes , I forget to inform that. Even i got stuck in there, There is no conversion for bullet points, currently i am going forward with this simple conversion, since I need to complete it soon. Later i will add the conversion for bullet points too, and will edit the answer. – Noorul Oct 03 '16 at 06:46
  • If you got any better solution pls update it here :) – Noorul Oct 03 '16 at 06:46
  • Hi Kinjan, I updated the answer for bullet and numbering too. – Noorul Oct 13 '16 at 10:10
  • Thanks a lot. Did you checked my other question, if you can help me with that? – Kinjan Bhavsar Oct 13 '16 at 10:11
  • Yes I checked, am not that much familiar with those concepts.I regret that I can't help you in that.:( – Noorul Oct 13 '16 at 10:13
  • This works fine. But I want to remove `` which is added during conversion. Is it possible? – Kinjan Bhavsar Oct 13 '16 at 11:08
  • 1
    yes , u can. it gives the style options. if you remove that u need to add some alternate for that, or go without style options. – Noorul Oct 14 '16 at 09:09
  • Thank you. Works like a charm – thezapper Jan 13 '18 at 09:36
  • 1
    @Noorul To help others, you may want to add a line in your original post that states that your solution covers the features of converting of the bullets and numbering, as well. I had first ignored your solution after reading a comment from a user that it does not cover bullets and numbering. But then I had to go through all the comments of this post to later discover that you indeed cover those two features as well. – nam Mar 02 '19 at 17:24