How can I change the background color of specific characters in a RTF document?
Asked Answered
B

4

6

I'm trying to output RTF (Rich Text Format) from a Ruby program - and I'd prefer to just emit RTF directly without using the RTF gem as I'm doing pretty simple stuff.

I would like to highlight specific characters in a DNA sequence alignment and from the docs it seems that I can either use \highlightN ... \highlight0 or \cbN ... \cb1

The problem is that I cannot get \cb to work in either Word:Mac 2008 or Mac TextEdit (\cf works fine so I know it's not a color table issue)

\highlight does work but seemingly only with two of the possible colors (black and red) and \highlight does not use the custom color table.

By creating simple docs in Word with character shading and saving as RTF I can see blocks of ridiculously verbose RTF code that presumably does what I want, but it is so impenetrable that I'm not seeing the wood for the trees.

Part of the problem may well be that Mac Word is just not implementing RTF properly. I don't have a Windows version of Word handy.

Anyone know the right way to shade blocks of text?

Thanks --Rob

Balustrade answered 18/8, 2010 at 15:28 Comment(0)
E
6

There is a note in the RTF Pocket Guide that says MS Word does not implement the \cb command. It says MS Word uses \chshdng0\chcbpatN (where "N" is the color number that you would use with \cb). The book recommends using something like the following for compatibility with programs that implement \cbN and/or \chshdng0\chcbpatN: {\chshdng0\chcbpat5\cb5 text}.

Note: The copy of the book I have was published in 2003, so it might be a bit out-of-date.

Eversole answered 18/8, 2010 at 21:53 Comment(0)
D
5

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.

Dov answered 22/3, 2017 at 18:40 Comment(0)
R
1

Use WordPad to create RTF documents, not Word. WordPad creates much simpler documents, i.e. approaching human-readable.

I use WordPad every time I need to display formatted text in a WinForms application, and need something that the RichTextBox control can handle being assigned to its Rtf parameter.

Rompers answered 5/6, 2016 at 18:57 Comment(0)
S
0

Comment on @Jacob Carpenter's answer: https://mcmap.net/q/1661639/-how-can-i-change-the-background-color-of-specific-characters-in-a-rtf-document

In Apache OpenOffice 4.1.15 \chshdng0\chcbpatN turns out to be the combination of control words and parameters which produce the expected background color. Changing 0 to a larger number makes the color darker (10000 yielding black):

Screenshot from Apache OpenOffice showing color #E0E0FF being shaded with \chsdngN with N=0,100,1000,5000,10000

WordPad on Windows 10 22H2 seems to ignore \chshdngN producing the same color regardless the value of the parameter N. Same goes for LibreOffice Writer 6.1.6.

Seducer answered 28/2 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.