Search code examples
c#winformsrtf

How to remove the extra spaces before the cell text?


I am working with WinForms. In my application, I need to use RTF for formatting cell text. However, when I set the RTF style, there are extra spaces shown before the text in the first cell. Here is the sample code I used:

string rtf3 =
    @"{\rtf1\ansi" +
    // font table
    @"{\fonttbl" +
    @"\f0 Axel;} " +
    @"\deff0 " +
    // color table
    @"{\colortbl" +
    @"\red186\green0\blue0; " +
    @"\red240\green224\blue255;} " +
    // first line
    @"\ql\f0\fs18 " + "David" +
    @"\plain\par" +
    // closing bracket
    @"}";

In this, I can't understand the style settings. Please anyone suggest me how to resolve the extra spaces as per the below image?

screenshot of form


Solution

  • You should remove the spaces that are not part of an RTF control word that you do not want to appear in the output. So spaces after ; and }, and before {. So your code will look like this:

    string rtf3 =
        @"{\rtf1\ansi" +
        // font table
        @"{\fonttbl" +
        @"\f0 Axel;}" +
        @"\deff0" +
        // color table
        @"{\colortbl" +
        @"\red186\green0\blue0;" +
        @"\red240\green224\blue255;}" +
        // first line
        @"\ql\f0\fs18 " + "David" +
        @"\plain\par" +
        // closing bracket
        @"}";