Spot colors

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

AddSpotColorImage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.stamper;
  9.  
  10. import com.itextpdf.io.image.ImageData;
  11. import com.itextpdf.io.image.ImageDataFactory;
  12. import com.itextpdf.kernel.color.DeviceCmyk;
  13. import com.itextpdf.kernel.pdf.*;
  14. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  15. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23.  
  24. @Category(SampleTest.class)
  25. public class AddSpotColorImage extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_spot_color_image.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/image.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddSpotColorImage().manipulatePdf(DEST);
  33.     }
  34.  
  35.     public PdfArray getSeparationColorspace(PdfWriter writer, DeviceCmyk cmyk) {
  36.         PdfArray array = new PdfArray(PdfName.Separation);
  37.         array.add(new PdfName("mySpotColor"));
  38.         array.add(PdfName.DeviceCMYK);
  39.         PdfDictionary func = new PdfDictionary();
  40.         func.put(PdfName.FunctionType, new PdfNumber(2));
  41.         func.put(PdfName.Domain, new PdfArray(new float[]{0, 1}));
  42.         func.put(PdfName.C0, new PdfArray(new float[]{0, 0, 0, 0}));
  43.         // magic numbers are from itext5
  44.         func.put(PdfName.C1, new PdfArray(cmyk.getColorValue()));
  45.         func.put(PdfName.N, new PdfNumber(1));
  46.         array.add(func);
  47.         return array;
  48.     }
  49.  
  50.     @Override
  51.     protected void manipulatePdf(String dest) throws Exception {
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  53.  
  54.         // suppose that this is our image data
  55.         byte circleData[] = {(byte) 0x3c, (byte) 0x7e, (byte) 0xff,
  56.                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7e,
  57.                 (byte) 0x3c};
  58.  
  59.         PdfArray colorspace = getSeparationColorspace(pdfDoc.getWriter(), new DeviceCmyk(0.8f, 0.3f, 0.3f, 0.1f));
  60.         ImageData image = ImageDataFactory.create(8, 8, 1, 1, circleData, new int[]{0, 0});
  61.         PdfImageXObject imageXObject = new PdfImageXObject(image);
  62.         imageXObject.put(PdfName.ColorSpace, colorspace);
  63.         imageXObject.makeIndirect(pdfDoc);
  64.         // Now we add the image to the existing PDF document
  65.         PdfPage pdfPage = pdfDoc.getFirstPage();
  66.         pdfPage.setIgnorePageRotationForContent(true);
  67.         PdfCanvas canvas = new PdfCanvas(pdfPage);
  68.         canvas.addXObject(imageXObject, 100, 200, 100);
  69.  
  70.         pdfDoc.close();
  71.     }
  72. }
AddSpotColorShape.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.stamper;
  9.  
  10. import com.itextpdf.kernel.color.DeviceCmyk;
  11. import com.itextpdf.kernel.color.Separation;
  12. import com.itextpdf.kernel.pdf.*;
  13. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  14. import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
  15. import com.itextpdf.kernel.pdf.function.PdfFunction;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23.  
  24. @Category(SampleTest.class)
  25. public class AddSpotColorShape extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_spot_color_shape.pdf";
  27.     public static final String SRC = "./src/test/resources/pdfs/image.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddSpotColorShape().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  38.         PdfPage pdfPage = pdfDoc.getFirstPage();
  39.         pdfPage.setIgnorePageRotationForContent(true);
  40.         PdfCanvas canvas = new PdfCanvas(pdfPage);
  41.         canvas.arc(0, 0, 842, 595, 0, 360);
  42.         canvas.arc(25, 25, 817, 570, 0, 360);
  43.         canvas.arc(50, 50, 792, 545, 0, 360);
  44.         canvas.arc(75, 75, 767, 520, 0, 360);
  45.         canvas.eoClip();
  46.         canvas.newPath();
  47.         canvas.setFillColor(new Separation(createCmykColorSpace(0.8f, 0.3f, 0.3f, 0.1f), 0.4f));
  48.         canvas.rectangle(0, 0, 842, 595);
  49.         canvas.fill();
  50.  
  51.         pdfDoc.close();
  52.     }
  53.  
  54.     private PdfSpecialCs.Separation createCmykColorSpace(float c, float m, float y, float k) {
  55.         float[] c0 = new float[]{0, 0, 0, 0};
  56.         float[] c1 = new float[]{c, m, y, k};
  57.         PdfFunction f = new PdfFunction.Type2(new PdfArray(new float[]{0, 1}), null, new PdfArray(c0), new PdfArray(c1), new PdfNumber(1));
  58.         PdfSpecialCs.Separation cs = new PdfSpecialCs.Separation("iTextSpotColorCMYK", new DeviceCmyk(c, m, y, k).getColorSpace(), f);
  59.  
  60.         return cs;
  61.     }
  62. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now