I have this code to create a RTF document but i can’t find how to colorize the text, could someone help me out ?
# encoding: utf-8
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.dirname(__FILE__)+"/../lib/")
require 'rtf'
include RTF
colour = Colour.new(150, 150, 150)
style = CharacterStyle.new
style.bold = true
style.font_size = 28
style.foreground = colour
document = RTF::Document.new(RTF::Font.new(RTF::Font::ROMAN, 'Times New Roman'))
document.paragraph(style) do |p|
p << "And there you have it."
end
File.open('my_document.rtf', 'w') {|file| file.write(document.to_rtf)}
gives
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rtf-0.3.3/lib/rtf/style.rb:124:in `prefix': undefined method `index' for nil:NilClass (NoMethodError)
Check out the last example in the RDoc – http://ruby-rtf.rubyforge.org/docs/index.html.
You have created a CharacterStyle and are applying it to a paragraph, which is not allowed. You need to apply the CharacterStyle to the text.
Change your output to be: