rubyonrails: rtf

首先是安裝:
gem install rtf

代碼:

#!/usr/bin/env ruby

require 'rubygems'
require 'rtf'

include RTF

# Create required styles.
styles = {}
styles['HEADER'] = CharacterStyle.new
styles['HEADER'].bold = true
styles['HEADER'].font_size = 28
styles['NORMAL'] = ParagraphStyle.new
styles['NORMAL'].justification = ParagraphStyle::FULL_JUSTIFY
styles['INDENTED'] = ParagraphStyle.new
styles['INDENTED'].left_indent = 400

document = Document.new(Font.new(Font::ROMAN, 'Arial'))
document.paragraph do |p|
p.apply(styles['HEADER']) do |s|
s << '1.0 Introduction'
end
end
document.paragraph(styles['NORMAL']) do |p|
p << 'Here is a short example program in the Ruby programming '
p << 'language that demonstrates writing a single line of text '
p << 'to a file created in the current working directory...'
end

c = 1
document.paragraph(styles['INDENTED']) do |n1|
n1.line_break
n1.font(Font.new(Font::MODERN, 'Courier New')) do |n2|
n2 << "#{sprintf('%02d', c)} File.open('output.txt', 'w') do |file|"
c += 1
n2.line_break
n2 << "#{sprintf('%02d', c)} file.write('Some text.')"
c += 1
n2.line_break
n2 << "#{sprintf('%02d', c)} end"
end
end

document.line_break
document.paragraph(styles['NORMAL']) do |p|
p << "And there you have it. A simple example indeed."
end

File.open('example01.rtf', 'w') do |file|
file.write(document.to_rtf)
end
發佈了12 篇原創文章 · 獲贊 169 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章