C# – Richtextbox draw an rtf line

clinerichtextboxrtf

I want to add a horizontal line to the RichTextBox as a delimiter of my text.
I've found some examples of RTF code implementing a line and tried them in that way:

rtbResFile.Rtf = @"{\rtf1{\pard some text.\par}{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}{\pard \par}{\pard some other text.\par}}";

This way implements creating a blank paragraph with border, so that should looks like a line. However it doesn't show anything. Just a blank paragraph.
Even if I try to implement it in the way include line object

{\rtf1
{\pard some text.\par}
{\pard {\*\do\dobxcolumn\dobypara\dodhgt
        \dpline\dpxsize9200\dplinesolid\dplinew30}\par}
{\pard some other text.\par}
}

It still shows nothing. Does RichTextBox supports this? Or any other ways include the horizontal line in the rtf string?

Best Answer

There are a few different ways to create a horizontal line in RTF. Depending on the control or program being used your mileage may vary. RTF implementations in controls and programs tend to simply ignore markup that they don't know how to deal with.

By drawing polygons:

{\pard{\*\do
\dobxcolumn \dobypara \dodhgt7200
\dpline \dpptx0 \dppty0 \dpptx7200
\dppty0 \dpx0 \dpy0 \dpxsize7200
\dpysize0 \dplinew15
\dplinecor0 \dplinecog0 \dplinecob0 }\par}

By inserting a blank paragraph with a border followed by another blank paragraph without a border:

{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}
{\pard\par}

You can change the size and apparent positionof the line by setting indents on the paragraph:

{\pard \li2268 \ri567
\brdrb \brdrs \brdrw10 \brsp20 \par}
{\pard\par}

I highly recommend O'Reilly's RTF Pocket Guide for working with this stuff, which is where this came from.

Some further experimentation produced the code below, which does work in WordPad and the RichTextBox control.

{\pict\wmetafile8\picw26\pich26\picwgoal20000\pichgoal15 
0100090000035000000000002700000000000400000003010800050000000b0200000000050000
000c0202000200030000001e000400000007010400040000000701040027000000410b2000cc00
010001000000000001000100000000002800000001000000010000000100010000000000000000
000000000000000000000000000000000000000000ffffff00000000ff040000002701ffff0300
00000000
}

Basically, it involves inserting a 1x1 pixel image of a black dot and stretching it as needed by adjusting the height and width goals. The goal measurement is in twips. A twip is defined as being 1/1440 of an inch. It's a horrible hack, but it works.

Related Topic