[ 
https://issues.apache.org/jira/browse/PDFBOX-3823?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16048342#comment-16048342
 ] 

Tilman Hausherr commented on PDFBOX-3823:
-----------------------------------------

Please try this code:

{code:java}
    public static PDImageXObject createFromCMYKImage(PDDocument document, 
BufferedImage cmykImage)
            throws IOException
    {
        int height = cmykImage.getHeight();
        int width = cmykImage.getWidth();

        WritableRaster raster = cmykImage.getRaster();
        byte[] data = new byte[width * height * 4];
        int byteIdx = 0;
        int[] pixel = null;
        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                pixel = raster.getPixel(x, y, pixel);
                for (int c = 0; c < 4; ++c)
                {
                    data[byteIdx++] = (byte) pixel[c];
                }
            }
        }

        return prepareImageXObject(document, data, width, height, 8, 
PDDeviceCMYK.INSTANCE);
    }

    private static PDImageXObject prepareImageXObject(PDDocument document,
            byte[] byteArray, int width, int height, int bitsPerComponent,
            PDColorSpace initColorSpace) throws IOException
    {
        //pre-size the output stream to half of the input
        ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length 
/ 2);

        Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE);
        filter.encode(new ByteArrayInputStream(byteArray), baos, new 
COSDictionary(), 0);

        ByteArrayInputStream encodedByteStream = new 
ByteArrayInputStream(baos.toByteArray());
        return new PDImageXObject(document, encodedByteStream, 
COSName.FLATE_DECODE,
                width, height, bitsPerComponent, initColorSpace);
    }
{code}


> Question about forming a PDF document, including CMYK images for printing.
> --------------------------------------------------------------------------
>
>                 Key: PDFBOX-3823
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3823
>             Project: PDFBox
>          Issue Type: Task
>          Components: PDModel
>    Affects Versions: 2.0.6
>            Reporter: Mikhail Semionenko
>            Assignee: Tilman Hausherr
>            Priority: Minor
>              Labels: CMYK
>             Fix For: 2.0.7, 3.0.0
>
>         Attachments: Screen Shot 2017-06-09 at 11.37.22.png
>
>
> It is necessary to form a PDF document, which in the future will be sent to 
> print the company's printers. There are several ICC files provided by 
> companies for printing.
> I have RGB images that should be in CMYK after adding to PDF.
> My code is now for drawing an image:
> ImageObject = LosslessFactory.createFromImage (document, image);
> ContentStream.drawImage (imageObject, position.x, position.y, 
> imageSize.width, imageSize.height);
> I'm wondering whether it is possible to display images in a document in 
> ColorSpace CMYK? Unfortunately, I'm not find working examples yet.
> If so, how can I get PDImageXObject in CMYK ColorSpace from BufferedImage in 
> RGB ColorSpace?
> When I try to create PDImageXObject instance from CMYK image file I get 
> exception:
> javax.imageio.IIOException: Unsupported Image Type



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to