我試圖創建一個PDF在單個頁面上有一個單一的圖像。棘手的部分是使用自定義(在單獨的文件中定義)CMYK顏色空間。PdfBox - PDColorSpaceFactory.createColorSpace(document,iccColorSpace)throws nullpointerexception
我試着打電話給
PDColorSpaceFactory.createColorSpace(文件,iccColorSpace)
但繼續得到NullPointerException異常。我已經成功地跟蹤這個問題到構造函數:
public PDICCBased(PDDocument doc)
{
array = new COSArray();
array.add(COSName.ICCBASED);
array.add(new PDStream(doc));
}
的PDICCBased對象有流領域,這顯然沒有設置。因此,當它被調用時:
public static PDColorSpace createColorSpace(PDDocument doc, ColorSpace cs) throws IOException
{
PDColorSpace retval = null;
if(cs.isCS_sRGB())
{
retval = PDDeviceRGB.INSTANCE;
}
else if(cs instanceof ICC_ColorSpace)
{
ICC_ColorSpace ics = (ICC_ColorSpace)cs;
PDICCBased pdCS = new PDICCBased(doc);
retval = pdCS;
COSArray ranges = new COSArray();
for(int i=0; i<cs.getNumComponents(); i++)
{
ranges.add(new COSFloat(ics.getMinValue(i)));
ranges.add(new COSFloat(ics.getMaxValue(i)));
}
PDStream iccData = pdCS.getPDStream();
OutputStream output = null;
try
{
output = iccData.createOutputStream(); <<<<<<<<<-------------
output.write(ics.getProfile().getData());
}
finally
{
if(output != null)
{
output.close();
}
}
pdCS.setNumberOfComponents(cs.getNumComponents());
}
else
{
throw new IOException("Not yet implemented:" + cs);
}
return retval;
}
引發NullPointerException。
我錯過了什麼嗎?有沒有更好的方法來創建PDF使用CMYK色彩空間?
更新createColorSpace:
public static PDColorSpace createColorSpace(PDDocument doc, ColorSpace cs) throws IOException
{
PDColorSpace retval = null;
if(cs.isCS_sRGB())
{
retval = PDDeviceRGB.INSTANCE;
}
else if(cs instanceof ICC_ColorSpace)
{
ICC_ColorSpace ics = (ICC_ColorSpace)cs;
// CREATING MANUALLY THE COS ARR ****************************
COSArray cosArray = new COSArray();
cosArray.add(COSName.ICCBASED);
PDStream pdStream = new PDStream(doc);
cosArray.add(pdStream.getStream());
// USING DIFFERENT CONSTRUTOR *******************************
PDICCBased pdCS = new PDICCBased(cosArray);
retval = pdCS;
COSArray ranges = new COSArray();
for(int i=0; i<cs.getNumComponents(); i++)
{
ranges.add(new COSFloat(ics.getMinValue(i)));
ranges.add(new COSFloat(ics.getMaxValue(i)));
}
PDStream iccData = pdCS.getPDStream();
OutputStream output = null;
try
{
output = ((COSStream)iccData.getCOSObject()).createFilteredStream();
output.write(ics.getProfile().getData());
}
finally
{
if(output != null)
{
output.close();
}
}
pdCS.setNumberOfComponents(cs.getNumComponents());
}
else
{
throw new IOException("Not yet implemented:" + cs);
}
return retval;
}
,從創建的顏色空間的伎倆。
ColorSpace中的從自定義ICC創建並將其應用到給定的圖像:
ICC_ColorSpace iccColorSpace = new ICC_ColorSpace(ICC_Profile.getInstance("C:\\...\\USWebCoatedSWOP.icc"));
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), iccColorSpace, null);
image = op.filter(image, null);
的PDF的創建:
PDDocument document = new PDDocument();
PDColorSpace colorSpace = createColorSpace(document, iccColorSpace);
PDPage blankPage = new PDPage(new PDRectangle(100, 100));
document.addPage(blankPage);
PDPageContentStream pdPageContentStream = new PDPageContentStream(document, blankPage);
PDXObjectImage pdxObjectImage = new PDPixelMap(document, image);
pdxObjectImage.setColorSpace(colorSpace);
pdPageContentStream.drawXObject(pdxObjectImage, 0, 0, imagePostScriptWidth, imagePostScriptHeight);
pdPageContentStream.close();
document.save(byteArrayOutputStream);
document.close();
希望這有助於。
您能否包含錯誤的堆棧跟蹤? –