Tilman, thank you very much for your response. I tried to implement your suggestion but unfortunately without luck. It just produces:

6 0 obj
<<
/Cs1 /DeviceGray
/Cs2 /DeviceN
>>
endobj

Here is a working example of my trial. I am indeed using PDFBox version 1.8.8 and Java 1.8.0_40-ea.

packagecom.here.devicentest;

importorg.apache.pdfbox.cos.*;
importorg.apache.pdfbox.exceptions.COSVisitorException;
importorg.apache.pdfbox.io.RandomAccessBuffer;
importorg.apache.pdfbox.pdmodel.PDDocument;
importorg.apache.pdfbox.pdmodel.PDPage;
importorg.apache.pdfbox.pdmodel.PDResources;
importorg.apache.pdfbox.pdmodel.common.PDRectangle;
importorg.apache.pdfbox.pdmodel.common.function.PDFunctionType4;
importorg.apache.pdfbox.pdmodel.edit.PDPageContentStream;
importorg.apache.pdfbox.pdmodel.font.PDFont;
importorg.apache.pdfbox.pdmodel.font.PDType1Font;
importorg.apache.pdfbox.pdmodel.graphics.color.*;

importjava.io.IOException;
importjava.io.OutputStream;
importjava.lang.String;
importjava.lang.System;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Map;

