Spot colors

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

8th November 2015
admin-marketing

Switch code for this example

AddSpotColorImage.java
  1. package sandbox.stamper;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Image;
  5. import com.itextpdf.text.pdf.CMYKColor;
  6. import com.itextpdf.text.pdf.PdfArray;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfDictionary;
  9. import com.itextpdf.text.pdf.PdfImage;
  10. import com.itextpdf.text.pdf.PdfIndirectObject;
  11. import com.itextpdf.text.pdf.PdfName;
  12. import com.itextpdf.text.pdf.PdfNumber;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class AddSpotColorImage {
  25.     public static final String SRC = "resources/pdfs/image.pdf";
  26.     public static final String DEST = "results/stamper/spot_color_image.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddSpotColorImage().manipulatePdf(SRC, DEST);
  32.     }
  33.  
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         // suppose that this is our image data
  38.         byte circledata[] = { (byte) 0x3c, (byte) 0x7e, (byte) 0xff,
  39.                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x7e,
  40.                 (byte) 0x3c };
  41.         // we have an image of 8 by 8 with 1 component and 1 bit per component
  42.         // the 1 value is colored, the 0 value is transparent
  43.         Image image = Image.getInstance(8, 8, 1, 1, circledata, new int[]{0, 0});
  44.         // By default, the colorspace of such an image is DeviceGray
  45.         // In other words: the image is in black and white
  46.         // We want to use a separation colorspace:
  47.         PdfArray colorspace = getSeparationColorspace(stamper.getWriter(), new CMYKColor(0.8f, 0.3f, 0.3f, 0.1f));
  48.         // We get the image as a stream object
  49.         PdfImage stream = new PdfImage(image, "", null);
  50.         // and we change its color space:
  51.         stream.put(PdfName.COLORSPACE, colorspace);
  52.         // We add the stream to the writer
  53.         PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
  54.         // We adapt the original image
  55.         image.setDirectReference(ref.getIndirectReference());
  56.         image.scaleAbsolute(100, 100);
  57.         image.setAbsolutePosition(100, 200);
  58.         // Now we add the image to the existing PDF document
  59.         PdfContentByte canvas = stamper.getOverContent(1);
  60.         canvas.addImage(image);
  61.         stamper.close();
  62.         reader.close();
  63.     }
  64.  
  65.     public PdfArray getSeparationColorspace(PdfWriter writer, CMYKColor cmyk) {
  66.         PdfArray array = new PdfArray(PdfName.SEPARATION);
  67.         array.add(new PdfName("mySpotColor"));
  68.         array.add(PdfName.DEVICECMYK);
  69.         PdfDictionary func = new PdfDictionary();
  70.         func.put(PdfName.FUNCTIONTYPE, new PdfNumber(2));
  71.         func.put(PdfName.DOMAIN, new PdfArray(new float[]{0, 1}));
  72.         func.put(PdfName.C0, new PdfArray(new float[]{0, 0, 0, 0}));
  73.         func.put(PdfName.C1, new PdfArray(new float[]{cmyk.getCyan(), cmyk.getMagenta(), cmyk.getYellow(), cmyk.getBlack()}));
  74.         func.put(PdfName.N, new PdfNumber(1));
  75.         array.add(func);
  76.         return array;
  77.     }
  78. }
AddSpotColorShape.java
  1. package sandbox.stamper;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.pdf.CMYKColor;
  5. import com.itextpdf.text.pdf.PdfContentByte;
  6. import com.itextpdf.text.pdf.PdfReader;
  7. import com.itextpdf.text.pdf.PdfSpotColor;
  8. import com.itextpdf.text.pdf.PdfStamper;
  9. import com.itextpdf.text.pdf.SpotColor;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14.  
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class AddSpotColorShape {
  19.     public static final String SRC = "resources/pdfs/image.pdf";
  20.     public static final String DEST = "results/stamper/spot_color.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new AddSpotColorShape().manipulatePdf(SRC, DEST);
  26.     }
  27.  
  28.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  29.         PdfReader reader = new PdfReader(src);
  30.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  31.         PdfContentByte canvas = stamper.getOverContent(1);
  32.         canvas.arc(0, 0, 842, 595, 0, 360);
  33.         canvas.arc(25, 25, 817, 570, 0, 360);
  34.         canvas.arc(50, 50, 792, 545, 0, 360);
  35.         canvas.arc(75, 75, 767, 520, 0, 360);
  36.         canvas.eoClip();
  37.         canvas.newPath();
  38.         PdfSpotColor psc = new PdfSpotColor("mySpotColor", new CMYKColor(0.8f, 0.3f, 0.3f, 0.1f));
  39.         canvas.setColorFill(new SpotColor(psc, 0.4f));
  40.         canvas.rectangle(0, 0, 842, 595);
  41.         canvas.fill();
  42.         stamper.close();
  43.         reader.close();
  44.     }
  45. }
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