2012-07-26 55 views
0

我有這段代碼來創建一個RTF文檔,但我找不到如何給文本着色,有人可以幫我嗎?RTF顏色的文本在Ruby RTF寶石

# 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)} 

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) 

回答

1

退房在RDoc的最後一個例子 - http://ruby-rtf.rubyforge.org/docs/index.html

您已經創建了一個CharacterStyle並將它應用於段落,這是不允許的。您需要將CharacterStyle應用於文本。

改變你的輸出是:

document.paragraph() do |p| 
    p.apply(style) do |t| 
     t << "And there you have it." 
    end 
end 
+0

就是這樣,非常感謝賈斯汀 – peter 2012-08-02 20:02:49