You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Floris <fn...@gmail.com> on 2015/04/01 20:02:03 UTC

Separation Space

Hallo there,

I am trying to define a Separation color space but get the following 
error message if I try to run the program posted beneath. The error 
seems to be quite local; coming only from the setAlternateColorSpace. 
What is going on here? I am using 
org.apache.pdfbox:pdfbox:app-1.8.10-20150327.210556-4 (in this version 
DeviceN is working) and java version 1.8.0_40-ea.

Have a nice day,
Floris

Error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 
2, Size: 2
     at java.util.ArrayList.rangeCheck(ArrayList.java:653)
     at java.util.ArrayList.set(ArrayList.java:444)
     at org.apache.pdfbox.cos.COSArray.set(COSArray.java:148)
     at 
org.apache.pdfbox.pdmodel.graphics.color.PDSeparation.setAlternateColorSpace(PDSeparation.java:182)
     at 
com.here.devicentest.DeviceNTest.WriteSeprationPDF(DeviceNTest.java:463)
     at com.here.devicentest.DeviceNTest.main(DeviceNTest.java:43)

Program:

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.ArrayList;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Map;

public classDeviceNTest {

     public static voidmain(String[] args) {

         try{
		WriteSeprationPDF("SeparationColorTest.pdf");             
             	System.out.println("Done Writing!");

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

     }

     public static voidWriteSeprationPDF(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);

         /*
          Construct Separation Space
           */
         PDSeparation cs3 =newPDSeparation();
         cs3.setColorantName("MYCOLORNAME");
         cs3.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);

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

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

         // - Dictionary
         COSDictionary functiondata3 =newCOSDictionary();

         functiondata3.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
         functiondata3.setItem(COSName.DOMAIN, domains3);
         functiondata3.setItem(COSName.RANGE, ranges3);
         functiondata3.setItem(COSName.TYPE, COSName.FUNCTION);

         // - Stream
         String functionText3 ="{dup dup dup}";

         COSStream functionStream3 =newCOSStream(functiondata3,newRandomAccessBuffer());
         OutputStream out3 = functionStream3.createUnfilteredStream();
         out3.write(functionText3.getBytes("US-ASCII"));
         out3.close();

         // Construct the tint transform function.
         PDFunctionType4 tinttransformfunction3 = (PDFunctionType4) PDFunctionType4.create(functionStream3);

         // Add it to the color space.
         cs3.setTintTransform(tinttransformfunction3);

         colorspaces.put("Cs3", cs3);

         // 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(cs3);
         contentStream.setNonStrokingColor(1);
         contentStream.fillRect(10,10,20,20);

         /*
             Add some text (Hello World) to the page.
          */
         contentStream.beginText();
         contentStream.moveTextPositionByAmount(10,10);

         contentStream.setNonStrokingColorSpace(cs1);
         contentStream.setFont(currentfont,16);
         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();
             }
         }
     }
}



Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

I have done a first fix. Wait some time until it will be available at 
the bottom of
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/1.8.10-SNAPSHOT/
and tell me if it works, or what "bad things" happen.

Btw I still like to have a workable version of your test code. (I'm 
lazy, last time I added all the spaces myself)

Tilman

