Text Style Size & Alignment Tutorial
In this tutorial you'll learn how to use different text attributes
for your reports.
Nerval Reports has a small subset of predefined
colors at the ReportColors class. Obviously, it probably won't have
all the desired colors one should expect to use at their reports. To
fulfill that, you can declare new colors inside document's head via a
call to
addColor function. There's no limit of how many colors
declarations you can do, but each created color object is only valid
(and safe) inside the report it was declared.
reportGenerator.beginDocumentHead();
lilac = reportGenerator.addColor(200, 162, 200, "lilac");
celestialBlue = reportGenerator.addColor(73, 151, 208, "celestialBlue");
reportGenerator.endDocumentHead();
At the head we can also define the font that
should be used (note that it's a hint, because not all
ReportGenerators supports font changes). Let's hint to use a non
serifed font (I prefer serifed fonts, but let's be different here).
reportGenerator.hintNonSerifFont();
A report is implemented based on a state machine
(those familiar with OpenGL will get it immediately). Each state
change will affect all operations after its call. For example, if one
changes the text color to another, this color will affect all texts
added with
addText calls (or similar) after that, until the
color is changed again. The same applies to alignment, font size,
bold and italic texts and everything that could be defined with a
'set*' call.
You can change the text color with the function
setTextColor. The color used can be either one predefined
at
ReportColors class, or any other declared at current
document's head. The color that is defined before any
setTextColor call is
ReportColors.DEFAULT_TEXT_COLOR.
reportGenerator.beginDocumentBody();
reportGenerator.setTextColor(ReportColors.BLUE);
reportGenerator.addText("This is a blue sentence.");
reportGenerator.addLineBreak();
reportGenerator.endDocumentBody();
It's possible to change text color inside the
same textual line with multiple
addText's combined with
setTextColor calls:
reportGenerator.addText("I'm with the previous defined color, ");
reportGenerator.setTextColor(celestialBlue);
reportGenerator.addText("but I'm with the new celestial blue ");
reportGenerator.setTextColor(ReportColors.RED);
reportGenerator.addText("and red I could be.");
reportGenerator.addLineBreak();
The use of font sizes is analogous to color's use.
For multiple format support, the sizes are defined in terms of how big
(or small) they are and should be one of those defined at
ReportFontSize class. From smaller to bigger, they are:
TINY, EXTRA_SMALL, SMALL, NORMAL, LARGE, EXTRA_LARGE, HUGE.
You can change font size within the same text line.
reportGenerator.setTextColor(ReportColors.DEFAULT_TEXT_COLOR);
reportGenerator.addText("I'm the default (normal) text size, ");
reportGenerator.setFontSize(ReportFontSize.LARGE);
reportGenerator.addText("and I can grow ");
reportGenerator.setFontSize(ReportFontSize.SMALL);
reportGenerator.addText("or shrink ");
reportGenerator.setFontSize(ReportFontSize.EXTRA_SMALL);
reportGenerator.addText("at your ");
reportGenerator.setFontSize(ReportFontSize.TINY);
reportGenerator.addText("desire.");
reportGenerator.setFontSize(ReportFontSize.NORMAL);
reportGenerator.addLineBreak();
For bold and italic changes, the same applies.
Bold and italic are defined as 'on/off' states, and should be
defined, respectively, with
setBold and
setItalic
calls.
reportGenerator.addText("We can ");
reportGenerator.setBold(true);
reportGenerator.addText("bold some text ");
reportGenerator.setBold(false);
reportGenerator.setItalic(true);
reportGenerator.addText("italic some other ");
reportGenerator.setBold(true);
reportGenerator.addText("and bold italic the rest.");
reportGenerator.addLineBreak();
reportGenerator.setItalic(false);
For text alignment we have some differences.
Although on some ReportGenerators it could work without that, it's a
good practice to always define groups of aligned text, with the group
block of
PARAGRAPH type. For example, to center align
some text:
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
reportGenerator.beginGroup(ReportGroupType.PARAGRAPH);
reportGenerator.addText("I'm a centered bold text in a centered paragraph aligned group");
reportGenerator.endGroup();
reportGenerator.setBold(false);
As you can see at the generated file, the paragraph
group always add a line break to its end. If you desire to use
different alignments at the same line, you should use the
INLINE group type. But beware, because it's very easy to
overlap
INLINE group texts with another
INLINE group.
reportGenerator.setTextAlignment(ReportTextAlignment.LEFT);
reportGenerator.beginGroup(ReportGroupType.INLINE);
reportGenerator.addText("I'm at the left line side");
reportGenerator.endGroup();
reportGenerator.setTextAlignment(ReportTextAlignment.RIGHT);
reportGenerator.beginGroup(ReportGroupType.INLINE);
reportGenerator.addText("and I'm at the right at the same line.");
reportGenerator.endGroup();
reportGenerator.addLineBreak();
Almost everything learned here are also valid
inside tables, the only exception being
INLINE groups inside
cells which won't work (you can get similar results using multiple
cells each one with a distinct alignment). Let's create a table with
some color, style size and alignment changes inside its elements. One
thing is important to note here: if you desire to set an alignment to
the whole cell, you can define it before the
beginTableElement, without the need of a group definition, as
it can act as a group itself.
reportGenerator.setTableBorderStyle(1.0f, ReportColors.BLACK);
reportGenerator.beginTable(3);
reportGenerator.setTextColor(ReportColors.WHITE);
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
/* A larger table header */
reportGenerator.beginTableHeaderRow();
reportGenerator.setFontSize(ReportFontSize.LARGE);
reportGenerator.setBold(true);
reportGenerator.addTableHeaderCell("A header BIG line", 3);
reportGenerator.endTableHeaderRow();
/* A normal, but centered and white, column titles */
reportGenerator.beginTableHeaderRow();
reportGenerator.setFontSize(ReportFontSize.NORMAL);
reportGenerator.addTableHeaderCell("Column A");
reportGenerator.addTableHeaderCell("Column B");
reportGenerator.addTableHeaderCell("Column C");
reportGenerator.endTableHeaderRow();
/* Reset color and bold */
reportGenerator.setBold(false);
reportGenerator.setTextColor(ReportColors.DEFAULT_TEXT_COLOR);
/* Let's define a row with three different aligned cells. */
reportGenerator.beginTableRow();
reportGenerator.setTextAlignment(ReportTextAlignment.LEFT);
reportGenerator.addTableCell("I'm at left");
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
reportGenerator.addTableCell("I'm a centered cell");
reportGenerator.setTextAlignment(ReportTextAlignment.RIGHT);
reportGenerator.addTableCell("I'm at right");
reportGenerator.endTableRow();
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
reportGenerator.beginTableRow();
/* Add a cell with multicolored text cell with one word italic. */
reportGenerator.beginTableCell();
reportGenerator.addText("A ");
reportGenerator.setTextColor(lilac);
reportGenerator.setItalic(true);
reportGenerator.addText("multicolored ");
reportGenerator.setItalic(false);
reportGenerator.setTextColor(celestialBlue);
reportGenerator.addText("text ");
reportGenerator.setTextColor(ReportColors.ORANGE);
reportGenerator.addText("cell.");
reportGenerator.endTableCell();
/* A cell with multiple font sizes. */
reportGenerator.setTextColor(ReportColors.DEFAULT_TEXT_COLOR);
reportGenerator.beginTableCell();
reportGenerator.setFontSize(ReportFontSize.EXTRA_LARGE);
reportGenerator.setBold(true);
reportGenerator.addText("Shout ");
reportGenerator.setBold(false);
reportGenerator.setFontSize(ReportFontSize.NORMAL);
reportGenerator.addText("and ");
reportGenerator.setFontSize(ReportFontSize.SMALL);
reportGenerator.addText("whisper ");
reportGenerator.setFontSize(ReportFontSize.EXTRA_SMALL);
reportGenerator.addText("lower ");
reportGenerator.setFontSize(ReportFontSize.TINY);
reportGenerator.addText("and lower.");
reportGenerator.endTableCell();
/* A right aligned cell with small text */
reportGenerator.setFontSize(ReportFontSize.SMALL);
reportGenerator.setTextAlignment(ReportTextAlignment.RIGHT);
reportGenerator.addTableCell("I'm a fully right-aligned cell, directly created with a addTableElement call avoiding the verbose begin-end block.");
reportGenerator.endTableRow();
reportGenerator.beginTableRow();
/* A cell with more than one paragraph, each one with distinct alignment. */
reportGenerator.beginTableCell();
/* (note that we are small) */
reportGenerator.setTextAlignment(ReportTextAlignment.RIGHT);
reportGenerator.setBold(true);
reportGenerator.beginGroup(ReportGroupType.PARAGRAPH);
reportGenerator.addText("April's 1st ");
reportGenerator.endGroup();
reportGenerator.setBold(false);
reportGenerator.setFontSize(ReportFontSize.NORMAL);
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
reportGenerator.beginGroup(ReportGroupType.PARAGRAPH);
reportGenerator.addText("This is the day some of us are fooled by 'expert' people.");
reportGenerator.endGroup();
reportGenerator.endTableCell();
/* A double cell */
reportGenerator.beginTableCell(2, 1);
reportGenerator.setTextAlignment(ReportTextAlignment.CENTER);
reportGenerator.beginGroup(ReportGroupType.PARAGRAPH);
reportGenerator.addText("Sunday, 01/01/1984");
reportGenerator.endGroup();
reportGenerator.setTextAlignment(ReportTextAlignment.RIGHT);
reportGenerator.beginGroup(ReportGroupType.PARAGRAPH);
reportGenerator.addText("The first day of 1984 year.");
reportGenerator.endGroup();
reportGenerator.endTableCell();
reportGenerator.endTableRow();
reportGenerator.endTable();
reportGenerator.endDocumentBody();
Here's the pdf generated by NervalReports' PDFReportGenerator:
And
here you can download the full code of this tutorial.