Skip to main content
Skip table of contents

Spot colors


addspotcolorimage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.DeviceCmyk;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
import com.itextpdf.kernel.pdf.function.PdfType2Function;
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
import com.itextpdf.kernel.pdf.xobject.PdfXObject;

import java.io.File;

public class AddSpotColorImage {
    public static final String DEST = "./target/sandbox/stamper/add_spot_color_image.pdf";
    public static final String SRC = "./src/main/resources/pdfs/image.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddSpotColorImage().manipulatePdf(DEST);
    }

    public PdfSpecialCs getSeparationColorspace(PdfWriter writer, DeviceCmyk cmyk) {
        PdfDictionary pdfDictionary = new PdfDictionary();
        pdfDictionary.put(PdfName.FunctionType, new PdfNumber(2));
        pdfDictionary.put(PdfName.Domain, new PdfArray(new float[] {0, 1}));
        pdfDictionary.put(PdfName.C0, new PdfArray(new float[] {0, 0, 0, 0}));
        pdfDictionary.put(PdfName.C1, new PdfArray(cmyk.getColorValue()));
        pdfDictionary.put(PdfName.N, new PdfNumber(1));

        PdfType2Function pdfFunction = new PdfType2Function(pdfDictionary);

        return new PdfSpecialCs.Separation("mySpotColor", cmyk.getColorSpace(), pdfFunction);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        // Suppose that this is our image data
        byte[] circleData = {(byte) 0x3c, (byte) 0x7e, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                (byte) 0xff, (byte) 0x7e, (byte) 0x3c};

        PdfSpecialCs colorspace = getSeparationColorspace(pdfDoc.getWriter(),
                new DeviceCmyk(0.8f, 0.3f, 0.3f, 0.1f));

        // Specifying a single component colorspace in the image
        ImageData image = ImageDataFactory.create(8, 8, 1, 1,
                circleData, new int[] {0, 0});
        PdfImageXObject imageXObject = new PdfImageXObject(image);
        imageXObject.put(PdfName.ColorSpace, colorspace.getPdfObject());
        imageXObject.makeIndirect(pdfDoc);

        // Now we add the image to the existing PDF document
        PdfPage pdfPage = pdfDoc.getFirstPage();
        pdfPage.setIgnorePageRotationForContent(true);
        PdfCanvas canvas = new PdfCanvas(pdfPage);
        Rectangle rect = PdfXObject.calculateProportionallyFitRectangleWithWidth(imageXObject, 100, 200, 100);
        canvas.addXObjectFittedIntoRectangle(imageXObject, rect);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Colorspace;
using iText.Kernel.Pdf.Function;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddSpotColorImage 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_spot_color_image.pdf";
        public static readonly String SRC = "../../../resources/pdfs/image.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddSpotColorImage().ManipulatePdf(DEST);
        }

        public PdfSpecialCs GetSeparationColorspace(PdfWriter writer, DeviceCmyk cmyk) 
        {
            PdfDictionary pdfDictionary = new PdfDictionary();
            pdfDictionary.Put(PdfName.FunctionType, new PdfNumber(2));
            pdfDictionary.Put(PdfName.Domain, new PdfArray(new float[] { 0, 1 }));
            pdfDictionary.Put(PdfName.C0, new PdfArray(new float[] { 0, 0, 0, 0 }));
            pdfDictionary.Put(PdfName.C1, new PdfArray(cmyk.GetColorValue()));
            pdfDictionary.Put(PdfName.N, new PdfNumber(1));

            IPdfFunction pdfFunction = new PdfType2Function(pdfDictionary);

            return new PdfSpecialCs.Separation("mySpotColor", cmyk.GetColorSpace(), pdfFunction);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            // Suppose that this is our image data
            byte[] circleData = new byte[] 
                    { (byte)0x3c, (byte)0x7e, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7e, (byte)0x3c };
            
            PdfSpecialCs colorspace = GetSeparationColorspace(pdfDoc.GetWriter(), 
                    new DeviceCmyk(0.8f, 0.3f, 0.3f, 0.1f));
            
            // Specifying a single component colorspace in the image
            ImageData image = ImageDataFactory.Create(8, 8, 1, 1, circleData, 
                    new int[] { 0, 0 });
            PdfImageXObject imageXObject = new PdfImageXObject(image);
            imageXObject.Put(PdfName.ColorSpace, colorspace.GetPdfObject());
            imageXObject.MakeIndirect(pdfDoc);
            
            // Now we add the image to the existing PDF document
            PdfPage pdfPage = pdfDoc.GetFirstPage();
            pdfPage.SetIgnorePageRotationForContent(true);
            PdfCanvas canvas = new PdfCanvas(pdfPage);
            Rectangle rect = PdfXObject.CalculateProportionallyFitRectangleWithWidth(imageXObject, 100, 200, 100);
            canvas.AddXObjectFittedIntoRectangle(imageXObject, rect);
            
            pdfDoc.Close();
        }
    }
}


addspotcolorshape

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.colors.DeviceCmyk;
import com.itextpdf.kernel.colors.Separation;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfSpecialCs;
import com.itextpdf.kernel.pdf.function.PdfType2Function;

import java.io.File;

public class AddSpotColorShape {
    public static final String DEST = "./target/sandbox/stamper/add_spot_color_shape.pdf";
    public static final String SRC = "./src/main/resources/pdfs/image.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddSpotColorShape().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfPage pdfPage = pdfDoc.getFirstPage();

        pdfPage.setIgnorePageRotationForContent(true);
        PdfCanvas canvas = new PdfCanvas(pdfPage);
        canvas.arc(0, 0, 842, 595, 0, 360);
        canvas.arc(25, 25, 817, 570, 0, 360);
        canvas.arc(50, 50, 792, 545, 0, 360);
        canvas.arc(75, 75, 767, 520, 0, 360);
        canvas.eoClip();
        canvas.endPath();
        canvas.setFillColor(new Separation(createCmykColorSpace(0.8f, 0.3f, 0.3f, 0.1f), 0.4f));
        canvas.rectangle(0, 0, 842, 595);
        canvas.fill();

        pdfDoc.close();
    }

    private PdfSpecialCs.Separation createCmykColorSpace(float c, float m, float y, float k) {
        double[] c0 = new double[] {0, 0, 0, 0};
        double[] c1 = new double[] {c, m, y, k};
        PdfType2Function pdfFunction = new PdfType2Function(new double[] {0, 1}, null, c0, c1, 1);
        PdfSpecialCs.Separation cs = new PdfSpecialCs.Separation("iTextSpotColorCMYK",
                new DeviceCmyk(c, m, y, k).getColorSpace(), pdfFunction);

        return cs;
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Colorspace;
using iText.Kernel.Pdf.Function;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddSpotColorShape 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_spot_color_shape.pdf";
        public static readonly String SRC = "../../../resources/pdfs/image.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddSpotColorShape().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfPage pdfPage = pdfDoc.GetFirstPage();
            
            pdfPage.SetIgnorePageRotationForContent(true);
            PdfCanvas canvas = new PdfCanvas(pdfPage);
            canvas.Arc(0, 0, 842, 595, 0, 360);
            canvas.Arc(25, 25, 817, 570, 0, 360);
            canvas.Arc(50, 50, 792, 545, 0, 360);
            canvas.Arc(75, 75, 767, 520, 0, 360);
            canvas.EoClip();
            canvas.EndPath();
            canvas.SetFillColor(new Separation(CreateCmykColorSpace(0.8f, 0.3f, 0.3f, 0.1f), 0.4f));
            canvas.Rectangle(0, 0, 842, 595);
            canvas.Fill();
            
            pdfDoc.Close();
        }

        private PdfSpecialCs.Separation CreateCmykColorSpace(float c, float m, float y, float k) 
        {
            double[] c0 = new double[] { 0, 0, 0, 0 };
            double[] c1 = new double[] { c, m, y, k };
            IPdfFunction pdfFunction = new PdfType2Function(new double[] { 0, 1 }, 
                    null, c0, c1,1);
            PdfSpecialCs.Separation cs = new PdfSpecialCs.Separation("iTextSpotColorCMYK", 
                    new DeviceCmyk(c, m, y, k).GetColorSpace(), pdfFunction);
            
            return cs;
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.