Class: WizRtf::Document
- Inherits:
-
Object
- Object
- WizRtf::Document
- Defined in:
- lib/wiz_rtf/document.rb
Overview
Rtf Document
Creates a new Rtf document specifing the format of the pages.
Example:
doc = WizRtf::Document.new do
text "A Example of Rtf Document", 'text-align' => :center, 'font-family' => 'Microsoft YaHei', 'font-size' => 48, 'font-bold' => true, 'font-italic' => true, 'font-underline' => true
image('h:\eahey.png')
page_break
text "A Table Demo", 'foreground-color' => WizRtf::Color::RED, 'background-color' => '#0f00ff'
table [[{content: WizRtf::Image.new('h:\eahey.png'),rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
[{content:'4',rowspan:3,colspan:2},8],[11]], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50} do
add_row [1]
end
end
doc.save('c:\text.rtf')
Instance Method Summary collapse
-
#color(*rgb) ⇒ Object
Sets the color for the text.
-
#font(name, family = nil, character_set = 0, pitch = 2) ⇒ Object
Sets the Font for the text.
- #head(&block) ⇒ Object
-
#image(file) ⇒ Object
Puts a image into the current position within the document.
-
#initialize(options = {}, &block) ⇒ Document
constructor
A new instance of Document.
-
#line_break ⇒ Object
Writes a new line.
-
#page_break ⇒ Object
Writes a page interruption (new page).
-
#render(io) ⇒ Object
Outputs the Complete Rtf Document to a Generic Stream as a Rich Text Format (RTF) *
io
- The Generic IO to Output the RTF Document. -
#save(file) ⇒ Object
Outputs the complete Rtf Document to a file as a Rich Text Format (RTF) *
file
- file path and filename. -
#table(rows = [], options = {}, &block) ⇒ Object
Creates a new Table *
rows
- a table can be thought of as consisting of rows and columns. -
#text(str, styles = {}) ⇒ Object
This will add a string of
str
to the document, starting at the current drawing position.
Constructor Details
#initialize(options = {}, &block) ⇒ Document
Returns a new instance of Document.
26 27 28 29 30 31 32 |
# File 'lib/wiz_rtf/document.rb', line 26 def initialize( = {}, &block) @fonts = [] @colors = [] @parts = [] font 'NSimSun' block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given? end |
Instance Method Details
#color(*rgb) ⇒ Object
Sets the color for the text.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wiz_rtf/document.rb', line 99 def color(*rgb) color = WizRtf::Color.new(*rgb) if index = @colors.index {|c| c.to_rgb_hex == color.to_rgb_hex} index += 1 else @colors << color index = @colors.size end index end |
#font(name, family = nil, character_set = 0, pitch = 2) ⇒ Object
Sets the Font for the text.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wiz_rtf/document.rb', line 85 def font(name, family = nil, character_set = 0, pitch = 2) unless index = @fonts.index {|f| f.name == name} index = @fonts.size opts = WizRtf::Font::FONTS.detect {|f| f[:name] == name} if opts @fonts << WizRtf::Font.new(opts[:name], opts[:family], opts[:character], opts[:pitch]) else @fonts << WizRtf::Font.new(name, family, character_set, pitch) end end index end |
#head(&block) ⇒ Object
80 81 82 |
# File 'lib/wiz_rtf/document.rb', line 80 def head(&block) block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given? end |
#image(file) ⇒ Object
Puts a image into the current position within the document.
-
file
- image file path and filename.
138 139 140 |
# File 'lib/wiz_rtf/document.rb', line 138 def image(file) @parts << WizRtf::Image.new(file) end |
#line_break ⇒ Object
Writes a new line.
160 161 162 |
# File 'lib/wiz_rtf/document.rb', line 160 def line_break @parts << WizRtf::Cmd.new(:par) end |
#page_break ⇒ Object
Writes a page interruption (new page)
165 166 167 |
# File 'lib/wiz_rtf/document.rb', line 165 def page_break @parts << WizRtf::Cmd.new(:page) end |
#render(io) ⇒ Object
Outputs the Complete Rtf Document to a Generic Stream as a Rich Text Format (RTF)
-
io
- The Generic IO to Output the RTF Document
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wiz_rtf/document.rb', line 36 def render(io) io.group do io.cmd :rtf, 1 io.cmd :ansi io.cmd :ansicpg, 2052 io.cmd :deff, 0 io.group do io.cmd :fonttbl @fonts.each_with_index do |font, index| io.group do io.delimit do io.cmd :f, index io.cmd font.family io.cmd :fprq, font.pitch io.cmd :fcharset, font.character_set io.write ' ' io.write font.name end end end end io.group do io.cmd :colortbl io.delimit @colors.each do |color| io.delimit do io.cmd :red, color.red io.cmd :green, color.green io.cmd :blue, color.blue end end end @parts.each do |part| part.render(io) end end end |
#save(file) ⇒ Object
Outputs the complete Rtf Document to a file as a Rich Text Format (RTF)
-
file
- file path and filename.
76 77 78 |
# File 'lib/wiz_rtf/document.rb', line 76 def save(file) File.open(file, 'w') { |file| render(WizRtf::RtfIO.new(file)) } end |
#table(rows = [], options = {}, &block) ⇒ Object
Creates a new Table
-
rows
- a table can be thought of as consisting of rows and columns.
Options:
-
column_widths
- sets the widths of the Columns.
Example:
table [
[{content: WizRtf::Image.new('h:\eahey.png'),rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
[{content:'4',rowspan:3,colspan:2},8],[11]
], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50} do
add_row [1]
end
155 156 157 |
# File 'lib/wiz_rtf/document.rb', line 155 def table(rows = [], = {}, &block) @parts << WizRtf::Table.new(rows, , &block) end |
#text(str, styles = {}) ⇒ Object
This will add a string of str
to the document, starting at the current drawing position.
Styles:
-
text-align
- sets the horizontal alignment of the text. optional values::left
,:center
,:right
-
font-family
- set the font family of the text.- optional values: 'Arial', 'Arial Black', 'Arial Narrow','Bitstream Vera Sans Mono', 'Bitstream Vera Sans','Bitstream Vera Serif','Book Antiqua','Bookman Old Style','Castellar','Century Gothic', 'Comic Sans MS','Courier New','Franklin Gothic Medium','Garamond','Georgia','Haettenschweiler','Impact','Lucida Console' 'Lucida Sans Unicode','Microsoft Sans Serif','Monotype Corsiva','Palatino Linotype','Papyrus','Sylfaen','Symbol' 'Tahoma','Times New Roman','Trebuchet MS','Verdana'.
-
font-size
- set font size of the text. -
font-bold
- setting the value true for bold of the text. -
font-italic
- setting the value true for italic of the text. -
font-underline
- setting the value true for underline of the text.
Example:
text "A Example of Rtf Document", 'text-align' => :center, 'font-family' => 'Microsoft YaHei', 'font-size' => 48, 'font-bold' => true, 'font-italic' => true, 'font-underline' => true
129 130 131 132 133 134 |
# File 'lib/wiz_rtf/document.rb', line 129 def text(str, styles = {}) styles['foreground-color'] = color(styles['foreground-color']) if styles['foreground-color'] styles['background-color'] = color(styles['background-color']) if styles['background-color'] styles['font-family'] = font(styles['font-family']) if styles['font-family'] @parts << WizRtf::Text.new(str, styles) end |