-1

I am trying to create pdf in java using pdfbox 1.8, but problem is I am not able to show CMYK image on pdf so I try to solution on same like below code:

File filePath = new File("C:/Users/msuryawanshi/Documents/10734730431625_C1LA.jpg");
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(new FileInputStream(filePath));
BufferedImage image = jpegDecoder.decodeAsBufferedImage();
imageUrl = "http://extranet.handgards.com/gs1/10734730431625_C1LA.jpg";
File f = new File("C:/Users/msuryawanshi/Documents/10734730431625_C1LA.jpg");
String url = "http://extranet.handgards.com/gs1/10734730431625_C1LA.jpg";
Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while (readers.hasNext()) {
    reader = (ImageReader) readers.next();
    if (reader.canReadRaster()) {
        break;
    }
}

//Stream the image file (the original CMYK image)
ImageInputStream input = ImageIO.createImageInputStream(f);
reader.setInput(input);
//Read the image raster
Raster raster = reader.readRaster(0, null);

//Create a new RGB image
BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(),
        BufferedImage.TYPE_4BYTE_ABGR);
//Fill the new image with the old raster
bi.getRaster().setRect(raster);
PDXObjectImage ximage = new PDPixelMap(document, bi);
contentStream.drawXObject(ximage, margin + 5, texty, 170, 100);

enter image description here

but image is not meaningful, I have attached output pdf and original image which i want display on my pdf. please help for the same.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Manojkumar
  • 31
  • 2
  • 5
  • 1
    I'm not able to understand from your question if you want to embed the CMYK version of the JPEG or not, either way - handling CMYK jpegs in Java is tricky. See http://stackoverflow.com/questions/8118712/java-cmyk-to-rgb-with-profile-output-is-too-dark/12132556#12132556 for a possible solution – iddo Dec 22 '16 at 09:50
  • 1
    PDFBox doesn't support embedding CMYK images at all. You might be able to embed it as an RGB image by using the twelvemonkeys library https://github.com/haraldk/TwelveMonkeys/ instead of Java ImageIO. – Tilman Hausherr Dec 22 '16 at 10:00
  • Hi @iddo i was try that solution but i am getting error atJpegImageParser parser = new JpegImageParser();( JpegImageParser cannot be resolved to a type )ByteSource byteSource = new ByteSourceFile(file);ByteSource cannot be resolved to a type, is there any jar needed? – Manojkumar Dec 22 '16 at 10:05
  • @Tilman probably you should post that as an answer. Even if it is negative. After all, no positive answer is to be expected. – mkl Dec 22 '16 at 10:12
  • @Manojkumar you should contact the original post author about that – iddo Dec 22 '16 at 12:25

1 Answers1

1

PDFBox doesn't support embedding CMYK images at all because java itself can't read such images. You might be able to embed it as an RGB image by using the twelvemonkeys library instead of Java ImageIO to read the JPEG into a BufferedImage. From there, just use PDPixelMap (in 1.8) or LosslessFactory (in 2.0).

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97