IText Jumpstart Tutorial
IText Jumpstart Tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Jumpstart Tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Hello PDF World!
Creating a PDF programmatically
Get the PdfWriter
PdfWriter writer = new PdfWriter(dest);
Get the PdfDocument
PdfDocument pdf = new PdfDocument(writer);
Get the Document
Document document = new Document(pdf);
Add Text : Hello PDF!
document.add(new Paragraph("Hello PDF World!"));
Close the Document
document.close();
3 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Basic Building blocks
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Building blocks
Two fold approach to create and manipulate PDFs
High Level
Low Level
Mix of High and Low Level
5 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
High level approach hides complexity of PDF syntax
Setting a Font
Default font is Helvetica
Standard Type 1 fonts: 14 fonts recognized by PDF viewers
Adding a Paragraph
// Create a PdfFont
PdfFont font =
PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
// Add a Paragraph
Paragraph paragraph = new Paragraph("iText in Bangkok");
Paragraph.setFont(font);
document.add(paragraph);
6 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
Set Font properties
Font Size
Italic or bold
Font Color
//Code
Paragraph para = new Paragraph(“iText”);
para.setStrokeColor(Color.RED).setStrokeWidth(0.5f)
para.setFontSize(24);
7 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
Adding lists
// Create a List
List list = new List();
// List items are idented by 12 user units
list.setSymbolIndent(12);
// Creates a bulleted list
list.setListSymbol("\u2022");
list.setFont(font);
// Add ListItem objects
list.add(new ListItem(“Monday”));
list.add(new ListItem(“Tuesday”));
list.add(new ListItem(“Wednesday”)) ;
// Add the list to document
document.add(list);
document.close();
8 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
Adding Images to PDF
Use of ImageDataFactory
detects the type of image that is passed
processes it so that it can be used in a PDF
document.add(p);
9 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
Adding a Table
Code
Table table = new Table(new float[]{4, 1, 3, 4, 3, 3});
table.setWidthPercent(100);
10 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of High Level APIs
Some other high level methods
Rotating a page
Setting Page Margins
Setting Text Alignment
Setting hyphenation
11 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF syntax and structure
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
PDF is a binary format
8 types of objects
Boolean
Numeric
String (Literal & Hexadecimal string)
Name
Array Dictionary
Stream
Null
13 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Comments are preceded by “%”
%%EOF – End of File marker
%PDF-X.Y – PDF version number
14 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Boolean objects
true
false
Numeric objects
Integer
Real
Strings
Preceded by a slash eg: /iText
Can contain any character
Name
Literal strings: enclosed by parentheses ()
Hexadecimal strings: enclosed by angle brackets <>
15 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Array
Collection of PDF objects
Heterogeneous
Enclosed in square brackets []
Dictionary
Key-Value pairs
Enclosed in Double angle brackets <<>>
Streams
Sequence of bytes of unlimited length
All streams are indirect
16 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Indirect objects
Unique identifier : is a positive integer
Generation number
Positive integer, zero inclusive
Starts at 0
Enclosed by obj and endobj
Example:
321 0 obj
(An indirect object)
endobj
This is an indirect string object with id “321” and generation number “0”. This
object can be referenced from somewhere else in the PDF by calling the id
and the generation number followed by “R”
<< /Value 321 0 R >>
17 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
PDF Structure has 4 sections :
Header
Body
XREF (Cross-reference Table)
Trailer
18 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Header
First line depicts the version
Replaced by /Version in the catalog dictionary (PDF 1.4+)
Second line indicating this is a binary file
19 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
XREF table
Starts with keyword “xref”
One entry per indirect object
20 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Trailer
Keyword trailer
Last line is %%EOF
21 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Body
Sequence of indirect objects
Fonts & Pages
22 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Document structure
Catalog is the Root dictionary
Contains references to Page Tree, AcroForm, Outlines, Metadata, StructTreeRoot
Metadata
Contains information about the document
Author, Title, Creation Date, Modification Date, etc
23 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Page Tree
Defines the order of the pages
Two types of nodes : Page tree nodes & Page nodes
Pages
Display information
User Units, MediaBox, Rotation, etc
Annotations
Content Stream(s)
Describes appearance of a page
Contains operators and operands
24 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
Operator
Keyword specifying an action
Drawing a line, moving the cursor, etc
Only meaningful in the content stream
25 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
26 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
PDF Syntax & Structure
RUPS
iText tool to inspect PDF files
Reading and Updating PDF Syntax
AGPL
27 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Low Level
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
Writes to a PDF content stream
PDF operators mapped to methods ()
m moveTo() method
l lineTo() method
S stroke() method
29 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
Use of Tranformation matrix
drawing objects in a new co-ordinate system
concatMatrix() method
parameters of this method are elements of a transformation
matrix
a b 0 //3rd column is fixed since we work in 2D
30 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
Graphics state
Properties such as the current transformation matrix, line width, stroke
color, fill color etc.
Default line width is 1 user unit
Default stroke color is black
Text state
Subset of the graphics state
Properties related to text i.e. text matrix, font and size etc
31 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
32 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
Color Spaces
As per PDF specification (ISO-32000)
Implemented under separate classes of iText
33 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
34 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Use of Low Level Approach
1 canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight());
2 canvas.beginText()
3 .setFontAndSize(..)
4 .setLeading(14 * 1.2f)
5 .moveText(70, -40);
6 for (String s : text) {
7 //Add text and move to the next line
8 canvas.newlineShowText(s);
9 }
10 canvas.endText();
35 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Combination of Both Approaches
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
Combining Low Level and High level approach for complex
tasks
37 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
38 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
39 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
40 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
IEventHandler interface
handleEvent() method
41 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
42 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Event Handlers and Renderers
43 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Annotations
44 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Annotations
ISO-32000-2 defines 28 different annotation types
Two are deprecated in PDF 2.0
iText can add all 26
Example of adding Text annotation
45 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Acroforms
Allows adding form fields
Text fields, List fields, Checkboxes, Radio buttons, Push buttons, Signature
fields
46 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Acroforms
Form Creation
Mostly created manually (Adobe software, LibreOffice, or any other GUI
tool)
Can be done programmatically
47 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Acroforms
Creating an acroform
PdfAcroForm form = PdfAcroForm.getAcroForm(PdfDocument d,Boolean b);
48 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Acroforms
Flattening of forms
End user is not allowed to change information in the PDF
Removes the user interactivity
Widget annotations are replaced with their content
49 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Adding content to Existing PDFs
1. Watermarks
2. Header & Footer
3. Merging PDFs
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Watermarks
Text or Images
Angle of watermark
Opacity of watermark
showTextAligned() method does the heavy loading
51 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Header and Footer
Read an existing PDF
PdfDocument pdf = new PdfDocument(PdfReader obj,PdfWriter obj);
Document document = new Document(pdf);
Add Header
canvas.beginText()
.setFontAndSize(..) //set the Font and its size
.moveText(width,height)
.showText("I want to believe")
.endText();
52 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Header & Footer
Add Footer
//Draw footer line
canvas.setStrokeColor(Color.BLACK)
.setLineWidth(.2f)
.moveTo(width, 20)
.lineTo(width, 20)
.stroke();
53 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Merging PDFs
Merging content of 2 PDF documents
Partial Merging
Selective pages from 2 documents are to be merged
PdfMerger merge(PdfDocument from, List<Integer> pages)
PdfMerger merge(PdfDocument from, int fromPage, int toPage)
54 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Merging PDFs
//1. Define new merger
PdfMerger merger = new PdfMerger(newPdf);
55 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Merging PDFs
Merging Forms
PDF can contain only one form
Merging is done using PdfPageFormCopier Class
Duplicate form fields need to be renamed
56 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Viewer Preferences
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Viewer Preferences
Set default behaviour for document
Setting Page Mode : for panel visibility
PdfName.UseNone
PdfName.UseOutlines
PdfName.UseThumbs
PdfName.FullScreen
PdfName.UseOC
PdfName.UseAttachments
58 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Viewer Preferences
59 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Viewer Preferences
Use of PdfViewerPreferences
Viewer related preferences
setFitWindow(), setHideMenuBar(), setHideToolbar(),
setCenterWindow(), setDisplayDocTitle(), setNonFullScreenPageMode(),
setDirection(), setViewArea() etc
Printer related preferences
setPrintArea(), setPrintScaling(),setNumCopies() etc
pdf.getCatalog().setPageLayout(PdfName.TwoColumnRight);
pdf.getCatalog().setPageMode(PdfName.UseThumbs);
pdf.getCatalog().setViewerPreferences(preferences);
60 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Metadata
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Metadata
62 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Metadata
XMP MetaData
Metadata store as XML inside a PDF
More Flexibility
63 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
iText 7 modules
pdfSweep
I/O Barcodes
Forms Signatures
Kernel Layout
pdfTest
pdfCalligraph
PDF/A
pdfInvoice
AsianFonts RUPS
pdfDebug
Part of iText 7 Core package, covered by AGPL license
Optional modules
© 2015, iText Group NV, iText Software Corp., iText Software BVBA
Thank You!
Log on to http://itextpdf.com/
Log on to http://developers.itextpdf.com/
65 Jumpstart tutorial
© 2015, iText Group NV, iText Software Corp., iText Software BVBA