Am 01.04.2015 um 20:22 schrieb Tilman Hausherr:
> Hi,
>
> There's obviously a bug in the initialization without parameters. The 
> array should be filled more / or the set method should fill what is 
> needed. I'll investigate and fix this and come back to you.
>
> Btw, your test code, could you please post it again and so that the 
> space between "new" and the rest is there? It is missing. Maybe it is 
> a tab?
>
> Tilman
>
>
> Am 01.04.2015 um 20:02 schrieb Floris:
>> Hallo there,
>>
>> I am trying to define a Separation color space but get the following 
>> error message if I try to run the program posted beneath. The error 
>> seems to be quite local; coming only from the setAlternateColorSpace. 
>> What is going on here? I am using 
>> org.apache.pdfbox:pdfbox:app-1.8.10-20150327.210556-4 (in this 
>> version DeviceN is working) and java version 1.8.0_40-ea.
>>
>> Have a nice day,
>> Floris
>>
>> Error:
>> Exception in thread "main" java.lang.IndexOutOfBoundsException: 
>> Index: 2, Size: 2
>>     at java.util.ArrayList.rangeCheck(ArrayList.java:653)
>>     at java.util.ArrayList.set(ArrayList.java:444)
>>     at org.apache.pdfbox.cos.COSArray.set(COSArray.java:148)
>>     at 
>> org.apache.pdfbox.pdmodel.graphics.color.PDSeparation.setAlternateColorSpace(PDSeparation.java:182)
>>     at 
>> com.here.devicentest.DeviceNTest.WriteSeprationPDF(DeviceNTest.java:463)
>>     at com.here.devicentest.DeviceNTest.main(DeviceNTest.java:43)
>>
>> Program:
>>
>> 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.ArrayList;
>> importjava.util.Arrays;
>> importjava.util.HashMap;
>> importjava.util.Map;
>>
>> public classDeviceNTest {
>>
>>     public static voidmain(String[] args) {
>>
>>         try{
>> WriteSeprationPDF("SeparationColorTest.pdf"); 
>> System.out.println("Done Writing!");
>>
>>         }catch(IOException e) {
>>             e.printStackTrace();
>>         }catch(COSVisitorException e) {
>>             e.printStackTrace();
>>         }
>>
>>     }
>>
>>     public static voidWriteSeprationPDF(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);
>>
>>         /*
>>          Construct Separation Space
>>           */
>>         PDSeparation cs3 =newPDSeparation();
>>         cs3.setColorantName("MYCOLORNAME");
>>         cs3.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);
>>
>>         // - Tint Transform
>>         /*
>>             Function Type 4
>>             - Domain
>>             - Range
>>             - Length
>>             - Type
>>             - Stream (containing the PS code).
>>          */
>>         // - Domain
>>         COSArray domains3 =newCOSArray();
>>         domains3.setFloatArray(new float[]{0,1});
>>
>>         // - Range
>>         COSArray ranges3 =newCOSArray();
>>         ranges3.setFloatArray(new 
>> float[]{0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f});
>>
>>         // - Dictionary
>>         COSDictionary functiondata3 =newCOSDictionary();
>>
>>         functiondata3.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
>>         functiondata3.setItem(COSName.DOMAIN, domains3);
>>         functiondata3.setItem(COSName.RANGE, ranges3);
>>         functiondata3.setItem(COSName.TYPE, COSName.FUNCTION);
>>
>>         // - Stream
>>         String functionText3 ="{dup dup dup}";
>>
>>         COSStream functionStream3 
>> =newCOSStream(functiondata3,newRandomAccessBuffer());
>>         OutputStream out3 = functionStream3.createUnfilteredStream();
>>         out3.write(functionText3.getBytes("US-ASCII"));
>>         out3.close();
>>
>>         // Construct the tint transform function.
>>         PDFunctionType4 tinttransformfunction3 = (PDFunctionType4) 
>> PDFunctionType4.create(functionStream3);
>>
>>         // Add it to the color space.
>>         cs3.setTintTransform(tinttransformfunction3);
>>
>>         colorspaces.put("Cs3", cs3);
>>
>>         // 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(cs3);
>>         contentStream.setNonStrokingColor(1);
>>         contentStream.fillRect(10,10,20,20);
>>
>>         /*
>>             Add some text (Hello World) to the page.
>>          */
>>         contentStream.beginText();
>>         contentStream.moveTextPositionByAmount(10,10);
>>
>>         contentStream.setNonStrokingColorSpace(cs1);
>>         contentStream.setFont(currentfont,16);
>>         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();
>>             }
>>         }
>>     }
>> }
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 02.04.2015 um 08:59 schrieb Tilman Hausherr:
>
> I'll think about whether to make a check for the current colorspace in 
> the other setNonStrokingColor methods if needed.

