White is a special color in the PDF file format. This is because of the way different colours work in the PDF file format. If you use color to paint over an existing color, 3 things could happen.
1. The color overwrites If you draw a green box over a red background, then the covered red bit is replaced by a green box.
2. The colors mix As happens with paints in real life, the colors mix so if you draw a white box over a red background you get a pink box where the red and white mix together.
3. The new color is transparent You draw an image and the background is regarded as transparent and shows that is behind the image.
Because the PDF file format tries to support different types of color, it allows all options to happen. In particular CMYK mode allows white to be treated as a transparent color – it is set in the GraphicsState. This allows PDF files to mimic qualities of ink rather than just draw over.
This is complicated enough to implement but working with Java adds two additional problems.
First of all, different types of BufferedImage exist in Java and they define white in different ways. The JPEG decoders will return a different type of image compared to building the image yourself. So, sometimes it is color zero, while other times it is value 255,255,255
Secondly, Java works in sRGB so data needs to be converted into that format from ICC or CMYK. When the color is defined with its own Key (Black) element, it is much easier to implement transparency on white rather than after it is converted to sRGB. And also Java does not always convert white to 255,255,255.
So white is a very important and complicated color and one of the many areas of the PDF file when things are not always as they first seem, especially manipulating them in Java.
This post is part of our “Understanding the PDF File Format” series. In each article, we aim to take a specific PDF feature and explain it in simple terms. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.