Sha256: c7780cc2b4d22b8c05b5c18f6803bb763fe44a6c1fbf489e3db5a863268093d5

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 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

sample_text = "Lorem ipsum dolor sit amet, con\u{00AD}sectetur
adipis\u{00AD}cing 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.
".tr("\n", ' ') * 10
items = [TextFragment.create(sample_text, font: doc.fonts.add("Times"))]

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

image = doc.images.add(File.join(__dir__, 'machupicchu.jpg'))
iw, ih = calculate_dimensions(image.width, image.height, rwidth: 100)

boxes = []
boxes << Box.create(width: iw, height: ih,
                    margin: [10, 30], position: :float) do |canv, box|
  canv.image(image, at: [0, 0], width: 100)
end
boxes << Box.create(width: 50, height: 50, margin: 20,
                    position: :float, position_hint: :right,
                    border: {width: 1, color: [[255, 0, 0]]})
boxes << TextBox.new(items, style: {position: :flow, align: :justify})

i = 0
frame_filled = false
until frame_filled
  box = boxes[i]
  drawn = false
  until drawn || frame_filled
    drawn = frame.draw(canvas, box)
    frame_filled = !frame.find_next_region unless drawn
  end
  i = (i + 1) % boxes.length
end

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

Version data entries

16 entries across 16 versions & 1 rubygems

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