I am mistaken. The "g" operator also sets the colorspace to DeviceGray. 
 From the spec:

G: Set the stroking colour space to DeviceGray (or the DefaultGray 
colour space; see 8.6.5.6, "Default Colour Spaces") and set the gray 
level to use for stroking operations. gray shall be a number between 0.0 
(black) and 1.0 (white).

g: Same as G but used for nonstroking operations

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
Hmm, you're right there, but the javadoc says that the command is for 
DeviceGray.

I looked in the source code, you have to use the command you used for 
DeviceN, this one uses the correct command.

     /**
      * Set the color components of current non stroking colorspace.
      *
      * @param components The components to set for the current color.
      * @throws IOException If there is an error while writing to the 
stream.
      */
     public void setNonStrokingColor(float[] components) throws IOException
     {
         for (int i = 0; i < components.length; i++)
         {
             appendRawCommands(components[i]);
             appendRawCommands(SPACE);
         }
         if (currentNonStrokingColorSpace instanceof PDSeparation || 
currentNonStrokingColorSpace instanceof PDPattern
                 || currentNonStrokingColorSpace instanceof PDDeviceN
                 || currentNonStrokingColorSpace instanceof PDICCBased)
         {
             appendRawCommands(SET_NON_STROKING_COLOR_COMPLEX);
         }
         else
         {
             appendRawCommands(SET_NON_STROKING_COLOR_SIMPLE);
         }
     }


I'll think about whether to make a check for the current colorspace in 
the other setNonStrokingColor methods if needed.


Tilman

Am 02.04.2015 um 02:08 schrieb Floris:
> I tried version pdfbox-app-1.8.10-20150401.225545-14 and the 
> separation color space is created properly. The color setting operator 
> is wrong though. I get:
>
> stream
> /Cs3 cs
> 1 g                                <--------------------- This should 
> be a 'scn' according to the PDF specifications. g is used with 
> DeviceGray only.
> 10 10 20 20 re
> f
> BT
> 10 10 Td
> /DeviceGray cs
> /F0 10 Tf
> 0.5 G
> (Hello World!) Tj
> ET
>
> endstream
>
> It also seems that in the separation channel 255 bit is mapped to 1 
> while in the DeviceN case the recipe is specified directly (in the 
> range of [0,1]): to obtain the above code I used 
> 'contentStream.setNonStrokingColor(255);'. Is this intentional?
>
>
> On 1-4-2015 23:12, Tilman Hausherr wrote:
>> I just fixed a second bug, so you'll have to wait again.
>> https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.10-SNAPSHOT/ 
>>
>>
>> Your version should be one higher than 11.
>>
>> I was able to create a PDF, it has a black rectangle and "llo" in black.
>>
>> Tilman
>>
>> Am 01.04.2015 um 21:39 schrieb Floris Necas:
>>> Here is the code again. I converted everything to spaces. Let me know
>>> whether it came through properly this time.
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Floris <fn...@gmail.com>.
I tried version pdfbox-app-1.8.10-20150401.225545-14 and the separation 
color space is created properly. The color setting operator is wrong 
though. I get:

stream
/Cs3 cs
1 g                                <--------------------- This should be 
a 'scn' according to the PDF specifications. g is used with DeviceGray only.
10 10 20 20 re
f
BT
10 10 Td
/DeviceGray cs
/F0 10 Tf
0.5 G
(Hello World!) Tj
ET

endstream

It also seems that in the separation channel 255 bit is mapped to 1 
while in the DeviceN case the recipe is specified directly (in the range 
of [0,1]): to obtain the above code I used 
'contentStream.setNonStrokingColor(255);'. Is this intentional?


On 1-4-2015 23:12, Tilman Hausherr wrote:
> I just fixed a second bug, so you'll have to wait again.
> https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.10-SNAPSHOT/ 
>
>
> Your version should be one higher than 11.
>
> I was able to create a PDF, it has a black rectangle and "llo" in black.
>
> Tilman
>
> Am 01.04.2015 um 21:39 schrieb Floris Necas:
>> Here is the code again. I converted everything to spaces. Let me know
>> whether it came through properly this time.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
I just fixed a second bug, so you'll have to wait again.
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.10-SNAPSHOT/

