Tutorial: iText by Example

Selected examples: EPS, TIFF, Barcodes

Encapsulated PostScript:
Since version 1.2.4 of iText, it's possible to add EPS images into your document (as a com.lowagie.text.Image). EPS is an extension of the PostScript graphics file format developed by Adobe Systems. Support in iText for this format is very basic, but it should be useful for simple logos.
Example: java com.lowagie.examples.objects.images.tiff.EncapsulatedPostScript
Adding EPS to PDF: see eps.pdf
External resources for this example: parrot.ps tiger.ps
Go to top of the page
Tiff:
iText also supports TIFF. TIFF is the Tagged Image File Format. It was developed in 1986 by an industry committee chaired by the Aldus Corporation (now part of Adobe). Microsoft and Hewlett-Packard were also on the committee. It is unique in that it incorporates multiple compression techniques, allowing the user to specify the best format for a type of image, and that one file can contain multiple images. TIFFs are common in desktop publishing, faxing, and medical imaging applications.
The examples below can be used as standalone tools. Tiff2Pdf converts a TIFF file into a PDF file. If the TIFF has more than one image, a PDF with more than one page will be generated.
Example: java com.lowagie.examples.objects.images.tiff.Tiff2Pdf 12.tif 338814-00.tif even.tif odd.tif
Converts some tiff files to PDF: see 12.pdf 338814-00.pdf even.pdf odd.pdf
External resources for this example: 12.tif 338814-00.tif odd.tif even.tif
The OddEven example was inspired by a tiffmesh example, which is part of another SourceForge library TiffTools. Tiffmesh is a utility to mesh together two TIFF files containing odd and even pages into one big TIFF file. So if you use a scanner to scan in text from double-sided pages, you can scan the odd pages into one tiff and the even pages into another one. With Tiffmesh you can knit both files together into one tiff. With the OddEven-example/tool, you can do the same to combine both files into one PDF document.
Example: java com.lowagie.examples.objects.images.tiff.OddEven odd.tif even.tif combined.pdf
Combines 2 tiffs, one with odd, another with even pages into 1 combined PDF: see combined.pdf
External resources for this example: odd.tif even.tif
Go to top of the page
Barcodes:
iText also comes with a number of classes that generate barcodes. The following types are supported:
  • Barcode39: code 39 and code 39 extended
  • Barcode128:
    • CODE128 - plain barcode 128.
    • CODE128_UCC - support for UCC/EAN-128 with a full list of AI.
    • CODE128_RAW - raw mode. The code attribute has the actual codes from 0 to 105 followed by '\uffff' and the human readable text.
  • BarcodeEAN: EAN13, EAN8, UPCA, UPCE, and EAN with supplemental 5, EAN with supplemental 5, EAN with supplemental 2
  • BarcodeEANSUPP: takes 2 barcodes, an EAN/UPC and a supplemental
  • BarcodeInter25: interleaved 2 of 5
  • BarcodePostnet: postnet and planet
  • BarcodePDF417: the 2D barcode PDF417
  • BarcodeCodabar: codabar
This is an code example to generate an EAN-barcode with value 9780201615883:
PdfContentByte cb = writer.getDirectContent();
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCodeType(codeEAN.EAN13);
codeEAN.setCode("9780201615883");
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
document.add(new Phrase(new Chunk(imageEAN, 0, 0)));
Example: java com.lowagie.examples.objects.images.tiff.Barcodes
List with different barcodes: see barcodes.pdf
All barcode classes implement the abstract class Barcode. Please read the API documentation to see the defaults for every specific barcode type, and for the different methods to change the 'look' of the barcode. To add a barcode to a document with iText, convert it to an Image or PdfTemplate object using createImageWithBarcode(com.lowagie.text.pdf.PdfContentByte, java.awt.Color, java.awt.Color) or createTemplateWithBarcode(com.lowagie.text.pdf.PdfContentByte, java.awt.Color, java.awt.Color). The colors specify the color of the bars and text are:
barColor textColor Result
null null bars and text painted with current fill color
barColor null bars and text painted with barColor
null textColor bars painted with current color
text painted with textColor
barColor textColor bars painted with barColor
text painted with textColor
If you want a barcode without any text, just set the font to null with setFont(com.lowagie.text.pdf.BaseFont)
Note that you also can create a textless java.awt.Image of the barcode object with createAwtImage(java.awt.Color, java.awt.Color).
Go to top of the page



Amazon books:
amazon.co.uk-link

amazon.co.uk-link

amazon.co.uk-link