Write CMYK image in PDF
Question
I need to add a CMYK Image (java.awt.BufferedImage
) to a Pdf-Document with iText.
I'm trying to do it with:
com.lowagie.text.Image img = Image.getInstance(BufferedImage, bgColor);
This produces an RGB image in the resulting PDF. (and I suppose it's a bug, because it just ignores ColorModel
). However I could use:
com.lowagie.text.Image img = Image.getInstance(byte[] rawData);
And it produces a correct CMYK-Image in PDF. But for the second case I need to convert java.awt.BufferedImage
in ByteArray
. I cannot do it with ImageIO.write(ByteArrayOutputStream)
. I also cannot do it with com.sun.image.codec.jpeg.JPEGImageEncoder
because I must use OpenJDK
.
Any ideas how can I achieve the correct behavior to write a CMYK image in PDF using iText?
Solution
So basically what you're asking is how to convert a BufferedImage
to a byte[]
to print to PDF?
BufferedImage img; // your image to be printed
String formatName; // name of the image format (see ImageIO docs)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( img, formatName, baos);
byte[] rawData = baos.toByteArray();
You should be able to use that for the CMYK-image as you had in your original post:
com.lowagie.text.Image img = Image.getInstance(byte[] rawData);