PDFlib Cookbook

cookbook

color/spot_color

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: spot_color.java,v 1.8 2014/01/30 14:05:23 rjs Exp $
 * Spot color:
 * Define and use several spot colors
 * 
 * Define and use a PANTONE spot color.
 * Define and use a HKS spot color.
 * Define and use a custom spot color based on alternate CMYK values.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.color;

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

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

	pdflib p = null;
	int font, spot;
	int y = 500, x = 30;
	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 + " $Revision: 1.8 $");
	    
	    /* Load the font */
	    font = p.load_font("Helvetica-Bold", "unicode", "");

	    if (font == -1)
		throw new Exception("Error: " + p.get_errmsg());
	    
	    /* Start the page */
	    p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
	    
	    
	    /* ---- Define and use a PANTONE spot color ---- */
	    
	    /* Define the spot color "PANTONE 281 U" from the builtin color
	     * library PANTONE */
	    spot = p.makespotcolor("PANTONE 281 U");
	    
	    /* Set the spot color "PANTONE 281 U" with a tint value of 1 (=100%)
	     * and output some text */
	    p.setcolor("fill", "spot", spot, 1.0, 0.0, 0.0);
	   
	    p.fit_textline("PANTONE 281 U spot color with a tint value of 100%",
		x, y -= 30, "font=" + font + " fontsize=16");
	    
	    /* Set the spot color "PANTONE 281 U" with a tint value of 0.5 (=50%)
	     * and output some text */
	    p.setcolor("fill", "spot", spot, 0.5, 0.0, 0.0);
	    
	    p.fit_textline("PANTONE 281 U spot color with a tint value of 50%",
		x, y -= 30, "font=" + font + " fontsize=16");
	    
	    
	    /* ---- Define and use a HKS spot color ---- */
	    
	    /* Define the spot color "HKS 39 E" from the builtin color library
	     * HKS */
	    spot = p.makespotcolor("HKS 39 E");
	    
	    /* Set the spot color "HKS 39 E" with a tint value of 1 (=100%)
	     * and output some text */
	    p.setcolor("fill", "spot", spot, 1.0, 0, 0);
	    
	    p.fit_textline("HKS 39 E spot color with a tint value of 100%",
		x, y -= 50, "font=" + font + " fontsize=16");
	    
	    /* Set the spot color "HKS 38 E" with a tint value of 0.7 (=70%)
	     * and output some text */
	    p.setcolor("fill", "spot", spot, 0.7, 0, 0);
	    
	    p.fit_textline("HKS 39 E spot color with a tint value of 70%",
		x, y -= 30, "font=" + font + " fontsize=16");
	    
	  
	    /* ---- Define and use a custom spot color based on a CMYK ----
	     * ---- alternate color                                    ---- */
	     
	    /* Set a CMYK color used as alternate CMYK color for the spot color */ 
	    p.setcolor("fill", "cmyk", 0, 0.2, 0.9, 0);
	    
	    /* Define a custom spot color called "CompanyLogo" with the alternate
	     * CMYK values set above
	     */
	    spot = p.makespotcolor("CompanyLogo");
	    
	    /* Now set the spot color "CompanyLogo" with a tint value of 1 and
	     * output some text */
	    p.setcolor("fill", "spot", spot, 1, 0, 0);
	    p.fit_textline("CompanyLogo custom spot color with a tint value of " +
		"100%", x, y -= 50, "font=" + font + " fontsize=16");

	    p.end_page_ext("");

	    p.end_document("");

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