PDFlib Cookbook

cookbook

color/spot_color_shading

Define and use several spot colors as shading.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Shadings with N=2/3/4 Pantone spot colors as stop colors
 * PDFlib automatically creates a suitable DeviceN color space in which
 * a shading between multiple spot colors can be expressed.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1.1
 * 
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.color;

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

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

        pdflib p = null;
        int font, sh, shp;
        int y = 600, x = 30, width = 700, height = 50, fontsize = 14,
                leading = 21;
        int exitcode = 0;

        try {
            p = new pdflib();

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

            /* This means that errors in load_font() etc. throw an exception */
            p.set_option("errorpolicy=exception");

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

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

            /* Load the font */
            font = p.load_font("NotoSerif-Bold", "unicode", "");

            /* Start the page */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            p.setfont(font, fontsize);

            /*
             * *******************************************************
             * Define a shading color based on two Pantone spot colors
             */
            p.fit_textline(
                    "Shading with spot colors PANTONE 123 U, PANTONE 289 U:", x,
                    y -= 2 * leading, "fillcolor=black");

            y -= height + fontsize;

            /* Create axial shading based on two Pantone spot colors */
            sh = p.shading("axial", x, y, x + width, y, 0, 0, 0, 0,
                    "startcolor={spotname {PANTONE 123 U} 1} "
                            + "  endcolor={spotname {PANTONE 289 U} 1}");

            shp = p.shading_pattern(sh, "");
            p.set_graphics_option("fillcolor={pattern " + shp + "}");

            /* Fill a rectangle with the shading */
            p.rect(x, y, width, height);
            p.fill();

            /*
             * *******************************************************
             * Define a shading based on three Pantone spot colors
             */
            p.fit_textline(
                    "Shading with spot colors PANTONE 123 U, PANTONE 289 U, PANTONE Rubine Red U:",
                    x, y -= 2 * leading, "fillcolor=black");

            y -= height + fontsize;

            /*
             * Create axial shading based on three Pantone spot colors, using
             * 100% of each component at 0%, 50%, 100%, respectively.
             */
            sh = p.shading("axial", x, y, x + width, y, 0, 0, 0, 0,
                    "stopcolors={  0%  {spotname {PANTONE 123 U} 1} "
                            + " 50%  {spotname {PANTONE 289 U} 1} "
                            + "100%  {spotname {PANTONE Rubine Red U} 1} }");

            shp = p.shading_pattern(sh, "");
            p.set_graphics_option("fillcolor={pattern " + shp + "}");

            /* Fill a rectangle with the shading */
            p.rect(x, y, width, height);
            p.fill();

            /*
             * *******************************************************
             * Define a shading based on four Pantone spot colors
             */
            p.fit_textline(
                    "Shading with spot colors PANTONE 123 U, PANTONE 289 U, PANTONE Rubine Red U, PANTONE 1215 U:",
                    x, y -= 2 * leading, "fillcolor=black");
            y -= height + fontsize;

            /*
             * Create axial shading based on DeviceN color, using 100% of each
             * component distributed over the range 0%-100%
             */
            sh = p.shading("axial", x, y, x + width, y, 0, 0, 0, 0,
                    "stopcolors={  0%  {spotname {PANTONE 123 U} 1} "
                            + " 33%  {spotname {PANTONE 289 U} 1} "
                            + " 66%  {spotname {PANTONE Rubine Red U} 1} "
                            + "100%  {spotname {PANTONE 1215 U} 1} }");

            shp = p.shading_pattern(sh, "");
            p.set_graphics_option("fillcolor={pattern " + shp + "}");

            /* Fill a rectangle with the shading */
            p.rect(x, y, width, height);
            p.fill();

            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);
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}