Sha256: 8455fde516574e789f0f1d4e44c3e142b11385b84da187ae070d3a855e6c2db4

Contents?: true

Size: 1.59 KB

Versions: 16

Compression:

Stored size: 1.59 KB

Contents

# ## Text Layouter - Alignment
#
# The [HexaPDF::Layout::TextLayouter] class can be used to easily lay out text
# inside a rectangular area, with various horizontal and vertical alignment
# options.
#
# The text can be aligned horizontally by setting [HexaPDF::Layout::Style#align]
# and vertically by [HexaPDF::Layout::Style#valign]. In this example, a sample
# text is laid out in all possible combinations.
#
# Usage:
# : `ruby text_layouter_alignment.rb`
#

require 'hexapdf'

sample_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. at".tr("\n", ' ')

doc = HexaPDF::Document.new
canvas = doc.pages.add.canvas
canvas.font("Times", size: 10, variant: :bold)

width = 100
height = 150
y_base = 800
tf = HexaPDF::Layout::TextFragment.create(sample_text,
                                          font: doc.fonts.add("Times"))
tl = HexaPDF::Layout::TextLayouter.new

[:left, :center, :right, :justify].each_with_index do |align, x_index|
  x = x_index * (width + 20) + 70
  canvas.text(align.to_s, at: [x + 40, y_base + 15])

  [:top, :center, :bottom].each_with_index do |valign, y_index|
    y = y_base - (height + 30) * y_index
    canvas.text(valign.to_s, at: [20, y - height / 2]) if x_index == 0

    tl.style.align(align).valign(valign)
    tl.fit([tf], width, height).draw(canvas, x, y)
    canvas.stroke_color(128, 0, 0).rectangle(x, y, width, -height).stroke
  end
end

doc.write("text_layouter_alignment.pdf", optimize: true)

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
hexapdf-0.11.9 examples/009-text_layouter_alignment.rb
hexapdf-0.11.8 examples/009-text_layouter_alignment.rb
hexapdf-0.11.7 examples/009-text_layouter_alignment.rb
hexapdf-0.11.6 examples/009-text_layouter_alignment.rb
hexapdf-0.11.5 examples/009-text_layouter_alignment.rb
hexapdf-0.11.4 examples/009-text_layouter_alignment.rb
hexapdf-0.11.3 examples/009-text_layouter_alignment.rb
hexapdf-0.11.2 examples/009-text_layouter_alignment.rb
hexapdf-0.11.1 examples/009-text_layouter_alignment.rb
hexapdf-0.11.0 examples/009-text_layouter_alignment.rb
hexapdf-0.10.0 examples/009-text_layouter_alignment.rb
hexapdf-0.9.3 examples/009-text_layouter_alignment.rb
hexapdf-0.9.2 examples/009-text_layouter_alignment.rb
hexapdf-0.9.1 examples/009-text_layouter_alignment.rb
hexapdf-0.9.0 examples/009-text_layouter_alignment.rb
hexapdf-0.8.0 examples/009-text_layouter_alignment.rb