Your version should be one higher than 11.

I was able to create a PDF, it has a black rectangle and "llo" in black.

Tilman

Am 01.04.2015 um 21:39 schrieb Floris Necas:
> Here is the code again. I converted everything to spaces. Let me know
> whether it came through properly this time.
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 01.04.2015 um 21:39 schrieb Floris Necas:
> I converted everything to spaces. Let me know
> whether it came through properly this time.
Yes, thanks!

Tilman

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Floris Necas <fn...@gmail.com>.
Here is the code again. I converted everything to spaces. Let me know
whether it came through properly this time.

package com.here.devicentest;

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

import java.io.IOException;
import java.io.OutputStream;
import java.lang.String;
import java.lang.System;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DeviceNTest {

    public static void main(String[] args) {

        try {
        WriteSeprationPDF("SeparationColorTest.pdf");
                System.out.println("Done Writing!");

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

    }

    public static void WriteSeprationPDF(String filename) throws
IOException, COSVisitorException {

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

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

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

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

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

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

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

        /*
         Construct Separation Space
          */
        PDSeparation cs3 = new PDSeparation();
        cs3.setColorantName("MYCOLORNAME");
        cs3.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);

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

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

        // - Dictionary
        COSDictionary functiondata3 = new COSDictionary();

        functiondata3.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
        functiondata3.setItem(COSName.DOMAIN, domains3);
        functiondata3.setItem(COSName.RANGE, ranges3);
        functiondata3.setItem(COSName.TYPE, COSName.FUNCTION);

        // - Stream
        String functionText3 = "{dup dup dup}";

        COSStream functionStream3 = new COSStream(functiondata3, new
RandomAccessBuffer());
        OutputStream out3 = functionStream3.createUnfilteredStream();
        out3.write(functionText3.getBytes("US-ASCII"));
        out3.close();

        // Construct the tint transform function.
        PDFunctionType4 tinttransformfunction3 = (PDFunctionType4)
PDFunctionType4.create(functionStream3);

        // Add it to the color space.
        cs3.setTintTransform(tinttransformfunction3);

        colorspaces.put("Cs3", cs3);

        // Add the color space map to the page resources.
        PDResources res = new PDResources();
        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 = new PDPageContentStream(doc,
pdpage, false, false);

        /*
         Add a rectangle to the page.
         */
        // Set color space
        contentStream.setNonStrokingColorSpace(cs3);
        contentStream.setNonStrokingColor(1);
        contentStream.fillRect(10,10,20,20);

        /*
            Add some text (Hello World) to the page.
         */
        contentStream.beginText();
        contentStream.moveTextPositionByAmount(10,10);

        contentStream.setNonStrokingColorSpace(cs1);
        contentStream.setFont(currentfont, 16);
        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 Wed, Apr 1, 2015 at 8:22 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Hi,
>
> There's obviously a bug in the initialization without parameters. The
> array should be filled more / or the set method should fill what is needed.
> I'll investigate and fix this and come back to you.
>
> Btw, your test code, could you please post it again and so that the space
> between "new" and the rest is there? It is missing. Maybe it is a tab?
>
> Tilman
>
>
>
> Am 01.04.2015 um 20:02 schrieb Floris:
>
>> Hallo there,
>>
>> I am trying to define a Separation color space but get the following
>> error message if I try to run the program posted beneath. The error seems
>> to be quite local; coming only from the setAlternateColorSpace. What is
>> going on here? I am using org.apache.pdfbox:pdfbox:app-1.8.10-20150327.210556-4
>> (in this version DeviceN is working) and java version 1.8.0_40-ea.
>>
>> Have a nice day,
>> Floris
>>
>> Error:
>> Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:
>> 2, Size: 2
>>     at java.util.ArrayList.rangeCheck(ArrayList.java:653)
>>     at java.util.ArrayList.set(ArrayList.java:444)
>>     at org.apache.pdfbox.cos.COSArray.set(COSArray.java:148)
>>     at org.apache.pdfbox.pdmodel.graphics.color.PDSeparation.
>> setAlternateColorSpace(PDSeparation.java:182)
>>     at com.here.devicentest.DeviceNTest.WriteSeprationPDF(
>> DeviceNTest.java:463)
>>     at com.here.devicentest.DeviceNTest.main(DeviceNTest.java:43)
>>
>> Program:
>>
>> 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.ArrayList;
>> importjava.util.Arrays;
>> importjava.util.HashMap;
>> importjava.util.Map;
>>
>> public classDeviceNTest {
>>
>>     public static voidmain(String[] args) {
>>
>>         try{
>>         WriteSeprationPDF("SeparationColorTest.pdf");
>>  System.out.println("Done Writing!");
>>
>>         }catch(IOException e) {
>>             e.printStackTrace();
>>         }catch(COSVisitorException e) {
>>             e.printStackTrace();
>>         }
>>
>>     }
>>
>>     public static voidWriteSeprationPDF(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);
>>
>>         /*
>>          Construct Separation Space
>>           */
>>         PDSeparation cs3 =newPDSeparation();
>>         cs3.setColorantName("MYCOLORNAME");
>>         cs3.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);
>>
>>         // - Tint Transform
>>         /*
>>             Function Type 4
>>             - Domain
>>             - Range
>>             - Length
>>             - Type
>>             - Stream (containing the PS code).
>>          */
>>         // - Domain
>>         COSArray domains3 =newCOSArray();
>>         domains3.setFloatArray(new float[]{0,1});
>>
>>         // - Range
>>         COSArray ranges3 =newCOSArray();
>>         ranges3.setFloatArray(new float[]{0.0f,1.0f,0.0f,1.0f,0.
>> 0f,1.0f,0.0f,1.0f});
>>
>>         // - Dictionary
>>         COSDictionary functiondata3 =newCOSDictionary();
>>
>>         functiondata3.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
>>         functiondata3.setItem(COSName.DOMAIN, domains3);
>>         functiondata3.setItem(COSName.RANGE, ranges3);
>>         functiondata3.setItem(COSName.TYPE, COSName.FUNCTION);
>>
>>         // - Stream
>>         String functionText3 ="{dup dup dup}";
>>
>>         COSStream functionStream3 =newCOSStream(functiondata3,
>> newRandomAccessBuffer());
>>         OutputStream out3 = functionStream3.createUnfilteredStream();
>>         out3.write(functionText3.getBytes("US-ASCII"));
>>         out3.close();
>>
>>         // Construct the tint transform function.
>>         PDFunctionType4 tinttransformfunction3 = (PDFunctionType4)
>> PDFunctionType4.create(functionStream3);
>>
>>         // Add it to the color space.
>>         cs3.setTintTransform(tinttransformfunction3);
>>
>>         colorspaces.put("Cs3", cs3);
>>
>>         // 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(cs3);
>>         contentStream.setNonStrokingColor(1);
>>         contentStream.fillRect(10,10,20,20);
>>
>>         /*
>>             Add some text (Hello World) to the page.
>>          */
>>         contentStream.beginText();
>>         contentStream.moveTextPositionByAmount(10,10);
>>
>>         contentStream.setNonStrokingColorSpace(cs1);
>>         contentStream.setFont(currentfont,16);
>>         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();
>>             }
>>         }
>>     }
>> }
>>
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
JIRA issue is
https://issues.apache.org/jira/browse/PDFBOX-2741

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Separation Space

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

