You are previewing iText in Action: Creating and Manipulating PDF.
O'Reilly logo
iText in Action: Creating and Manipulating PDF

Book Description

Imagine a publisher who wants to "stamp" his ebooks on the fly with the name of the buyer (to discourage sharing). Such a publisher would (and we know one who does) use iText for the task. Developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation will find this book unique in content

and

readability. Based on ongoing examples that encourage learning "in action," they will finally understand PDF and learn how to build applications that produce professional, high-quality PDF documents. While the basic functionality of iText is easy to acquire, this book lowers the learning curve for more advanced functionality. It explains how to use iText to create/manipulate PDF documents on-the-fly in one or more of the following situations:

  • Due to time or size, the PDF documents can't be produced manually

  • The content of the document must be calculated or based on user input.

  • The content needs to be customized or personalized.

  • The PDF content needs to be served in a web environment.

  • Documents are to be created in "batch process" mode.

  • All the examples are written in Java, but they can be easily adapted to .NET by developers using one of the .NET ports: iTextSharp (C#) or iText.NET (#J). While iText is a free Java library and the examples are written from the point of view of the Java developer, nine out of ten examples can be run by .NET developers with only minimal changes.

    Table of Contents

    1. Copyright
      1. Dedication
    2. Preface
    3. Acknowledgments
    4. About this Book
      1. How to use this book
      2. Roadmap
      3. Who should read this book?
      4. Code conventions
      5. Software requirements and downloads
      6. Author Online
      7. About the title
      8. About the cover illustration
    5. 1. Introduction
      1. 1. iText: when and why
        1. 1.1. The history of iText
          1. 1.1.1. How iText was born
          2. 1.1.2. iText today
            1. Ease of use and flexibility
            2. iText licensing
          3. 1.1.3. Beyond Java
            1. iText.NET and iTextSharp
            2. iText and pdftk
            3. iText and ColdFusion
            4. Using iText in PHP, Python, Ruby
        2. 1.2. iText: first contact
          1. 1.2.1. Running the examples in the book
          2. 1.2.2. Experimenting with the iText toolbox
        3. 1.3. An almost-true story
          1. 1.3.1. Some Foobar fiction
          2. 1.3.2. A document daydream
            1. Making a flyer
            2. Composing a study guide
          3. 1.3.3. Welcoming the student
            1. Producing documents in different languages
            2. Drawing a city map
            3. Adding street names to the map
            4. Adding interactive layers to the map
          4. 1.3.4. Producing and processing interactive documents
            1. Making documents interactive
            2. Adding watermarks and page numbers
            3. Using iText in a web application
            4. Creating and filling forms using iText
          5. 1.3.5. Making the dream come true
        4. 1.4. Summary
      2. 2. PDF engine jump-start
        1. 2.1. Generating a PDF document in five steps
          1. 2.1.1. Creating a new document object
            1. Page size
            2. Page color
            3. Page margins
          2. 2.1.2. Getting a DocWriter instance
            1. Creating the same document in different formats
            2. Choosing an OutputStream
          3. 2.1.3. Opening the document
            1. The PDF header
            2. Adding metadata
          4. 2.1.4. Adding content
            1. Using building blocks
            2. Low-level PDF generation
            3. Using java.awt.Graphics2D
          5. 2.1.5. Closing the document
            1. Design pattern
            2. PDF cross-reference table and trailer
        2. 2.2. Manipulating existing PDF files
          1. 2.2.1. Reading an existing PDF file
            1. Document properties
            2. Page size and rotation
            3. Reading damaged PDFs
            4. PdfReader and memory use
            5. Retrieving bookmarks
            6. Reading metadata
          2. 2.2.2. Using PdfStamper to change document properties
          3. 2.2.3. Using PdfStamper to add content
            1. Filling in a form
            2. Adding content to pages
            3. Inserting new pages
          4. 2.2.4. Introducing imported pages
          5. 2.2.5. Using imported pages with PdfWriter
          6. 2.2.6. Manipulating existing PDF files with PdfCopy
            1. Concatenating PDF files
            2. Selected pages
          7. 2.2.7. Concatenating forms with PdfCopyFields
          8. 2.2.8. Summary of the manipulation classes
        3. 2.3. Creating PDF in multiple passes
          1. 2.3.1. Stamp first, then copy
          2. 2.3.2. Copy first, then stamp
          3. 2.3.3. Stamp, copy, stamp
        4. 2.4. Summary
      3. 3. PDF: why and when
        1. 3.1. A document history
          1. 3.1.1. Adobe and documents
            1. The ancestors of PDF
            2. The Portable Document Format
          2. 3.1.2. The Acrobat family
          3. 3.1.3. The intellectual property of the PDF specification
        2. 3.2. Types of PDF
          1. 3.2.1. Traditional PDF
          2. 3.2.2. Tagged PDF
          3. 3.2.3. Linearized PDF
          4. 3.2.4. PDFs preserving native editing capabilities
          5. 3.2.5. PDF types that became an ISO standard
            1. PDF/X
            2. PDF/A and XMP
            3. PDF/E
          6. 3.2.6. PDF forms, FDF, and XFDF
          7. 3.2.7. XFA and XDP
          8. 3.2.8. Rules of thumb
        3. 3.3. PDF version history
          1. 3.3.1. Changing the user unit
          2. 3.3.2. PDF content and compression
            1. Existing PDF documents and compression
          3. 3.3.3. Encryption
            1. Encrypting existing PDF documents
            2. PDF passwords
            3. Overview of the permissions
            4. Encrypting a PDF document generated from scratch
            5. Decrypting an existing PDF file
        4. 3.4. Summary
    6. 2. Basic building blocks
      1. 4. Composing text elements
        1. 4.1. Wrapping Strings in text elements
          1. 4.1.1. The atomic building block: com.lowagie.text.Chunk
          2. 4.1.2. An ArrayList of Chunks: com.lowagie.text.Phrase
          3. 4.1.3. A sequence of Phrases: com.lowagie.text.Paragraph
        2. 4.2. Adding extra functionality to text elements
          1. 4.2.1. External and internal links: com.lowagie.text.Anchor
          2. 4.2.2. Lists and ListItems: com.lowagie.text.List/ListItem
          3. 4.2.3. Automatic bookmarking: com.lowagie.text.Chapter/Section
        3. 4.3. Chunk characteristics
          1. 4.3.1. Measuring and scaling
          2. 4.3.2. Lines: underlining and striking through text
          3. 4.3.3. TextRise: sub- and superscript
          4. 4.3.4. Simulating italic fonts: skewing text
          5. 4.3.5. Changing font and background colors
          6. 4.3.6. Simulating bold fonts: stroking vs. filling
        4. 4.4. Chunks and space distribution
          1. 4.4.1. The split character
          2. 4.4.2. Hyphenation
          3. 4.4.3. Changing the CharSpace ratio
        5. 4.5. Anchors revisited
          1. 4.5.1. Remote Goto
          2. 4.5.2. Local Goto
        6. 4.6. Generic Chunk functionality
          1. 4.6.1. Drawing custom backgrounds and lines
          2. 4.6.2. Implementing custom functionality
          3. 4.6.3. Building an index
        7. 4.7. Making a flyer (part 1)
        8. 4.8. Summary
      2. 5. Inserting images
        1. 5.1. Standard image types
          1. 5.1.1. BMP, EPS, GIF, JPEG, PNG, TIFF, and WMF
          2. 5.1.2. TIFF with multiple pages
          3. 5.1.3. Animated GIFs
        2. 5.2. Working with java.awt.Image
        3. 5.3. Byte arrays with image data
          1. 5.3.1. Raw image data
          2. 5.3.2. CCITT compressed images
          3. 5.3.3. Creating barcodes
          4. 5.3.4. Working with com.lowagie.text.pdf.PdfTemplate
        4. 5.4. Setting image properties
          1. 5.4.1. Adding images to the document
            1. Alignment and wrapping
            2. Images and Chunks
            3. Image borders
            4. Image sequence
          2. 5.4.2. Translating, scaling, and rotating images
            1. Adding an image at an absolute position
            2. Scaling images
            3. Image resolution
            4. Scale to fit a rectangle
            5. Image rotation
          3. 5.4.3. Image masks
        5. 5.5. Making a flyer (part 2)
          1. 5.5.1. Getting the Image instance
          2. 5.5.2. Setting the border, the alignment, and the dimensions
          3. 5.5.3. The resulting PDF
        6. 5.6. Summary
      3. 6. Constructing tables
        1. 6.1. Tables in PDF: PdfPTable
          1. 6.1.1. Your first PdfPTable
          2. 6.1.2. Changing the width and alignment of a PdfPTable
            1. Relative versus absolute width of the PdfPTable
            2. Column widths
            3. Spacing before and after a PdfPTable
          3. 6.1.3. Adding PdfPCells to a PdfPTable
            1. Alignment of the cell content
            2. Indentation and leading of the cell content
            3. Padding of the cell content
            4. Changing the row height
            5. Changing cell borders and colors
            6. Changing the rotation of a PdfPCell
          4. 6.1.4. Special PdfPCell constructors
            1. Nested tables
            2. Tables and images
          5. 6.1.5. Working with large tables
            1. Tables spanning multiple pages
            2. Repeating the table header and footer
            3. Memory management for large tables
          6. 6.1.6. Adding a PdfPTable at an absolute position
            1. Comparing document.add() with writeSelectedRows()
            2. Splitting a PdfPTable vertically
        2. 6.2. Alternatives to PdfPTable
          1. Advantages of the Table class
        3. 6.3. Composing a study guide (part 1)
          1. 6.3.1. The data source
          2. 6.3.2. Generating the PDF
        4. 6.4. Summary
      4. 7. Constructing columns
        1. 7.1. Retrieving the current vertical position
        2. 7.2. Adding text to ColumnText
          1. 7.2.1. Different ways to add text to a column
            1. ColumnText.addText(Phrase p)
            2. ColumnText.setSimpleColumn(Phrase p, ... )
            3. ColumnText.setText(Phrase p)
          2. 7.2.2. Keeping paragraphs together
          3. 7.2.3. Adding more than one column to a page
            1. Regular columns
            2. Irregular columns
            3. Text mode versus composite mode
        3. 7.3. Composing ColumnText with other building blocks
          1. 7.3.1. Combining text mode with images and tables
          2. 7.3.2. ColumnText in composite mode
        4. 7.4. Automatic columns with MultiColumnText
          1. 7.4.1. Regular columns with MultiColumnText
          2. 7.4.2. Irregular columns with MultiColumnText
        5. 7.5. Composing a study guide (part 2)
        6. 7.6. Summary
    7. 3. PDF text and graphics
      1. 8. Choosing the right font
        1. 8.1. Defining a font
          1. 8.1.1. Using the right terminology
          2. 8.1.2. Standard Type 1 fonts
            1. The iText Font class
            2. Defining the font style and color
            3. Defining the font size
            4. Embedded versus nonembedded fonts
        2. 8.2. Introducing base fonts
          1. 8.2.1. Working with an encoding
            1. Creating a BaseFont object
          2. 8.2.2. Class BaseFont and Type 1 fonts
            1. Adobe Font Metrics files
            2. PostScript Font Binary files
            3. Printer Font Metric files
          3. 8.2.3. Embedding Type 3 fonts
          4. 8.2.4. Working with TrueType fonts
          5. 8.2.5. Working with OpenType fonts
            1. OpenType font with PostScript outlines
            2. OpenType font with TrueType outlines
        3. 8.3. Composite fonts
          1. 8.3.1. What is Unicode?
            1. Using Unicode in CID fonts
          2. 8.3.2. Introducing Chinese, Japanese, Korean (CJK) fonts
            1. Using other CMaps
          3. 8.3.3. Embedding CIDFonts
          4. 8.3.4. Using TrueType collections
        4. 8.4. Summary
      2. 9. Using fonts
        1. 9.1. Other writing directions
          1. 9.1.1. Vertical writing
          2. 9.1.2. Writing from right to left
        2. 9.2. Sending a message of peace (part 1)
        3. 9.3. Advanced typography
          1. 9.3.1. Handling diacritics
            1. Changing the character advance
            2. Changing a proportional font into a monospace font
          2. 9.3.2. Dealing with ligatures
            1. Ligatures in the Latin alphabet
            2. Arabic ligatures
        4. 9.4. Automating font creation and selection
          1. 9.4.1. Getting a Font object from the FontFactory
            1. Registering separate fonts
            2. Registering font directories
          2. 9.4.2. Automatic font selection
            1. Automatic selection of Greek symbols
            2. Automatic selection of glyphs
        5. 9.5. Sending a message of peace (part 2)
        6. 9.6. Summary
      3. 10. Constructing and painting paths
        1. 10.1. Path construction and painting operators
          1. 10.1.1. Seven path construction operators
          2. 10.1.2. Path-painting operators
            1. Stroking versus filling
            2. Nonzero winding number vs. even-odd rule
            3. Bézier curves
            4. Convenience methods to draw shapes
        2. 10.2. Working with iText’s direct content
          1. 10.2.1. Direct content layers
          2. 10.2.2. PdfPTable and PdfPCell events
            1. Implementing PdfPCellEvent
            2. Implementing PdfPTableEvent
            3. Combining table and cell events
        3. 10.3. Graphics state operators
          1. 10.3.1. The graphics state stack
          2. 10.3.2. Changing the characteristics of a line
            1. Line cap and line join styles
            2. Line dash pattern
            3. Overview
            4. Literal PDF syntax
        4. 10.4. Changing the coordinate system
          1. 10.4.1. The CTM
            1. Doing the math
            2. Affine transformations
          2. 10.4.2. Positioning external objects
            1. Image XObjects
            2. PostScript XObjects
            3. PdfTemplates
        5. 10.5. Drawing a map of a city (part 1)
          1. 10.5.1. The XML/SVG source file
          2. 10.5.2. Parsing the SVG file
        6. 10.6. Summary
      4. 11. Adding color and text
        1. 11.1. Adding color to PDF files
          1. 11.1.1. Device colorspaces
          2. 11.1.2. Separation colorspaces
          3. 11.1.3. Painting patterns
            1. Tiling patterns
            2. Shading patterns
          4. 11.1.4. Using color with basic building blocks
        2. 11.2. The transparent imaging model
          1. 11.2.1. Transparency groups
          2. 11.2.2. Isolation and knockout
          3. 11.2.3. Applying a soft mask to an image
        3. 11.3. Clipping content
        4. 11.4. PDF’s text state
          1. 11.4.1. Text objects
            1. Text-positioning and text-showing operators
            2. Text-state operators
          2. 11.4.2. Convenience methods to position and show text
            1. Caveats
        5. 11.5. The map of Foobar (part 2)
        6. 11.6. Summary
      5. 12. Drawing to Java Graphics2D
        1. 12.1. Obtaining a Java.awt.Graphics2D instance
          1. 12.1.1. A simple example from Sun’s tutorial
            1. The 2D Graphics tutorial trail
            2. Integrating iText into this example
          2. 12.1.2. Mapping AWT fonts to PDF fonts
          3. 12.1.3. Drawing glyph shapes instead of using a PDF font
        2. 12.2. Two-dimensional graphics in the real world
          1. 12.2.1. Exporting Swing components to PDF
          2. 12.2.2. Drawing charts with JFreeChart
        3. 12.3. PDF’s optional content
          1. 12.3.1. Making content visible or invisible
          2. 12.3.2. Adding structure to layers
          3. 12.3.3. Using a PdfLayer
          4. 12.3.4. Optional content membership
          5. 12.3.5. Changing the state of a layer with an action
          6. 12.3.6. Optional content in XObjects and annotations
        4. 12.4. Enhancing the map of Foobar
          1. 12.4.1. Defining the layers for the map and the street names
          2. 12.4.2. Combining iText and Apache Batik
          3. 12.4.3. Adding tourist information to the map
        5. 12.5. Summary
    8. 4. Interactive PDF
      1. 13. Browsing a PDF document
        1. 13.1. Changing viewer preferences
          1. 13.1.1. Setting the page layout
          2. 13.1.2. Choosing the page mode
          3. 13.1.3. Viewer options
        2. 13.2. Visualizing thumbnails
          1. 13.2.1. Changing the page labels
          2. 13.2.2. Changing the thumbnail image
        3. 13.3. Adding page transitions
        4. 13.4. Adding bookmarks
          1. 13.4.1. Creating destinations
            1. public PdfDestination(int type)
            2. public PdfDestination(int type, float parameter)
            3. public PdfDestination(int type, float left, float top, float zoom)
            4. public PdfDestination(int type, float left, float bottom, float right, float top)
          2. 13.4.2. Constructing an outline tree
          3. 13.4.3. Adding actions to an outline tree
          4. 13.4.4. Retrieving bookmarks from an existing PDF file
          5. 13.4.5. Manipulating bookmarks in existing PDF files
        5. 13.5. Introducing actions
          1. 13.5.1. Actions to go to an internal destination
          2. 13.5.2. Actions to go to an external destination
          3. 13.5.3. Triggering actions from events
          4. 13.5.4. Adding JavaScript to a PDF document
          5. 13.5.5. Launching an application
        6. 13.6. Enhancing the course catalog
        7. 13.7. Summary
      2. 14. Automating PDF creation
        1. 14.1. Creating a page
          1. 14.1.1. Adding empty pages
          2. 14.1.2. Defining page boundaries
          3. 14.1.3. Reordering pages
        2. 14.2. Common page event functionality
          1. 14.2.1. Overview of the PdfPageEvent methods
          2. 14.2.2. Adding a header and a footer
          3. 14.2.3. Adding page X of Y
          4. 14.2.4. Adding watermarks
          5. 14.2.5. Creating an automatic slide show
          6. 14.2.6. Automatically creating bookmarks
          7. 14.2.7. Automatically creating a table of contents
        3. 14.3. Alternative XML solutions
          1. 14.3.1. Writing a letter on company stationery
            1. Writing the page events
            2. Writing the code that parses the XML
            3. Batch-processing the XML
          2. 14.3.2. Parsing a play
          3. 14.3.3. Parsing (X)HTML
          4. 14.3.4. Using HtmlWorker to parse HTML snippets
        4. 14.4. Enhancing the course catalog (part 2)
        5. 14.5. Summary
      3. 15. Creating annotations and fields
        1. 15.1. Introducing annotations
          1. 15.1.1. Simple annotations
            1. Text annotations
            2. Link annotations
            3. Movie annotations
          2. 15.1.2. Other types of annotations
            1. File attachments
            2. Free text annotations
            3. Line, square, and circle annotations
          3. 15.1.3. Adding annotations to a chunk or image
        2. 15.2. Creating an AcroForm
          1. 15.2.1. Button fields
            1. Adding a pushbutton
            2. Adding radio buttons
            3. Adding check boxes
            4. Convenience classes for button fields
          2. 15.2.2. Creating text fields
          3. 15.2.3. Creating choice fields
        3. 15.3. Submitting a form
          1. 15.3.1. Choosing field names
          2. 15.3.2. Adding actions to the pushbuttons
            1. Submitting as HTML
            2. Submitting as FDF
            3. Submitting as XFDF
            4. Submitting as PDF
            5. Reset, hide, and show fields
          3. 15.3.3. Adding actions
        4. 15.4. Comparing HTML and PDF forms
        5. 15.5. Summary
      4. 16. Filling and signing AcroForms
        1. 16.1. Filling in the fields of an AcroForm
          1. 16.1.1. Retrieving information about the fields (part 1)
          2. 16.1.2. Filling fields
          3. 16.1.3. Retrieving information from a field (part 2)
          4. 16.1.4. Flattening a PDF file
          5. 16.1.5. Optimizing the flattening process
        2. 16.2. Working with FDF and XFDF files
          1. 16.2.1. Reading and writing FDF files
          2. 16.2.2. Reading XFDF files
        3. 16.3. Signing a PDF file
          1. 16.3.1. Adding a signature field to a PDF file
          2. 16.3.2. Using public and private keys
          3. 16.3.3. Generating keys and certificates
          4. 16.3.4. Signing a document
            1. KeyStore, PrivateKey and Certificate[]
            2. Creating the signature
            3. Validating the PDF in Adobe Reader
            4. Using a smart card for signing
        4. 16.4. Verifying a PDF file
        5. 16.5. Summary
      5. 17. iText in web applications
        1. 17.1. Writing PDF to the ServletOutputStream: pitfalls
          1. 17.1.1. Solving problems related to content type-related problems
          2. 17.1.2. Troubleshooting the blank-page problem
            1. Server-related problems
            2. Browser-related problems
          3. 17.1.3. Problems with PDF generated from JSP
          4. 17.1.4. Avoiding multiple hits per PDF
          5. 17.1.5. Workaround for the timeout problem
        2. 17.2. Putting the theory into practice
          1. 17.2.1. A personalized course catalog
          2. 17.2.2. Creating a learning agreement form
            1. Using FieldPositioningEvents
            2. Using JavaScript to manipulate fields
            3. Using file selection fields
          3. 17.2.3. Reading an FDF file in a JSP page
        3. 17.3. Summary
      6. 18. Under the hood
        1. 18.1. Inside iText and PDF
          1. 18.1.1. Factors of success
          2. 18.1.2. The file structure of a PDF document
            1. The file trailer
            2. The cross-reference table
          3. 18.1.3. Basic PDF objects
          4. 18.1.4. Climbing up the object tree
            1. The catalog dictionary
            2. Retrieving the bookmarks
            3. The pages/page dictionary
        2. 18.2. Extracting and editing text
          1. 18.2.1. Reading a page’s content stream
          2. 18.2.2. Why iText doesn’t do text extraction
          3. 18.2.3. Why you shouldn’t use PDF as a format for editing
        3. 18.3. Rendering PDF
          1. 18.3.1. How to print a PDF file programmatically
            1. Sending PDF to the printer
            2. Using a viewer application to print a PDF
          2. 18.3.2. Printing a PDF file in a web application
        4. 18.4. Manipulating PDF files
          1. 18.4.1. Toolbox tools
          2. 18.4.2. The learning agreement (revisited)
            1. Changing the submit URL
            2. Extracting a file from an FDF
        5. 18.5. Summary
    9. A. Class diagrams
      1. A.1. PDF/RTF/HTML creation classes
      2. A.2. PDF manipulation classes
      3. A.3. Text element classes
      4. A.4. Image classes
      5. A.5. Barcode classes
      6. A.6. Table classes
      7. A.7. Font classes
      8. A.8. Color classes
      9. A.9. PdfObject classes
    10. B. Creating barcodes
      1. B.1. Barcodes to identify products
        1. com.lowagie.text.pdf.BarcodeEAN
        2. com.lowagie.text.pdf.BarcodeEANSUPP
        3. com.lowagie.text.pdf.Barcode128
        4. com.lowagie.text.pdf.BarcodeInter25
      2. B.2. Barcodes for postal services and other industries
        1. com.lowagie.text.pdf.BarcodePostnet
        2. com.lowagie.text.pdf.Barcode39
        3. com.lowagie.text.pdf.Codabar
      3. B.3. Barcode properties
        1. Overview of barcode properties
      4. B.4. Two-dimensional barcodes
        1. com.lowagie.text.pdf.BarcodePDF417
    11. C. Open parameters
    12. D. Signing a PDF with a smart card
    13. E. Dealing with exceptions
      1. E.1. iText-specific exception classes
        1. E.1.1. com.lowagie.text.BadElementException
        2. E.1.2. com.lowagie.text.DocumentException
      2. E.2. Standard Java exceptions
        1. E.2.1. java.io.IOException
        2. E.2.2. java.lang.RuntimeException
        3. E.2.3. Converting checked exceptions
      3. E.3. Virtual machine errors
        1. E.3.1. java.lang.OutOfMemoryError
        2. E.3.2. Class or method not found error
    14. F. Pdf/X, Pdf/A, and tagged PDF
      1. F.1. PDF/X
      2. F.2. PDF/A
        1. F.2.1. Creating an XMP metadata stream
        2. F.2.2. Existing PDF files and XMP metadata
      3. F.3. Tagged PDF
        1. F.3.1. Standard structure types
        2. F.3.2. Marked content
      4. F.4. To be continued
    15. G. Resources
      1. PDF in general
      2. Publications by Adobe Systems Incorporated
      3. Font-related bibliography and sites
      4. iText-related links
      5. Links to PDF tools mentioned in the book
      6. Limited list of other projects and products using iText