public classDeviceNTest {

    public static voidmain(String[] args) {

        try{

            WritePDF("MyDeviceN_Test.pdf");
            System.out.println("Done Writing!");

        }catch(IOException e) {
            e.printStackTrace();
        }catch(COSVisitorException e) {
            e.printStackTrace();
        }

    }

    public static voidWritePDF(String filename)throwsIOException, 
COSVisitorException {

        // Construct the PDF document
        PDDocument doc =newPDDocument();

        // Construct the only page.
        PDPage pdpage =newPDPage();

        // Specify the media box.
        PDRectangle mediabox =newPDRectangle(50,50);
        pdpage.setMediaBox( mediabox );

        // Specify the fonts.
        PDFont currentfont = PDType1Font.HELVETICA;

        // Specify the color spaces.
        Map<String, PDColorSpace> colorspaces =newHashMap<String, 
PDColorSpace>();

        // Specify DeviceGray for text.
        PDColorSpace cs1 =newPDDeviceGray();

        // Add the color sapce to the color space map.
        colorspaces.put("Cs1", cs1);

        // Specify DeviceN for a rectangle.
        /*
            DeviceN
            - Color Names
            - Alternate Color Space
            - Tint Transform Function
            - Attributes
         */
        PDDeviceN cs2 =newPDDeviceN();

        // - Color Names
        
cs2.setColorantNames(Arrays.asList("Cyan","Magenta","Yellow","Black","Orange","Green"));

        // - Alternate Color Space
        cs2.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);

        // - Tint Transform
        /*
            Function Type 4
            - Domain
            - Range
            - Length
            - Type
            - Stream (containing the PS code).
         */
        // - Domain
        COSArray domains =newCOSArray();
        domains.setFloatArray(new float[]{0,1,0,1,0,1,0,1,0,1,0,1});

        // - Range
        COSArray ranges =newCOSArray();
        ranges.setFloatArray(new 
float[]{0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f});

        // - Dictionary
        COSDictionary functiondata =newCOSDictionary();

        functiondata.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
        functiondata.setItem(COSName.DOMAIN, domains);
        functiondata.setItem(COSName.RANGE, ranges);
        functiondata.setItem(COSName.TYPE, COSName.FUNCTION);

        // - Stream
        String functionText ="{pop pop}";

        COSStream functionStream 
=newCOSStream(functiondata,newRandomAccessBuffer());
        OutputStream out = functionStream.createUnfilteredStream();
        out.write(functionText.getBytes("US-ASCII"));
        out.close();

        // Construct the tint transform function.
        PDFunctionType4 tinttransformfunction = (PDFunctionType4) 
PDFunctionType4.create(functionStream);

        // Add it to the color space.
        cs2.setTintTransform(tinttransformfunction);

        // - Attributes (just an empty dictionary)
        PDDeviceNAttributes attribs =newPDDeviceNAttributes();
        cs2.setAttributes(attribs);

        // Add the color space to the color space map.
        colorspaces.put("Cs2", cs2);

        // Add the color space map to the page resources.
        PDResources res =newPDResources();
        pdpage.setResources(res);
        pdpage.getResources().setColorSpaces(colorspaces);

        // Add the page to the document.
        doc.addPage( pdpage );


        // Specify the page (pdpage) contents of the PDF file (doc).
        PDPageContentStream contentStream =newPDPageContentStream(doc, 
pdpage,false,false);

        /*
         Add a rectangle to the page.
         */
        // Set color space
        contentStream.setNonStrokingColorSpace(cs2);
        contentStream.setNonStrokingColor(new float[]{1,1,1,1,1,1});
        contentStream.fillRect(10,10,20,20);

        /*
            Add some text (Hello World) to the page.
         */
        contentStream.beginText();
         floatinitialpointX =0;
         floatinitialpointY =0;

         contentStream.moveTextPositionByAmount(10,10);

        contentStream.setNonStrokingColorSpace(cs1);
        contentStream.setStrokingColor(0.9);
        contentStream.drawString("Hello World!");
        contentStream.endText();

        // Close the stream.
        contentStream.close();

        // Finally save the document.
        doc.save( filename );

        // Close the document.
        if( doc !=null) {
            try{
                doc.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}



On 26-3-2015 17:25, Tilman Hausherr wrote:
Am 25.03.2015 um 22:52 schrieb Floris:
Hey there,

I am struggeling getting the PDDeviceN color space to work. I am trying to create a simple page with one rectangle having a color defined against a DeviceN space. I tried the following:

PDDeviceN cs =newPDDeviceN();
cs.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);
cs.setColorantNames(Arrays.asList("Cyan","Magenta","Yellow","Black","Orange","Green"));

PDFunction func;
COSArray domains =newCOSArray();
COSArray ranges =newCOSArray();

domains.setFloatArray(new float[]{0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f}); ranges.setFloatArray(new float[]{0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f});

COSDictionary dict=newCOSDictionary();
dict.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
dict.setItem(COSName.DOMAIN, domains);
dict.setItem(COSName.RANGE, ranges);

try{
    func = PDFunction.create(tintdata);
        cs.setTintTransform(func);
}catch(IOException e) {
        e.printStackTrace();
}

This results in a NullPointerError in the first row of the try-part. The type4-function should just pop the first two values (to get from CMYKOG to CMYK). Could someone give me a jump start on how to define a PDDeviceN space?

Have a nice day,
Floris



Hello,

- tintdata is not defined
- the postscript code of the type4 function is missing

Here's an excerpt of a PDF file with a type 4 function:


48 0 obj
<<
/FunctionType 4
/Domain [0 1]
/Range [0 1 0 1 0 1 0 1]
/Length 62
>>
stream
{dup 0.37 mul exch dup 0 mul exch dup 0.34 mul exch 0.34 mul }
endstream
endobj

i.e. your function must have a stream which contains the postscript code. Thus call PDFunction.create() with a COStream. This object should have the dictionary that you already have, and as stream the "code" of the function.


COSDictionary dict=newCOSDictionary();
dict.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
dict.setItem(COSName.DOMAIN, domains);
dict.setItem(COSName.RANGE, ranges);

String functionText = "{ push pop whatever }";

COSStream functionStream = new COSStream(dict, new RandomAccessBuffer());
OutputStream out = functionStream.createUnfilteredStream();
out.write(functionText.getBytes("US-ASCII"));
out.close();

func = PDFunction.create(functionStream);


I didn't test this code. If it doesn't work, please post some complete code that creates a PDF (that fails) and I'll try to help you more (However I can't help you with the postscript code).

Tilman

PS: always mention the version when you ask a question (in your case probably 1.8.*)


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


Reply via email to