Which image types are supported by iText?

Category: 
Tags: imagesimage typeiText 7

I want to add a PNG image to a PDF file, but I've read that PNG files aren't supported in PDF.

Does this mean that I can't use PNG files when using iText?

iText supports JPEG, JPEG2000, GIF, PNG, BMP, WMF, TIFF, CCITT and JBIG2 images. This doesn't mean that these images types are also supported in PDF. If a specific image type (for instance PNG) isn't supported in PDF, iText will convert it to an image type that is defined in the PDF specification.

  • JPEG images are kept as is by iText. You can take the content stream of an Image XObject of type JPEG, copy it into a file and you'll have a valid JPEG image. You can recognize these images by their filter: /DCTDecode.
  • JPEG2000 is supported since PDF 1.5. The name of the filter is JPXDecode.
  • Although PDF supports images with LZW compression (used for GIFs), iText decodes GIF images into a raw image. If you create an Image in iText with a path to a GIF file, you'll get an image with filter /FlateDecode in your PDF.
  • PNG isn't supported in PDF, which is why iText will also decode PNG images into rw images. If the color space of the image is DeviceGray and if the image only has 1 bit per component, CCITT will be used as compression and you'll recognize the filter /CCITTFaxDecode. Otherwise, the filter /FlateDecode will be used.
  • BMP files will be stored as a series of compressed pixels using /FlateDecode as filter.
  • WMF is special. If you insert a WMF file into a PDF document using iText, iText will convert that image into PDF syntax. Instead of adding an Image XObject, iText will create a form XObject.
  • When the image data is encoded using the CCITT facsimile standard, the /CCITTFaxDecode filter will be used. These are typically monochrome images with one bit per pixel.
  • TIFFs will be examined by iText. Depending on the TIFFs parameters, iText can decide to use /CCITTFaxDecode, /FlateDecode or even /DCTDecode as filter.
  • JBIG2 uses the /JBIG2Decode filter.

Normally, you don't need to worry about the image type. The Image class takes care of choosing the right compression method for you.

Extra remark: PDF supports transparency, but please be aware that transparent images as such aren't supported in PDF. An single image that contains transparent areas will be converted to two different images:

  1. The opaque image,

  2. An image mask.

This is inherent to PDF. If you add a transparent image to a PDF file, you can't extract that image from the PDF. Instead you'll have to abstract two images, the opaque version and its image mask.

Click this link if you want to see how to answer this question in iText 5.