Sha256: 010833d99271d2309033735c4ef094ff5860f84d42e5035b54688a53b397cae8

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

# # Frame - Text Flow
#
# This example shows how [HexaPDF::Layout::Frame] and [HexaPDF::Layout::TextBox]
# can be used to flow text around objects.
#
# Three boxes are placed repeatedly onto the frame until it is filled: two
# floating boxes (one left, one right) and a text box. The text box is styled to
# flow its content around the other two boxes.
#
# Usage:
# : `ruby frame_text_flow.rb`
#

require 'hexapdf'
require 'hexapdf/utils/graphics_helpers'

include HexaPDF::Layout
include HexaPDF::Utils::GraphicsHelpers

doc = HexaPDF::Document.new

page = doc.pages.add
media_box = page.box(:media)
frame = Frame.new(media_box.left + 20, media_box.bottom + 20,
                  media_box.width - 40, media_box.height - 40)

boxes = []
boxes << doc.layout.image_box(File.join(__dir__, 'machupicchu.jpg'),
                              width: 100, margin: [10, 30], position: :float)
boxes << Box.create(width: 50, height: 50, margin: 20,
                    position: :float, position_hint: :right,
                    border: {width: 1, color: [[255, 0, 0]]})
boxes << doc.layout.lorem_ipsum_box(count: 3, position: :flow, align: :justify)

i = 0
frame_filled = false
until frame_filled
  box = boxes[i]
  drawn = false
  until drawn || frame_filled
    result = frame.fit(box)
    if result.success?
      frame.draw(page.canvas, result)
      drawn = true
    else
      frame_filled = !frame.find_next_region
    end
  end
  i = (i + 1) % boxes.length
end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hexapdf-0.26.2 examples/017-frame_text_flow.rb
hexapdf-0.26.1 examples/017-frame_text_flow.rb
hexapdf-0.26.0 examples/017-frame_text_flow.rb
hexapdf-0.25.0 examples/017-frame_text_flow.rb
hexapdf-0.24.2 examples/017-frame_text_flow.rb
hexapdf-0.24.1 examples/017-frame_text_flow.rb
hexapdf-0.24.0 examples/017-frame_text_flow.rb