pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Floris <fne...@gmail.com>
Subject Separation Space
Date Wed, 01 Apr 2015 18:02:03 GMT
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();
             }
         }
     }
}



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message