public final class LayoutBox extends Object
A LayoutBox
is a box for laying out text, which allows a great
deal more control over positioning than the standard
drawText
method.
A LayoutBox has a fixed width but no predefined height. Text and "Boxes" can
be added to the box, and when the box is complete it can be drawn onto a
page using the PDFPage.drawLayoutBox
method. At its simplest, the
following will create a single line of Text on the page.
PDFStyle style = new PDFStyle(); style.setFont(new StandardFont(StandardFont.HELVETICA), 12); style.setFillColor(Color.black); LayoutBox box = new LayoutBox(page.getWidth()-100); box.addText("Hello, World", style, Locale.getDefault()); page.drawLayoutBox(box, 50, page.getHeight()-50);
The LayoutBox class also allows "boxes" to be inserted into the flow, which can later be used to position images or similar items in the text. For example, the following code will produce an image in the top-left hand corner with text wrapping around it:
PDFStyle style = new PDFStyle(); style.setFont(new StandardFont(StandardFont.HELVETICA), 12); style.setFillColor(Color.black); LayoutBox box = new LayoutBox(page.getWidth()-100); LayoutBox.Box imagebox = box.addBoxLeft(100,100, PDFStyle.TEXTALIGN_BASELINE); imagebox.setImage(myimage); box.addText("Hello, World", style, Locale.getDefault()); page.drawLayoutBox(box, 50, page.getHeight()-50);
Images can also be drawn inline with the addBoxInline
method, or to
the right with the addBoxRight
method.
Modifier and Type | Class and Description |
---|---|
class |
LayoutBox.Box
A class representing a Box, several of which make up the visible
content of a LayoutBox.
|
class |
LayoutBox.Text
The Text class is a subclass of
LayoutBox.Box which
is specifically for displaying Text. |
Modifier and Type | Field and Description |
---|---|
static int |
CLEAR_LEFT
A flag indicating that the Box created by
addBoxLeft(float, float, int) or addBoxRight(float, float, int)
should always be flat against the left margin - it should have no content to
its left. |
static int |
CLEAR_NONE
A flag indicating that the Box created by
addBoxLeft(float, float, int) or addBoxRight(float, float, int)
does not require either a left or right margin to line up against. |
static int |
CLEAR_RIGHT
A flag indicating that the Box created by
addBoxLeft(float, float, int) or addBoxRight(float, float, int)
should always be flat against the right margin - it should have no content to
its right. |
Constructor and Description |
---|
LayoutBox(float width)
Create a new LayoutBox of the specified width.
|
LayoutBox(float width,
Locale locale)
Create a new LayoutBox of the specified width, and with the specified
Locale as the parent locale of the LayoutBox.
|
Modifier and Type | Method and Description |
---|---|
LayoutBox.Box |
addBoxFullWidth(float height)
Add a new box that takes the full width of the LayoutBox, less the width of
any left or right floating boxes.
|
LayoutBox.Box |
addBoxInline(float width,
float height,
int align)
Add a new Box which will be appear "inline" - ie. positioned in the same way
as the text.
|
LayoutBox.Box |
addBoxLeft(float width,
float height,
int clearflags)
Add a new Box which will float at the left of the LayoutBox.
|
LayoutBox.Box |
addBoxRight(float width,
float height,
int clearflags)
Add a new Box which will float at the right of the LayoutBox.
|
void |
addLineBreak(PDFStyle style)
Add a line-break in the specified style.
|
float |
addTab(float[] stops)
Add a horizontal tab to the LayoutBox.
|
LayoutBox.Text |
addText(char[] buf,
int off,
int len,
PDFStyle style,
Locale locale)
Add a line of text to the LayoutBox.
|
LayoutBox.Text |
addText(String string,
PDFStyle style,
Locale locale)
Add a line of text to the LayoutBox.
|
LayoutBox.Text |
addTextNoBreak(char[] buf,
int off,
int len,
PDFStyle style,
Locale locale)
Add a line of text to the LayoutBox.
|
LayoutBox.Text |
addTextNoBreak(String string,
PDFStyle style,
Locale locale)
Add a line of text to the LayoutBox.
|
void |
beginTag(String tag,
Map<String,Object> atts)
Open a structural tag on this LayoutBox.
|
void |
endTag()
Close a structural tag on this LayoutBox.
|
void |
flush()
Flush the flowbox.
|
LayoutBox.Box[] |
getBoxes()
Return the list of boxes which make up the LayoutBox.
|
float |
getHeight()
Return the height in points of the LayoutBox.
|
int |
getNumberOfLines()
Return the number of lines in the LayoutBox.
|
PDFStyle |
getStyle()
Return the style of the LayoutBox, as set by
setStyle(org.faceless.pdf2.PDFStyle) |
boolean |
isEmpty()
Return true if the LayoutBox is empty, false if it's not
|
boolean |
isFlushed()
Return true if the LayoutBox has been flushed - ie. if there are any items
of text that are still to be positioned.
|
void |
setStyle(PDFStyle style)
Set the default style of the box.
|
void |
setWordBreaksAllowed(boolean breaks)
Set whether word breaks are allowed between items added to the LayoutBox.
|
LayoutBox |
splitAt(float splitpos)
Split a LayoutBox into two boxes at the specified height.
|
public static final int CLEAR_NONE
addBoxLeft(float, float, int)
or addBoxRight(float, float, int)
does not require either a left or right margin to line up against.public static final int CLEAR_LEFT
addBoxLeft(float, float, int)
or addBoxRight(float, float, int)
should always be flat against the left margin - it should have no content to
its left.public static final int CLEAR_RIGHT
addBoxLeft(float, float, int)
or addBoxRight(float, float, int)
should always be flat against the right margin - it should have no content to
its right.public LayoutBox(float width)
width
- the width of the LayoutBox, in pointspublic LayoutBox(float width, Locale locale)
addText
methods.width
- the width of the LayoutBox, in pointslocale
- the overall locale of the LayoutBoxpublic void setStyle(PDFStyle style)
addText
,
addTextNoBreak
or addLineBreak
style
- the default style of the LayoutBoxgetStyle()
public LayoutBox.Text addText(String string, PDFStyle style, Locale locale)
Add a line of text to the LayoutBox. The text may be broken into
smaller units to fit the line, in which case the LayoutBox.Text.getNextTwin()
method
can be used to traverse through them.
Since 2.0 the text may contain newline (\n) characters, which act in the normal way
(prior to 2.0 it was necessary to call the addLineBreak(org.faceless.pdf2.PDFStyle)
method).
Tab (\t) characters are not recognised however - to insert a horizontal tab into the
text, you need to call the addTab(float[])
method, as this is the only way for the
system to know how wide each tab is to be.
string
- the text to display.style
- the style in which to display the textlocale
- the locale of the text. With locales where this is unlikely
to make a difference, ie. western european languages, this may be null
Text
object representing this string.public LayoutBox.Text addText(char[] buf, int off, int len, PDFStyle style, Locale locale)
addText(String,PDFStyle,Locale)
,buf
- the buffer containing the text to addoff
- the offset of the start of the meaningful content of the bufferlen
- the length of the meaningful content of the bufferstyle
- the style in which to display the textlocale
- the locale of the text. With locales where this is unlikely
to make a difference, ie. western european languages, this may be null
public void setWordBreaksAllowed(boolean breaks)
Set whether word breaks are allowed between items added to the LayoutBox.
By default word breaks are allowed at appropriate locations in any text
added via addText()
, and between any two calls to
addText()
or addTextNoBreak()
.
This method can be called to turn off those word-breaking opportunities,
and is required to suppress breaks between subsequent calls to
addTextNoBreak()
.
breaks
- whether to allow word breaks - the default value is true
PDFStyle.setLineBreakBehaviour(int)
public LayoutBox.Text addTextNoBreak(String string, PDFStyle style, Locale locale)
string
- the text to displaystyle
- the style in which to display the textlocale
- the locale of the text. With locales where this is unlikely
to make a difference, ie. western european languages, this may be null
Text
object representing this string.public LayoutBox.Text addTextNoBreak(char[] buf, int off, int len, PDFStyle style, Locale locale)
addTextNoBreak(String,PDFStyle,Locale)
,buf
- the buffer containing the text to addoff
- the offset of the start of the meaningful content of the bufferlen
- the length of the meaningful content of the bufferstyle
- the style in which to display the textlocale
- the locale of the text. With locales where this is unlikely
to make a difference, ie. western european languages, this may be null
Text
object representing this string.public float addTab(float[] stops)
Add a horizontal tab to the LayoutBox. Tabs are specified as an array
of floats which represent the position (in points) of the tab stop
from the left of the box. For instance, { 80, 100, 150 }
would cause the first cursor to move right to 80, 100 or 150 points
from the left of the LayoutBox, depending on how far in it already was.
If the cursor is past the last entry in the array, then tab stops
are assumed to continue over the same width to the right of the box.
The width of these stops is the same as the last specified width - so
in the example above, tab stops 200, 250, 300, 350 and so on are implied.
This means that for the simplest case - a tab stop every 50 points -
all you need to do is specify an array with a single float of { 50 }
.
If there are no further tab stops available on the current line, the cursor is moved to the start of the next line.
Note Tab stops only work with left-aligned text. Consequently, if the
alignment for the LayoutBox is anything other than TEXTALIGN_LEFT
,
an exception is thrown.
stops
- an array of one or more floats defining the position of tab
stops in points from the left edge of the LayoutBoxIllegalStateException
- if the LayoutBox is not left-aligned.public void addLineBreak(PDFStyle style)
style
- the style defining the font in which to add the linebreakpublic LayoutBox.Box addBoxInline(float width, float height, int align)
width
- the width of the rectangleheight
- the height of the rectanglealign
- how to align the text - one of PDFStyle.TEXTALIGN_TOP
,
PDFStyle.TEXTALIGN_MIDDLE
or PDFStyle.TEXTALIGN_BOTTOM
LayoutBox.Box
representing this objectpublic LayoutBox.Box addBoxFullWidth(float height)
height
- the height of the box.public LayoutBox.Box addBoxLeft(float width, float height, int clearflags)
width
- the width of the rectangleheight
- the height of the rectangleclearflags
- logical-or of zero or more of CLEAR_LEFT
or CLEAR_RIGHT
LayoutBox.Box
representing this objectpublic LayoutBox.Box addBoxRight(float width, float height, int clearflags)
width
- the width of the rectangleheight
- the height of the rectangleclearflags
- logical-or of zero or more of CLEAR_LEFT
or CLEAR_RIGHT
LayoutBox.Box
representing this objectpublic void beginTag(String tag, Map<String,Object> atts)
endTag()
See the PDFCanvas
version of this method for the full documnentation.PDFCanvas.beginTag(java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)
public void endTag()
beginTag()
See the PDFCanvas
version of this method for the full documnentation.PDFCanvas.beginTag(java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)
public boolean isEmpty()
public float getHeight()
flush()
method, otherwise there is a very good chance the
last line of text hasn't been positioned (and won't be included in the returned value).public boolean isFlushed()
getBoxes()
or the getHeight()
methods are called.flush()
public void flush()
isFlushed()
public LayoutBox.Box[] getBoxes()
flushed
, some of these items may not have a position
specified.LayoutBox.Box
objects which make up the visible contents
of the LayoutBox.isFlushed()
,
flush()
public PDFStyle getStyle()
setStyle(org.faceless.pdf2.PDFStyle)
public LayoutBox splitAt(float splitpos)
public int getNumberOfLines()
Copyright © 2001-2019 Big Faceless Organization