使用pdfbox在pdf上显示CMYK图像

时间:2016-12-22 09:40:21

标签: java spring-mvc pdf pdf-generation pdfbox

我正在尝试使用pdfbox 1.8在java中创建pdf,但问题是我无法在pdf上显示CMYK图像,所以我尝试解决方法同样如下代码:

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

但是图片没有意义,我附上了输出pdf和想要在我的pdf上显示的原始图像。请帮忙。

1 个答案:

答案 0 :(得分:0)

PDFBox根本不支持嵌入CMYK图像,因为java本身无法读取此类图像。您可以使用twelvemonkeys library而不是Java ImageIO将JPEG嵌入到BufferedImage中,将其嵌入RGB图像。从那里,只需使用PDPixelMap(在1.8中)或LosslessFactory(在2.0中)。