Itext in Action Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
Covers iText 5 SECOND EDITION Bruno Lowagie MANNING Praise for the First Edition Each aspect is explained with numerous examples that can be applied to real-world problems right away. —Ulf Ditmer, JavaRanch Any developer who is making serious use of iText would be a fool not to buy this book. —Dave Gilbert, jfree.org Thorough and complete ... will be a long-running, valuable resource for iText and PDF. —Alan Dennis Software Architect, MyFamily.com One of the best technical books I have ever read! Great work! —Oliver Zeigermann Technical Trainer, CoreMedia AG I wholeheartedly recommend it. —Doug James eReporting Team Lead, Benefitfocus.com, Inc. Impressive! It provides depth without all the noise. —Justin Lee, President, Antwerkz Inc. Valuable to any developer using PDF. —Stuart Caborn, Consultant, Thoughtworks Licensed to Bruno Lowagie <[email protected]> Licensed to Bruno Lowagie <[email protected]> iText in Action Second Edition BRUNO LOWAGIE MANNING Greenwich (74° w. long.) Licensed to Bruno Lowagie <[email protected]> For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 180 Broad Street Suite 1323 Stamford, CT 06901 Email: [email protected] ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co.Development editor:Katharine Osborne 180 Broad Street, Suite 1323 Copyeditor: Andy Carroll Stamford, CT 06901 Cover designer: Marija Tudor Typesetter: Gordan Salinovic ISBN 9781935182610 Printed in the United States of America 12345678910–MAL –151413121110 Licensed to Bruno Lowagie <[email protected]> To Ingeborg, Inigo, and Jago Licensed to Bruno Lowagie <[email protected]> Licensed to Bruno Lowagie <[email protected]> brief contents PART 1CREATING PDF DOCUMENTS FROM SCRATCH .................1 1 ■ Introducing PDF and iText 3 2 ■ Using iText’s basic building blocks 20 3 ■ Adding content at absolute positions 57 4 ■ Organizing content in tables 93 5 ■ Table, cell, and page events 122 PART 2MANIPULATING EXISTING PDF DOCUMENTS...............157 6 ■ Working with existing PDFs 159 7 ■ Making documents interactive 194 8 ■ Filling out interactive forms 236 PART 3ESSENTIAL ITEXT SKILLS............................................281 9 ■ Integrating iText in your web applications 283 10 ■ Brightening your document with color and images 317 11 ■ Choosing the right font 349 12 ■ Protecting your PDF 380 vii Licensed to Bruno Lowagie <[email protected]> viii BRIEF CONTENTS PART 4UNDER THE HOOD.....................................................411 13 ■ PDFs inside-out 413 14 ■ The imaging model 452 15 ■ Page content and structure 493 16 ■ PDF streams 526 Licensed to Bruno Lowagie <[email protected]> contents preface xvii preface to the first edition xviv acknowledgments xxi about this book xxiii about the title xxviii about the cover illustration xxix PART 1CREATING PDF DOCUMENTS FROM SCRATCH......1 Introducing PDF and iText 3 1 1.1 Things you can do with PDF 4 1.2 Working with the examples in this book 6 1.3 Creating a PDF document in five steps with iText 8 Creating a new Document object 9 ■ Getting a PdfWriter instance 12 ■ Opening the Document 13 ■ Adding content 15 ■ Closing the Document 17 1.4 Summary 19 ix Licensed to Bruno Lowagie <[email protected]> x CONTENTS Using iText’s basic building blocks 20 2 2.1 Illustrating the examples with a real-world database 21 2.2 Adding Chunk, Phrase, Paragraph, and List objects 22 The Chunk object: a String, a Font, and some attributes 23 ■ The Phrase object: a List of Chunks with leading 25 ■ Paragraph object: a Phrase with extra properties and a newline 30 ■ Distributing text over different lines 34 ■ The List object: a sequence of Paragraphs called ListItem 37 The DrawInterface: vertical position marks, separators, and tabs 41 2.3 Adding Anchor, Image, Chapter, and Section objects 46 The Anchor object: internal and external links 47 ■ Chapter and Section: get bookmarks for free 49 ■ The Image object: adding raster format illustrations 51 ■ Summary 56 Adding content at absolute positions 57 3 3.1 Introducing the concept of direct content 58 Direct content layers 58 ■ Graphics state and text state 60 ■ A real- world database: three more tables 62 3.2 Adding text at absolute positions 67 Convenience method: PdfContentByte.showTextAligned() 67 Convenience method: ColumnText.showTextAligned() 70 3.3 Working with the ColumnText object 74 Using ColumnText in text mode 75 ■ Using ColumnText in composite mode 81 3.4 Creating reusable content 84 Image XObjects 84 ■ The PdfTemplate object 87 3.5 Summary 92 Organizing content in tables 93 4 4.1 Constructing tables 94 Your first PdfPTable 94 ■ PdfPTable properties 95 4.2 Changing the properties of a cell 98 PdfPCell in text mode 99 ■ PdfPCell in composite mode 107 4.3 Dealing with large tables 112 Repeating headers and footers 112 ■ Splitting tables 113 Memory management for LargeElement implementations 115 4.4 Adding a table at an absolute position 116 Working with writeSelectedRows() 116 ■ Wrapping tables in columns 119 4.5 Summary 121 Licensed to Bruno Lowagie <[email protected]> CONTENTS xi Table, cell, and page events 122 5 5.1 Decorating tables using table and cell events 123 Implementing the PdfPTableEvent interface 123 ■ Implementing the PdfPCellEvent interface 125 ■ Combining table and cell events 128 5.2 Events for basic building blocks 133 Generic Chunk functionality 134 ■ Paragraph events 138 ■ Chapter and Section events 138 ■ Page order and blank pages 141 5.3 Overview of the page boundaries 142 The media box 143 ■ The crop box 145 ■ Other page boundaries 147 5.4 Adding page events to PdfWriter 148 Adding a header and a footer 148 ■ Solving the “page X of Y” problem 150 ■ Adding a watermark 152 ■ Creating a slideshow 154 5.5 Summary 155 PART 2MANIPULATING EXISTING PDF DOCUMENTS ...157 Working with existing PDFs 159 6 6.1 Accessing an existing PDF with PdfReader 160 Retrieving information about the document and its pages 160 Reducing the memory use of PdfReader 163 6.2 Copying pages from existing PDF documents 164 Importing pages 165 ■ Scaling and superimposing pages 167 N-up copying and tiling PDF documents 170 6.3 Adding content with PdfStamper 173 Adding content at absolute positions 173 ■ Creating a PDF in multiple passes 175 ■ Adding company stationery to an existing document 176 Inserting pages into an existing document 178 ■ Filling out a PDF form 179 6.4 Copying pages with PdfCopy 186 Concatenating and splitting PDF documents 187 ■ PdfCopy versus PdfSmartCopy 189 ■ Concatenating forms 191 6.5 Summary 192 Making documents interactive 194 7 7.1 Introducing actions 195 Document-navigation actions 195 ■ Explicit destinations 199 JavaScript in PDF documents 202 ■ More actions 205 Licensed to Bruno Lowagie <[email protected]> xii CONTENTS 7.2 Adding bookmarks 208 Creating bookmarks for a new document 208 ■ Retrieving bookmarks from an existing document 210 ■ Adding bookmarks to an existing document 212 ■ Concatenating documents with bookmarks 213 ■ Open parameters 214 7.3 Creating annotations 215 Text annotations 215 ■ Link annotations 220 ■ File attachments 222 ■ Stamp, line, and rectangle annotations 224 7.4 JavaScript programming in PDF 226 Triggering JavaScript from a button 226 ■ Showing and hiding an annotation 227 ■ A popup triggered by a button that doesn’t need to be pushed 229 ■ Additional actions 231 ■ A PDF calculator 232 7.5 Summary 234 Filling out interactive forms 236 8 8.1 Introducing AcroForms 237 8.2 Selecting states or trigger actions with button fields 238 Radio fields and radio buttons 238 ■ Check boxes 240 Pushbuttons 241 8.3 Filling in data with text fields 244 Creating text fields 244 ■ Filling out text fields 247 ■ Text fields and fonts 249 ■ Validating text fields 252 8.4 Selecting options with choice fields 252 Creating lists and combo boxes 253 ■ Manipulating lists and combo boxes 255 8.5 Refining the form-filling process 255 Choosing field names 256 ■ Optimizing the filling process 257 Partial form flattening 258 ■ Customized form flattening 260 8.6 Introducing the XML Forms Architecture (XFA) 264 Static XFA forms 265 ■ Dynamic XFA forms 270 8.7 Preserving the usage rights of Reader-enabled forms 275 Reader-enabling a form using Adobe Acrobat 276 ■ Filling out Reader-enabled forms using iText 277 8.8 Summary 278 Licensed to Bruno Lowagie <[email protected]> CONTENTS xiii PART 3ESSENTIAL ITEXT SKILLS ................................281 Integrating iText in your web applications 283 9 9.1 Creating a PDF from a servlet