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:
- JPEG2000 is supported since PDF 1.5. The name of the filter is
- Although PDF supports images with LZW compression (used for GIFs), iText decodes GIF images into a raw image. If you create an
Imagein iText with a path to a GIF file, you'll get an image with filter
/FlateDecodein 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
/FlateDecodewill be used.
- BMP files will be stored as a series of compressed pixels using
- 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
/CCITTFaxDecodefilter 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
- JBIG2 uses the
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. A single image that contains transparent areas will be converted to two different images:
The opaque image,
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.