Module: Uniword::Builder

Defined in:
lib/uniword/builder.rb,
lib/uniword/builder/has_borders.rb,
lib/uniword/builder/has_shading.rb,
lib/uniword/builder/run_builder.rb,
lib/uniword/builder/sdt_builder.rb,
lib/uniword/builder/toc_builder.rb,
lib/uniword/builder/base_builder.rb,
lib/uniword/builder/list_builder.rb,
lib/uniword/builder/chart_builder.rb,
lib/uniword/builder/image_builder.rb,
lib/uniword/builder/style_builder.rb,
lib/uniword/builder/table_builder.rb,
lib/uniword/builder/theme_builder.rb,
lib/uniword/builder/comment_builder.rb,
lib/uniword/builder/section_builder.rb,
lib/uniword/builder/document_builder.rb,
lib/uniword/builder/footnote_builder.rb,
lib/uniword/builder/numbering_builder.rb,
lib/uniword/builder/paragraph_builder.rb,
lib/uniword/builder/table_row_builder.rb,
lib/uniword/builder/watermark_builder.rb,
lib/uniword/builder/table_cell_builder.rb,
lib/uniword/builder/bibliography_builder.rb,
lib/uniword/builder/header_footer_builder.rb

Overview

Builder module provides a construction API for OOXML models.

The Builder API orchestrates the creation and manipulation of OOXML model objects without adding convenience methods to the model classes themselves.

Examples:

Build a document

doc = Uniword::Builder::DocumentBuilder.new
doc.paragraph { |p| p << 'Hello World' }
doc.save('output.docx')

Load and modify a document

doc = Uniword::Builder::DocumentBuilder.from_file('input.docx')
doc.paragraph { |p| p << 'Appended content' }
doc.save('output.docx')

Defined Under Namespace

Modules: HasBorders, HasShading Classes: BaseBuilder, BibliographyBuilder, ChartBuilder, CommentBuilder, DocumentBuilder, FootnoteBuilder, HeaderFooterBuilder, ImageBuilder, ListBuilder, NumberingBuilder, ParagraphBuilder, RunBuilder, SdtBuilder, SectionBuilder, StyleBuilder, TableBuilder, TableCellBuilder, TableRowBuilder, ThemeBuilder, TocBuilder, WatermarkBuilder

Class Method Summary collapse

Class Method Details

.date_field(format: "M/d/yyyy") ⇒ Wordprocessingml::Paragraph

Factory: creates a paragraph containing a DATE field

Parameters:

  • format (String) (defaults to: "M/d/yyyy")

    Date format (default ‘M/d/yyyy’)

Returns:



187
188
189
# File 'lib/uniword/builder.rb', line 187

def self.date_field(format: "M/d/yyyy")
  build_field_paragraph(" DATE \\@ \"#{format}\" ")
end

.floating_image(path, width: nil, height: nil, align: nil, wrap: :square, behind_text: false) ⇒ Wordprocessingml::Drawing

Factory: creates a floating image Drawing NOTE: This does NOT register the image in the document’s image_parts. Use DocumentBuilder#floating_image instead for proper DOCX packaging.

Parameters:

  • path (String)

    Path to image file

  • width (Integer, nil) (defaults to: nil)

    Width in EMU

  • height (Integer, nil) (defaults to: nil)

    Height in EMU

  • align (Symbol, nil) (defaults to: nil)

    Horizontal alignment

  • wrap (Symbol) (defaults to: :square)

    Text wrapping style

  • behind_text (Boolean) (defaults to: false)

    Place behind text

Returns:



135
136
137
138
139
140
# File 'lib/uniword/builder.rb', line 135

def self.floating_image(path, width: nil, height: nil, align: nil,
                        wrap: :square, behind_text: false)
  ImageBuilder.create_floating(nil, path, width: width, height: height,
                                          align: align, wrap: wrap,
                                          behind_text: behind_text)
end

Factory: creates a Hyperlink with optional text run

Parameters:

  • target (String)

    URL or bookmark target

  • text (String, nil) (defaults to: nil)

    Link display text

  • color (String)

    Link color (hex, default ‘0000FF’)

  • underline (Boolean)

    Underline the link (default true)

Returns:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/uniword/builder.rb', line 86

def self.hyperlink(target, text = nil, **options)
  hl = Wordprocessingml::Hyperlink.new
  hl.target = target
  if text
    color = options.fetch(:color, "0000FF")
    ul = options.fetch(:underline, true)
    run = self.text(text, color: color, underline: ul ? "single" : nil)
    hl.runs << run
  end
  hl
end

.image(path, width: nil, height: nil) ⇒ Wordprocessingml::Run

