Tabs not handled correctly when using HTML provoder

2 posts, 0 answers
  1. Paul
    Paul avatar
    4 posts
    Member since:
    Oct 2012

    Posted 04 Jan 2013 Link to this post

    UPDATE: I fixed this by loading the html string directly instead of first converting to a byte array.

    So this:
        string text = File.ReadAllText(@"c:\temp\RichText.txt");
        Byte[] convertedText = Encoding.ASCII.GetBytes(text);
        RadDocument document = _htmlFormatProvider.Import(convertedText);

    became this:
        string text = File.ReadAllText(@"c:\temp\RichText.txt");
        RadDocument document = _htmlFormatProvider.Import(text);





    When I save and reload using an HtmlFormatProvider I get strange results with Tab characters.

    Here is an example where the original text is "a<tab>b<tab>c".

    The first time it is saved it looks like this. The tabs have been replaced with two "&nbsp;":
    <style type="text/css">
    .p_CC664AAA { margin: 0px 0px 12px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } 
    .s_6BF1D20F { font-family: 'Calibri';font-style: normal;font-size: 16px;color: #000000; } 
    </style><p class="p_CC664AAA"><span class="s_6BF1D20F">a&nbsp; &nbsp; b&nbsp; &nbsp; c</span></p>

    After reloading and saving again it looks like this. The two "&nbsp;" have been replaced with whitespace:
    <style type="text/css">
    .p_384D933F { margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } 
    .s_6BF1D20F { font-family: 'Calibri';font-style: normal;font-size: 16px;color: #000000; } 
    </style><p class="p_384D933F"><span class="s_6BF1D20F">a    b    c</span></p>

    After reloading and saving again it looks like this. The whitespaces have been replaced with question marks:
    <style type="text/css">
    .p_384D933F { margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } 
    .s_6BF1D20F { font-family: 'Calibri';font-style: normal;font-size: 16px;color: #000000; } 
    </style><p class="p_384D933F"><span class="s_6BF1D20F">a? ? b? ? c</span></p>

    Here is the sample code I am using for this test: 
    public Form1()
    {
        InitHtmlFormatProvider();
        InitializeComponent();
    }
     
    private static HtmlFormatProvider _htmlFormatProvider;
     
    private static void InitHtmlFormatProvider()
    {
        _htmlFormatProvider = new HtmlFormatProvider();
        var htmlExportSettings = new HtmlExportSettings();
        htmlExportSettings.DocumentExportLevel = DocumentExportLevel.Fragment;
        _htmlFormatProvider.ExportSettings = htmlExportSettings;
    }
     
    private void commandBarButtonLoad_Click(object sender, EventArgs e)
    {
        string text = File.ReadAllText(@"c:\temp\RichText.txt");
        Byte[] convertedText = Encoding.ASCII.GetBytes(text);
        RadDocument document = _htmlFormatProvider.Import(convertedText);
        radRichTextBox1.Document = document;
    }
     
    private void commandBarButtonSave_Click(object sender, EventArgs e)
    {
        string html = _htmlFormatProvider.Export(radRichTextBox1.Document);
        File.WriteAllText(@"c:\temp\RichText.txt", html);           
    }


  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 09 Jan 2013 Link to this post

    Hello Paul,

    I am glad to hear that you managed to resolve the issue. It seems that conversion to bytes loses the tabs due to encoding changes.

    All the best,
    Svett
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top