PdfBox - PDColorSpaceFactory.createColorSpace (document, iccColorSpace) löst Nullpunkterexception aus

Ich versuche, eine PDF-Datei zu erstellen, die ein einzelnes Bild auf einer einzelnen Seite enthält. Der schwierige Teil besteht darin, einen benutzerdefinierten (in einer separaten Datei definierten) CMYK-Farbraum zu verwenden.

Ich habe versucht, @ anzuruf

PDColorSpaceFactory.createColorSpace (Dokument, iccColorSpace)

, aber immer nullpointerexception. Ich habe es geschafft, das Problem bis zum Konstruktor zu verfolgen:

public PDICCBased( PDDocument doc )
{
    array = new COSArray();
    array.add( COSName.ICCBASED );
    array.add( new PDStream( doc ) );
}

Das PDICCBased Objekt hatStro Feld und es ist offensichtlich nicht gesetzt. Also, wenn es heißt:

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;
}

Eine NullPointerException wird ausgelöst.

Bin ich etwas vermisst? Gibt es einen anderen \ besseren Weg, um einen PDF-CMYK-Farbraum zu erstellen?

Aktualisiert 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;
}

Das hat es geschafft, den Farbraum zu erstellen.

Die Erstellung von ColorSpace aus einem benutzerdefinierten IC und dessen Anwendung auf ein bestimmtes Bild:

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);

Die Erstellung der 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();

Hoffe das hilft

Antworten auf die Frage(2)

Ihre Antwort auf die Frage