0

我有这个代码来创建一个 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)
4

1 回答 1

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
于 2012-08-02T17:17:56.643 回答