如何更改RTF文档中特定字符的背景颜色?

时间:2010-08-18 15:28:26

标签: ruby rtf

我正在尝试从Ruby程序输出RTF(富文本格式) - 我更喜欢直接发出RTF而不使用RTF gem,因为我做的很简单。

我想突出显示DNA序列比对中的特定字符,从文档中我可以使用\ highlightN ... \ highlight0或\ cbN ... \ cb1

问题是我不能让\ cb在Word:Mac 2008或Mac TextEdit中工作(\ cf工作正常所以我知道它不是颜色表问题)

\ highlight确实有效,但看似只有两种可能的颜色(黑色和红色)和\ highlight不使用自定义颜色表。

通过在Word中创建带有字符着色的简单文档并保存为RTF,我可以看到大概冗长的RTF代码块,大概可以做我想要的,但它是如此难以理解,以至于我没有看到树木的木材。 / p>

部分问题可能是Mac Word没有正确实现RTF。我没有Windows版的Word方便。

任何人都知道遮挡文本块的正确方法吗?

由于 --Rob

3 个答案:

答案 0 :(得分:5)

RTF Pocket Guide中有一条说明说MS Word没有实现\ cb命令。它说MS Word使用\ chshdng0 \ chcbpatN(其中“N”是与\ cb一起使用的颜色编号)。本书建议使用类似下面的内容来兼容实现\ cbN和/或\ chshdng0 \ chcbpatN的程序:{\ chshdng0 \ chcbpat5 \ cb5 text}。

注意:我所拥有的书的副本于2003年出版,因此可能有点过时。

答案 1 :(得分:3)

The sequence of RTF commands that seems to be most universally supported by RTF-capable applications is:

\chshdng10000\chcbpatN\chcfpatN\cbN

These commands:

  1. set the shading to 100 percent
  2. set the pattern foreground and background colors to the color from the color table (we're not actually specifying a shading pattern)
  3. set the character background to the color from the color table

Word was the most difficult application to properly render background colors in:

Despite what the latest (1.9.1) RTF spec says, Word 2013 does not resolve \highlightN colors from the \colortbl. Instead, \highlightN maps to a predefined list of colors. It looks like those colors come from the 1.5 version of the RTF spec.

Regarding \cb, the 1.9.1 spec contains this helpful pointer at the end of the section on Color Table:

Note: Windows versions of Word have never supported \cbN, but it can be emulated by the control word sequence \chshdng0\chcbpatN.

This is almost a useful suggestion, except that if you read the documentation for \chshdngN:

Character shading. The N argument is a value representing the shading of the text in hundredths of a percent.

So, 0 turns out to not be a very useful value; 100 / 0.01 gives us the 10000 we used in the sequence above.

答案 2 :(得分:1)

使用写字板创建RTF文档,而不是Word。写字板创建了更简单的文档,即接近人类可读的文档。

每次我需要在WinForms应用程序中显示格式化文本时,我都会使用写字板,并且需要RichTextBox控件可以处理分配给其Rtf参数的内容。