Tutorial: iText by Example

Text

PDF Syntax:
PDF Syntax to write text looks like this:
BT
  /F13 12 Tf
  288 720 Td
  (ABC) Tj
ET
The example above write ABC (Tj) at a certain position (Tj) using a certain font (/F13) with a certain size (12). Text State is a subset of the graphics state and again iText offers lots of methods so that you don't have to write PDF syntax manually, but before looking at the lists of the text state, text positioning and text showing operators, you must know about the text object operators:
BT beginText()
ET endText()
If you use any method mentioned below outside a beginText/endText block, your PDF reader will give you a 'damaged PDF file' warning or error. Don't forget to use these methods!
Go to top of the page
Text State:
There are 9 graphics state parameters that only affect text (see table 5.1 in the PDF Reference Manual). In the table below, I sum up the parameters (italic), the corresponding operators (bold, see table 5.2 in the Reference Manual) and the corresponding iText method to change the text state.
Tc Tc setCharacterSpacing(float)
Tw Tw setWordSpacing(float)
Th Th setHorizontalScaling(float)
Tl TL setLeading(float)
Tf Tf setFontAndSize(com.lowagie.text.pdf.BaseFont, float)
Tfs
Tmode Tr setTextRenderingMode(int)
The text rendering mode is one of the following values: See the chapter on Chunks for an example.
Trise Ts setTextRise(float)
Tk PdfGState.setTextKnockout(boolean)
Text also has it's own coordinate system: text space. It is defined by the text matrix Tm and the text state parameters Tfs, Th and Trise, which together form the transformation matrix text space to user space. This is the list of Text-positioning parameters (see also Table 5.5 in the PDF Reference Manual):
Td moveText(float, float)
Td moveTextWithLeading(float, float)
Tm setTextMatrix(float, float) setTextMatrix(float, float, float, float, float, float)
T* newlineText()
Finally there are some text-showing operators (see Table 5.6 of the PDF Reference Manual).
Tj showText(java.lang.String)
' newlineShowText(java.lang.String)
" newlineShowText(float, float, java.lang.String)
TJ showText(com.lowagie.text.pdf.PdfTextArray)
iText also adds some extra methods that combine different methods that are summed up above: showTextKerned(java.lang.String), showTextAligned(int,%20java.lang.String,%20float,%20float,%20float) and showTextAlignedKerned(int,%20java.lang.String,%20float,%20float,%20float).
This is a simple examples that writes some text at absolute positions:
Example: java com.lowagie.examples.directcontent.text.Text
Adding text at absolute positions: see text.pdf
In this example, you see that graphics state methods also apply to text state: text glyphs are shapes and if you want to change the color, you have to change the 'colorfill':
Example: java com.lowagie.examples.directcontent.text.Logo
Draws the iText logo: see logo.pdf
Go to top of the page
Fonts:
The first part of Chapter 5 of the PDF Reference Manual deals with Text State; the second part (starting with section 5.4) tells you all about fonts.
In this tutorial fonts are discussed in 3 chapters of a separate part:
  1. Font objects
  2. Font characteristics
  3. Getting Fonts
Go to top of the page



Amazon books:
amazon.co.uk-link