Sha256: 28e37ac03710a5032be903c11dd8cde7846dd96db1f9b9282d7599cab39fa0f1

Contents?: true

Size: 1.52 KB

Versions: 18

Compression:

Stored size: 1.52 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
page_box = page.box
frame = Frame.new(page_box.left + 20, page_box.bottom + 20,
                  page_box.width - 40, page_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, align: :right,
                    background_color: "hp-blue-light2",
                    border: {width: 1, color: "hp-blue-dark"})
boxes << doc.layout.lorem_ipsum_box(count: 3, position: :flow, text_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

18 entries across 18 versions & 1 rubygems

Version Path
hexapdf-1.0.2 examples/017-frame_text_flow.rb
hexapdf-1.0.1 examples/017-frame_text_flow.rb
hexapdf-1.0.0 examples/017-frame_text_flow.rb
hexapdf-0.47.0 examples/017-frame_text_flow.rb
hexapdf-0.46.0 examples/017-frame_text_flow.rb
hexapdf-0.45.0 examples/017-frame_text_flow.rb
hexapdf-0.44.0 examples/017-frame_text_flow.rb
hexapdf-0.41.0 examples/017-frame_text_flow.rb
hexapdf-0.40.0 examples/017-frame_text_flow.rb
hexapdf-0.39.1 examples/017-frame_text_flow.rb
hexapdf-0.39.0 examples/017-frame_text_flow.rb
hexapdf-0.38.0 examples/017-frame_text_flow.rb
hexapdf-0.37.2 examples/017-frame_text_flow.rb
hexapdf-0.37.1 examples/017-frame_text_flow.rb
hexapdf-0.37.0 examples/017-frame_text_flow.rb
hexapdf-0.36.0 examples/017-frame_text_flow.rb
hexapdf-0.35.1 examples/017-frame_text_flow.rb
hexapdf-0.35.0 examples/017-frame_text_flow.rb