RTF Bullet List Example

99封情书 提交于 2019-11-30 02:34:32

问题


It seems impossible to find a very simple working example for an RTF bullet list that looks like this:

  • Hello
  • World
    • Nested (but with square as bullet please)
    • Also nested (also square bullet)
  • and regular again

I tried to make a small sample by using Word or TextEdit.app but they produce a lot of garbage. Can anyone help out?


回答1:


If you looked in the Word or Wordpad formatted RTF long enough, you would find some lines that look like this:

{\f2 {\pntext \'B7\tab}{*\pn\pnlvlblt\pnstart1{\pntxtb\'B7}}{\ltrch This is a test.}\li720\ri0\sa0\sb0\jclisttab\tx720\fi-360\ql\par}

{\f2 {\pntext \'B7\tab}{*\pn\pnlvlblt\pnstart1{\pntxtb\'B7}}{\ltrch So is this.}\li720\ri0\sa0\sb0\jclisttab\tx720\fi-360\ql\par}

This will format like this

  • This is a test.
  • So is this.

Nested lists are probably a little bit harder, but this is what you'll want if you don't have any nested lists.

Now it depends on what you want to do with this. If you're just displaying it, every tag means something useful. When I worked with lists, I was converting them to HTML, so a lot of the formatting tags (\li,\ri,\tx, etc.), I didn't need for my application because I was just using <ol>,<ul> and <li> tags. The main thing to see it the tag \pnlvlblt, which makes this list a bulleted list. Also, \'B7 is the actual unicode tag for the bullet, ·

The most updated specification of RTF is here:

https://www.microsoft.com/en-us/download/details.aspx?id=10725




回答2:


A simple, straightforward way is to just use characters as follows:

\line \bullet Hello
\line \bullet World
\line \tab \bullet Nested (but with square as bullet please)
\line \tab \u9632 Also nested (also square bullet)
\line \tab and regular again

You could still parse the RTF back to a tag-based format afterwards, with enclosing tags as hierarchy information is being provided by the indentation.

As you can see, any sort of unicode usage is fine. For the unicodes see this site.

For more information, be sure to consult the Bullets and Numbering section of the RTF Specifications.




回答3:


I tried Saur's anser but the "Also nested (also square bullet)" line had the A of Also covered up by the square bullet. Not sure why that happened.
■lso nested (also square bullet)

I like using the line indent commands so if a line wrap happens, the wrapped text is indented as well. When you do \line \tab, anything that runs off the end of the printed page will start at the left margin instead of where your indented text started from. This is system generated by a System.Windows.Forms.RichTextBox which I pasted into (from MS-Word) and then called RichTextBox.RTF to get the code.

{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset2 Symbol;}{\f1\froman\fprq2\fcharset0 Times New Roman;}{\f2\fnil\fprq2\fcharset2 Wingdings;}{\f3\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\ltrpar\fi-360\li360\f0\fs20\'b7\tab\f1 Hello\par
\f0\'b7\tab\f1 World\par
\pard\ltrpar\fi-360\li990\f2\'a7\tab\f1 Nested (but with square as bullet please)\par
\f2\'a7\tab\f1 Also nested (also square bullet)\par
\pard\ltrpar\fi-360\li360\sa200\sl276\slmult1\f0\'b7\tab\f1 and regular again\lang1024\par
\pard\ltrpar\lang1033\f3\fs17\par
}



回答4:


This is how I'm creating a bullet list:

{\rtf1\ansi\deff0
{{\li0\pntext\pn\pnlvlblt\pntxtb\u8226?\tab}{This is item one}\par}
{{\li0\pntext\pn\pnlvlblt\pntxtb\u8226?\tab}{This is item two}\par}
{{\li400\pntext\pn\pnlvlblt\pntxtb\u9642?\tab}{This is item three}\par}
{{\li400\pntext\pn\pnlvlblt\pntxtb\u9642?\tab}{This is item four}\par}
{{\li0\pntext\pn\pnlvlblt\pntxtb\u8226?\tab}{This is item five}\par}
}

Renders as:

Be aware that the multilevel list is not really threated like one in word. It's basicly a normal list with indented nodes.

Explanation:

\li0 : intend 0 (usefull for multilevel lists)

\pntext : preffix for all numbered/bulleted paragraphs

\pn : turns on paragraph numbering (also needed for bullet lists)

\pnlvlblt : this paragraph is a bullet paragraph

\pntxtb : text before (the icon)

\u8226? : unicode character for bullet point

\u9642? unicode character for small black square

\tab : adds a tab after the icon

\par : end of paragraph




回答5:


This is for people who want to add text and bullet list to an extended wpf toolkit (wpfToolKit) richTextBox. I created this, because I had some problems with the solutions here. They didnt work for me or the \tab was too large infront of the bullet. With this implementation, you can adjust the indention infront of the bullets.

Notes for this implementation:

  • you have to play around with the indent (\li). The values have to differ by hundreds in my case to see an effect. in this example: 100 and 500.

  • using \par\pard for each line in the richtext box is important. Without it, it would indent other lines without bullets, too.

  • the header includes stuff like coloring, which you probably dont need.

Methods

public  string AddBullet(string textRow, int indent)
{
    return $@"\li{indent}{{\pntext\f1\'B7}}{{\*\pn\pnlvlblt{{\pntxtb\'B7}}}}{textRow}";
}

public  string AddNewLine(string text)
{
    return $@"{text}\par\pard ";
}


public  string AddHeader(string text, string fontName, int fontSize)
{
    return $@"{{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{{\fonttbl{{\f0\fnil\fcharset0 {fontName};}}{{\f1\fnil\fcharset2 Symbol;}}}}{{\colortbl ;\red0\green77\blue187;\red128\green0\blue0;\red0\green0\blue0;\red155\green0\blue211;\red81\green163\blue69;\red51\green51\blue51;}}\viewkind4\uc1\pard\sl0\slmult1\f0\fs{fontSize * 2}\lang7 {text}}}";
}

How to use the Methods

var regularRow1     = AddNewLine("AAAA");
var bulletRow1      = AddNewLine(AddBullet("BBBB", 100));
var bulletRowNested = AddNewLine(AddBullet("CCCC", 500));
var bulletRow2      = AddNewLine(AddBullet("BBBB", 100));
var regularRow2     = AddNewLine("AAAA");

var richTextWithoutHeader = regularRow1 + bulletRow1 + bulletRowNested + bulletRow2 + regularRow2;
var richText = AddHeader(richTextWithoutHeader, "Arial", 12);

Result



来源:https://stackoverflow.com/questions/15367975/rtf-bullet-list-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!