32
bounding_box/stretchy.rb
Bounding Boxes accept an optional :height parameter. Unless it is provided the bounding box
will be stretchy. It will expand the height to fit all content generated inside it.
y_position = cursor
bounding_box([0, y_position], :width => 200, :height => 100) do
text "This bounding box has a height of 100. If this text gets too large " +
"it will flow to the next page."
transparent(0.5) { stroke_bounds }
end
bounding_box([300, y_position], :width => 200) do
text "This bounding box has variable height. No matter how much text is " +
"written here, the height will expand to fit."
text " _" * 100
text " *" * 100
transparent(0.5) { stroke_bounds }
end
This bounding box has a height of
100. If this text gets too large it will
flow to the next page.
This bounding box has variable
height. No matter how much text is
written here, the height will expand to
fit.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * *
33
bounding_box/nesting.rb
Normally when we provide the top left corner of a bounding box we express the coordinates
relative to the margin box. This is not the case when we have nested bounding boxes. Once
nested the inner bounding box coordinates are relative to the outter bounding box.
This example shows some nested bounding boxes with fixed and stretchy heights. Note how the
cursor method returns coordinates relative to the current bounding box.
def box_content(string)
text string
transparent(0.5) { stroke_bounds }
end
gap = 20
bounding_box([50, cursor], :width => 400, :height => 200) do
box_content("Fixed height")
bounding_box([gap, cursor - gap], :width => 300) do
text "Stretchy height"
bounding_box([gap, bounds.top - gap], :width => 100) do
text "Stretchy height"
transparent(0.5) { dash(1); stroke_bounds; undash }
end
bounding_box([gap * 7, bounds.top - gap], :width => 100, :height => 50) do
box_content("Fixed height")
end
transparent(0.5) { dash(1); stroke_bounds; undash }
end
bounding_box([gap, cursor - gap], :width => 300, :height => 50) do
box_content("Fixed height")
end
end
Fixed height
Stretchy height
Stretchy height
Fixed height
Fixed height
38
bounding_box/indentation.rb
Sometimes you just need to indent a portion of the contents of a bounding box, and using a nested
bounding box is pure overkill. The indent method is what you might need.
Just provide a number for it to indent all content generated inside the block.
text "No indentation on the margin box."
indent(20) do
text "Some indentation inside an indent block."
end
move_down 20
bounding_box([50, cursor], :width => 400, :height => cursor) do
transparent(0.5) { stroke_bounds }
move_down 10
text "No indentation inside this bounding box."
indent(40) do
text "Inside an indent block. And so is this horizontal line:"
stroke_horizontal_rule
end
move_down 10
text "No indentation"
move_down 20
indent(60) do
text "Another indent block."
bounding_box([0, cursor], :width => 200) do
text "Note that this bounding box coordinates are relative to the " +
"indent block"
transparent(0.5) { stroke_bounds }
end
end
end
No indentation on the margin box.
Some indentation inside an indent block.
No indentation inside this bounding box.
Inside an indent block. And so is this horizontal line:
No indentation
Another indent block.
Note that this bounding box
coordinates are relative to the indent
block
13
bounding_box/canvas.rb
The origin example already mentions that a new document already comes with a margin box
whose bottom left corner is used as the origin for calculating coordinates.
What has not been told is that there is one helper for "bypassing" the margin box: canvas. This
method is a shortcut for creating a bounding box mapped to the absolute coordinates and
evaluating the code inside it.
The following snippet draws a circle on each of the four absolute corners.
canvas do
fill_circle [bounds.left, bounds.top], 30
fill_circle [bounds.right, bounds.top], 30
fill_circle [bounds.right, bounds.bottom], 30
fill_circle [0, 0], 30
end
28
bounding_box/russian_boxes.rb
This example is mostly just for fun, and shows how nested bounding boxes can simplify
calculations. See the "Bounding Box" section of the manual for more basic uses.
def combine(a1, a2)
output = []
a1.each do |i1|
a2.each do |i2|
output += [[i1, i2]]
end
end
output
end
def recurse_bounding_box(max_depth = 4, depth = 1)
width = (bounds.width - 15) / 2
height = (bounds.height - 15) / 2
left_top_corners = combine([5, bounds.right - width - 5],
[bounds.top - 5, height + 5])
left_top_corners.each do |lt|
bounding_box(lt, :width => width, :height => height) do
stroke_bounds
recurse_bounding_box(max_depth, depth + 1) if depth < max_depth
end
end
end
# Set up a bbox from the dashed line to the bottom of the page
bounding_box([0, cursor], :width => bounds.width, :height => cursor) do
recurse_bounding_box
end
C# HTML5 PDF Viewer SDK deployment on IIS in .NET place where you store XDoc.PDF.HTML5 Viewer correspond site-> Edit Permissions -> Security -> Group or user names -> Edit -> Add -> Add Everyone usersgiven
pdf security settings; advanced pdf encryption remover
6
Layout
Prawn has support for two-dimensional grid based layouts out of the box.
The examples show:
• How to define the document grid
• How to configure the grid rows and columns gutters
• How to create boxes according to the grid
12
layout/simple_grid.rb
The document grid on Prawn is just a table-like structure with a defined number of rows and
columns. There are some helpers to create boxes of content based on the grid coordinates.
define_grid accepts the following options which are pretty much self-explanatory: :rows,
:columns, :gutter, :row_gutter, :column_gutter
# The grid only need to be defined once, but since all the examples should be
# able to run alone we are repeating it on every example
define_grid(:columns => 5, :rows => 8, :gutter => 10)
text "We defined the grid, roll over to the next page to see its outline"
start_new_page
grid.show_all
We defined the grid, roll over to the next page to see its outline
40
0,0
0,1
0,2
0,3
0,4
1,0
1,1
1,2
1,3
1,4
2,0
2,1
2,2
2,3
2,4
3,0
3,1
3,2
3,3
3,4
4,0
4,1
4,2
4,3
4,4
5,0
5,1
5,2
5,3
5,4
6,0
6,1
6,2
6,3
6,4
7,0
7,1
7,2
7,3
7,4
19
layout/boxes.rb
After defined the grid is there but nothing happens. To start taking effect we need to use the grid
boxes.
grid has three different return values based on the arguments received. With no arguments it will
return the grid itself. With integers it will return the grid box at those indices. With two arrays it will
return a multi-box spanning the region of the two grid boxes at the arrays indices.
# The grid only need to be defined once, but since all the examples should be
# able to run alone we are repeating it on every example
define_grid(:columns => 5, :rows => 8, :gutter => 10)
grid(4, 0).show
grid(5, 1).show
grid([6, 2], [7, 3]).show
grid([4, 4], [7, 4]).show
grid([7, 0], [7, 1]).show
4,0
5,1
6,2:7,3
4,4:7,4
7,0:7,1
31
layout/content.rb
Now that we know how to access the boxes we might as well add some content to them.
This can be done by taping into the bounding box for a given grid box or multi-box with the
bounding_box method.
# The grid only need to be defined once, but since all the examples should be
# able to run alone we are repeating it on every example
define_grid(:columns => 5, :rows => 8, :gutter => 10)
grid([5, 0], [7, 1]).bounding_box do
text "Adding some content to this multi_box.\n" + " _ " * 200
end
grid(6, 3).bounding_box do
text "Just a little snippet here.\n" + " _ " * 10
end
Adding some content to this multi_box.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
Just a little snippet
here.
_ _ _ _ _ _ _
_ _ _
Documents you may be interested
Documents you may be interested