PDFlib Cookbook

cookbook

color/overprint

Demonstrate overprint controls using CMYK and DeviceN colors or blend modes.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Overprint colors:
 * - demonstrate the difference between CMYK output with/without overprinting
 * - blendmode=darken as device-independent alternative to overprinting
 * - DeviceN is never affected by overprint settings
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: font file
 */
package com.pdflib.cookbook.pdflib.color;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class overprint
{
    public static void main (String argv[])
    {
    /* This is where the data files are. Adjust as necessary. */
    String searchpath = "../input";
    String outfile = "overprint.pdf";
    String title = "Overprint colors";

    pdflib p = null;
    int font, gstate, devicen;
    double x=50, y=700, width=150, height=75, fontsize=9;
    String optlist;
    int exitcode = 0;

    try {
        p = new pdflib();

        p.set_option("searchpath={" + searchpath + "}");

        /* This means we must check return values of load_font() etc. */
        p.set_option("errorpolicy=return");

        if (p.begin_document(outfile, "") == -1)
            throw new Exception("Error: " + p.get_errmsg());

        p.set_info("Creator", "PDFlib Cookbook");
        p.set_info("Title", title);

        /* The transparency group colorspace is important for Overprint
         * Preview on the screen.
         */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height transparencygroup={colorspace=devicecmyk}");

        font = p.load_font("NotoSerif-Regular", "unicode", "embedding");

        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

        p.setfont(font, 11);

        /* ********************************************************
         * Yellow and Cyan in CMYK color space without overprinting
         */
        p.set_graphics_option("fillcolor={cmyk 1 0 0 0}");
        p.rect(x, y, width, height);
        p.fill();

        optlist = "overprintfill=false";
        gstate = p.create_gstate(optlist);
        p.save();
        p.set_gstate(gstate);
        p.set_graphics_option("fillcolor={cmyk 0 0 1 0}");
        p.rect(x+width/2, y+height/2, width, height);
        p.fill();
        p.restore();
        p.fit_textline("CMYK colors without overprinting: " + optlist,
            x, y-2*fontsize, "fillcolor=black");

        
        /* ********************************************************
         * Overprint Yellow and Cyan in CMYK color space: this is
         * device-dependent; it requires "Overprint Preview" enabled in
         * Acrobat.
         */
        y -= 2*height;
        p.set_graphics_option("fillcolor={cmyk 1 0 0 0}");
        p.rect(x, y, width, height);
        p.fill();

        optlist = "overprintfill=true overprintmode=1";
        gstate = p.create_gstate(optlist);
        p.save();
        p.set_gstate(gstate);
        p.set_graphics_option("fillcolor={cmyk 0 0 1 0}");
        p.rect(x+width/2, y+height/2, width, height);
        p.fill();
        p.restore();
        p.fit_textline("Device-dependent overprinting: " + optlist,
            x, y-2*fontsize, "fillcolor=black");


        /* ********************************************************
         * Achieve device-independent overprinting effect with Yellow and
         * Cyan in CMYK color space with blend mode "Darken".
         */
        y -= 2*height;
        p.set_graphics_option("fillcolor={cmyk 1 0 0 0}");
        p.rect(x, y, width, height);
        p.fill();

        optlist = "blendmode=darken";
        gstate = p.create_gstate(optlist);
        p.save();
        p.set_gstate(gstate);
        p.set_graphics_option("fillcolor={cmyk 0 0 1 0}");
        p.rect(x+width/2, y+height/2, width, height);
        p.fill();
        p.restore();
        p.fit_textline("Device-independent overprinting effect with blend mode: " + optlist,
            x, y-2*fontsize, "fillcolor=black");


        /* ********************************************************
         * Overprint Yellow and Cyan in DeviceN color space: overprint
         * settings don't have any effect.
         */
        y -= 2*height;
        devicen = p.create_devicen(
            "names={Cyan Yellow} alternate=devicecmyk transform={{0 0 3 1 roll}} errorpolicy=exception");

        p.set_graphics_option("fillcolor={devicen " + devicen + " 1 0}");
        p.rect(x, y, width, height);
        p.fill();

        gstate = p.create_gstate("overprintfill=true overprintmode=1");
        p.save();
        p.set_gstate(gstate);
        p.set_graphics_option("fillcolor={devicen " + devicen + " 0 1}");
        p.rect(x+width/2, y+height/2, width, height);
        p.fill();
        p.restore();
        p.fit_textline("DeviceN colors (Cyan+Yellow): always unaffected by overprint settings",
            x, y-2*fontsize, "fillcolor=black");

        p.end_page_ext("");

        p.end_document("");

        } catch (PDFlibException e){
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg());
            exitcode = 1;
        } catch (Exception e) {
            System.err.println(e.toString());
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}