There's obviously a bug in the initialization without parameters. The 
array should be filled more / or the set method should fill what is 
needed. I'll investigate and fix this and come back to you.

Btw, your test code, could you please post it again and so that the 
space between "new" and the rest is there? It is missing. Maybe it is a tab?

Tilman


Am 01.04.2015 um 20:02 schrieb Floris:
> Hallo there,
>
> I am trying to define a Separation color space but get the following 
> error message if I try to run the program posted beneath. The error 
> seems to be quite local; coming only from the setAlternateColorSpace. 
> What is going on here? I am using 
> org.apache.pdfbox:pdfbox:app-1.8.10-20150327.210556-4 (in this version 
> DeviceN is working) and java version 1.8.0_40-ea.
>
> Have a nice day,
> Floris
>
> Error:
> Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 
> 2, Size: 2
>     at java.util.ArrayList.rangeCheck(ArrayList.java:653)
>     at java.util.ArrayList.set(ArrayList.java:444)
>     at org.apache.pdfbox.cos.COSArray.set(COSArray.java:148)
>     at 
> org.apache.pdfbox.pdmodel.graphics.color.PDSeparation.setAlternateColorSpace(PDSeparation.java:182)
>     at 
> com.here.devicentest.DeviceNTest.WriteSeprationPDF(DeviceNTest.java:463)
>     at com.here.devicentest.DeviceNTest.main(DeviceNTest.java:43)
>
> Program:
>
> 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.ArrayList;
> importjava.util.Arrays;
> importjava.util.HashMap;
> importjava.util.Map;
>
> public classDeviceNTest {
>
>     public static voidmain(String[] args) {
>
>         try{
>         WriteSeprationPDF("SeparationColorTest.pdf");                 
> System.out.println("Done Writing!");
>
>         }catch(IOException e) {
>             e.printStackTrace();
>         }catch(COSVisitorException e) {
>             e.printStackTrace();
>         }
>
>     }
>
>     public static voidWriteSeprationPDF(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);
>
>         /*
>          Construct Separation Space
>           */
>         PDSeparation cs3 =newPDSeparation();
>         cs3.setColorantName("MYCOLORNAME");
>         cs3.setAlternateColorSpace(PDDeviceCMYK.INSTANCE);
>
>         // - Tint Transform
>         /*
>             Function Type 4
>             - Domain
>             - Range
>             - Length
>             - Type
>             - Stream (containing the PS code).
>          */
>         // - Domain
>         COSArray domains3 =newCOSArray();
>         domains3.setFloatArray(new float[]{0,1});
>
>         // - Range
>         COSArray ranges3 =newCOSArray();
>         ranges3.setFloatArray(new 
> float[]{0.0f,1.0f,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f});
>
>         // - Dictionary
>         COSDictionary functiondata3 =newCOSDictionary();
>
>         functiondata3.setItem(COSName.FUNCTION_TYPE, COSInteger.get(4));
>         functiondata3.setItem(COSName.DOMAIN, domains3);
>         functiondata3.setItem(COSName.RANGE, ranges3);
>         functiondata3.setItem(COSName.TYPE, COSName.FUNCTION);
>
>         // - Stream
>         String functionText3 ="{dup dup dup}";
>
>         COSStream functionStream3 
> =newCOSStream(functiondata3,newRandomAccessBuffer());
>         OutputStream out3 = functionStream3.createUnfilteredStream();
>         out3.write(functionText3.getBytes("US-ASCII"));
>         out3.close();
>
>         // Construct the tint transform function.
>         PDFunctionType4 tinttransformfunction3 = (PDFunctionType4) 
> PDFunctionType4.create(functionStream3);
>
>         // Add it to the color space.
>         cs3.setTintTransform(tinttransformfunction3);
>
>         colorspaces.put("Cs3", cs3);
>
>         // 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(cs3);
>         contentStream.setNonStrokingColor(1);
>         contentStream.fillRect(10,10,20,20);
>
>         /*
>             Add some text (Hello World) to the page.
>          */
>         contentStream.beginText();
>         contentStream.moveTextPositionByAmount(10,10);
>
>         contentStream.setNonStrokingColorSpace(cs1);
>         contentStream.setFont(currentfont,16);
>         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();
>             }
>         }
>     }
> }
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org