Factory: creates a Run containing an inline image Drawing NOTE: This does NOT register the image in the document’s image_parts. Use DocumentBuilder#image instead for proper DOCX packaging.

Parameters:

  • path (String)

    Path to image file

  • width (Integer, nil) (defaults to: nil)

    Width in EMU

  • height (Integer, nil) (defaults to: nil)

    Height in EMU

Returns:



120
121
122
# File 'lib/uniword/builder.rb', line 120

def self.image(path, width: nil, height: nil)
  ImageBuilder.create_drawing(nil, path, width: width, height: height)
end

.line_breakWordprocessingml::Run

Factory: creates a Run containing a line break



163
164
165
166
167
# File 'lib/uniword/builder.rb', line 163

def self.line_break
  run = Wordprocessingml::Run.new
  run.break = Wordprocessingml::Break.new(type: "line")
  run
end

.page_breakWordprocessingml::Run

Factory: creates a page break Run



145
146
147
148
149
# File 'lib/uniword/builder.rb', line 145

def self.page_break
  run = Wordprocessingml::Run.new
  run.break = Wordprocessingml::Break.new(type: "page")
  run
end

.page_number_fieldWordprocessingml::Paragraph

Factory: creates a paragraph containing a PAGE field



172
173
174
# File 'lib/uniword/builder.rb', line 172

def self.page_number_field
  build_field_paragraph(" PAGE ")
end

.tabWordprocessingml::Run

Factory: creates a Run with a tab character



154
155
156
157
158
# File 'lib/uniword/builder.rb', line 154

def self.tab
  run = Wordprocessingml::Run.new
  run.tab = Wordprocessingml::Tab.new
  run
end

.tab_stop(position:, alignment: :left, leader: nil) ⇒ Properties::TabStop

Factory: creates a TabStop

Parameters:

  • position (Integer)

    Tab position in twips

  • alignment (Symbol, String) (defaults to: :left)

    :left, :center, :right, :decimal

  • leader (String, nil) (defaults to: nil)

    Leader character style

Returns:



104
105
106
107
108
109
110
# File 'lib/uniword/builder.rb', line 104

def self.tab_stop(position:, alignment: :left, leader: nil)
  Properties::TabStop.new(
    position: position,
    alignment: alignment.to_s,
    leader: leader,
  )
end

.text(content, **formatting) ⇒ Wordprocessingml::Run

Factory: creates a Run with optional formatting

Parameters:

  • content (String)

    Text content

  • bold (Boolean)

    Bold formatting

  • italic (Boolean)

    Italic formatting

  • underline (String, Boolean)

    Underline style

  • color (String)

    Font color (hex)

  • size (Integer)

    Font size in points

  • font (String)

    Font name

  • highlight (String)

    Highlight color

  • strike (Boolean)

    Strikethrough

  • small_caps (Boolean)

    Small caps

  • caps (Boolean)

    All caps

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/uniword/builder.rb', line 57

def self.text(content, **formatting)
  run = Wordprocessingml::Run.new(text: content)
  return run if formatting.empty?

  props = Wordprocessingml::RunProperties.new
  props.bold = Properties::Bold.new(value: true) if formatting[:bold]
  props.italic = Properties::Italic.new(value: true) if formatting[:italic]
  if formatting[:underline]
    val = formatting[:underline] == true ? "single" : formatting[:underline].to_s
    props.underline = Properties::Underline.new(value: val)
  end
  props.color = Properties::ColorValue.new(value: formatting[:color].to_s) if formatting[:color]
  props.size = Properties::FontSize.new(value: formatting[:size].to_i * 2) if formatting[:size]
  props.font = formatting[:font] if formatting[:font]
  props.highlight = Properties::Highlight.new(value: formatting[:highlight].to_s) if formatting[:highlight]
  props.strike = Properties::Strike.new(value: true) if formatting[:strike]
  props.small_caps = Properties::SmallCaps.new(value: true) if formatting[:small_caps]
  props.caps = Properties::Caps.new(value: true) if formatting[:caps]
  run.properties = props
  run
end

.time_field(format: "h:mm:ss am/pm") ⇒ Wordprocessingml::Paragraph

Factory: creates a paragraph containing a TIME field

Parameters:

  • format (String) (defaults to: "h:mm:ss am/pm")

    Time format (default ‘h:mm:ss am/pm’)

Returns:



195
196
197
# File 'lib/uniword/builder.rb', line 195

def self.time_field(format: "h:mm:ss am/pm")
  build_field_paragraph(" TIME \\@ \"#{format}\" ")
end

.total_pages_fieldWordprocessingml::Paragraph

Factory: creates a paragraph containing a NUMPAGES field



179
180
181
# File 'lib/uniword/builder.rb', line 179

def self.total_pages_field
  build_field_paragraph(" NUMPAGES ")
end