In an article posted in the BFO blog a year or so ago regarding text highlighting, we saw how to style a PDF by using the PDFStyle class. We will now look at formatting the text.
A simple way to place text
If you have played around with the PDF Library, you will have almost certainly already written something similar to the code below:
PDF pdf = new PDF();
PDFPage page = pdf.newPage("A4");
PDFFont baseFont = new StandardFont(StandardFont.COURIER);
PDFStyle baseStyle = new PDFStyle();
baseStyle.setFont(baseFont, 14);
baseStyle.setFillColor(Color.black);
page.setStyle(titleStyle);
String text = "The PDF times";
page.drawText(text, 50, 5);
This works fine for title and short text, but what about displaying longer text? Go
ahead and replace the
text value by a much larger body of text. Check the result.
Not that great right? That's because drawText isn't meant to handle long text.
The LayoutBox way
The basics
The class LayoutBox
is here to help you with long text. The idea is that a LayoutBox has a defined width and will
handle the line return to keep the text within this width. For instance:
String longText = "lorem ipsum ... ...";
//create a LayoutBox of 40% the size of the page
LayoutBox layoutBox = new LayoutBox(page.getWidth() * 0.4f);
layoutBox.addText(longText, someStyle, null);
page.drawLayoutBox(layoutBox, 5, 5);
Looking better? We can improve it further still with...
Inner-boxes
LayoutBox also allows you to create boxes within your LayoutBox. You can use
these boxes to insert a picture within your text like the following:
Box imgBox = layoutBox.addBoxLeft(
page.getWidth() * 0.2f, // box width
page.getWidth() * 0.2f, // box height
0 // clearing option
);
//loading a random picture from internet
URL url = new URL("http://lorempixel.com/200/200/business/PDF-times/");
//creating a PDFImage with it
PDFImage image = new PDFImage(url.openConnection().getInputStream());
imgBox.setImage(image);
layoutBox.addText(longText, someStyle, null);
page.drawLayoutBox(layoutBox, 5, 5);
You can also use the methods
addBoxFullWidth
,
addBoxRight
or
addBoxInline.
splitAt magic
Another handy thing you can do with LayoutBox is to use the
splitAt.
This method allows you to cut a LayoutBox at a given height and to get the leftovers in a new
LayoutBox ready to be added to your page. Here is an example of how to create a two-column
document
based on this method:
LayoutBox content;
//Fill the content box with text, picture, ...
//... ...
//
LayoutBox firstColumn, secondColumn;
//check if there is content to display
while(content.getHeight() > 0) {
System.out.println("spliting required!");
//if yes create a new page
page = pdfDoc.newPage("A4");
//cut the 2 columns to the right size
firstColumn = content.splitAt(page.getHeight() * 0.90f);
secondColumn = firstColumn.splitAt(page.getHeight() * 0.90f);
content = secondColumn.splitAt(page.getHeight() * 0.90f);
//add the columns to the page
page.drawLayoutBox(firstColumn, page.getWidth() * 0.05f, page.getHeight() * 0.95f);
page.drawLayoutBox(secondColumn, page.getWidth() * 0.55f, page.getHeight() * 0.95f);
}
And we are done for today!
Leo JeussetFreelance developer and BFO guest blogger
https://twitter.com/leojpod