PDFlib Cookbook

cookbook

pdfx/starter_pdfx5n updated

Create PDF/X-5n conforming output with an image and DeviceN color.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * PDF/X-5n starter:
 * Create PDF/X-5n conforming output with an image and DeviceN color
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file, image file, 7-colorant ('xCLR') ICC output
 *                intent profile
 */

package com.pdflib.cookbook.pdflib.pdfx;

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

class starter_pdfx5n {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        final String searchpath = "../input";

        pdflib p = null;
        final String imagefile = "nesrin_cmyk.jpg";
        
        // 7-colorant (xCLR) output intent ICC profile FOGRA55
        final String profilename =  "Ref-ECG-CMYKOGV_FOGRA55_TAC300.icc";
        
        /* The FOGRA55 ICC profile contains the following process colors:
            LGOMCCHANNEL01	"InkName = 'Cyan' LabFullToneColor = '62 -44 -50'"
            LGOMCCHANNEL02	"InkName = 'Magenta' LabFullToneColor = '52 81 -7'"
            LGOMCCHANNEL03	"InkName = 'Yellow' LabFullToneColor = '95 -6 95'"
            LGOMCCHANNEL04	"InkName = 'Black' LabFullToneColor = '12 2 0'"
            LGOMCCHANNEL05	"InkName = 'Orange' LabFullToneColor = '65 58 88'"
            LGOMCCHANNEL06	"InkName = 'Green' LabFullToneColor = '60 -75 0'"
            LGOMCCHANNEL07	"InkName = 'Violet' LabFullToneColor = '22 47 -56'"
         */
        
        int image, devicen;
        double width, height;
        int exitcode = 0;

        // Transform function for 2-color Lab-based spot color
        final String transform2 =
              "% DeviceN transform function for N=2 in CIE L*a*b* color space\n"
            + "% Lab color values of input colors must be listed here:\n"
            + "65 58 88                         % color 1: Orange\n"
            + "22 47 -56                        % color 2: Violet\n"
            + "% blend L values\n"
            + "7 index 6 index mul              % t1*L1\n"
            + "7 index 4 index mul              % t2*L2\n"
            + "add\n"
            + "9 1 roll                         % bottom: L\n"
            + "% blend a values\n"
            + "7 index 5 index mul              % t1*a1\n"
            + "7 index 3 index mul              % t2*a2\n"
            + "add\n"
            + "9 1 roll                         % bottom: a\n"
            + "% blend b values\n"
            + "7 index 4 index mul              % t1*b1\n"
            + "7 index 2 index mul              % t2*b2\n"
            + "add\n"
            + "9 1 roll                         % bottom: b\n"
            + "pop pop pop pop pop pop pop pop\n";

        try {
            p = new pdflib();

            /*
             * Set errorpolicy to return, this means we must check return
             * values of load_font() etc.
             * Set the search path for fonts and images etc.
             */
            p.set_option("errorpolicy=return SearchPath={{" + searchpath + "}}");

            if (p.begin_document("starter_pdfx5n.pdf", "pdfx=PDF/X-5n") == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            p.set_info("Creator", "PDFlib starter sample");
            p.set_info("Title", "starter_pdfx5n");

            if (p.load_iccprofile(profilename, "usage=outputintent") == -1) {
                System.err.println("Error: " + p.get_errmsg());
                System.err.println(
                    "An n-colorant ('xCLR') printer profile is required as output intent for PDF/X-5n.");
                p.delete();
                System.exit(2);
            }

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            p.fit_textline(
                "PDF/X-5n starter sample with 7-color output intent", 50, 800,
                "fontsize=18 fontname=NotoSerif-Regular ");

            /*
             * Create a DeviceN color space with two of the device's process
             * colors. The following types of spot colors can be used for
             * PDF/X-5n:
             * - all built-in Pantone and HKS colors
             * - spot colors described in the n-colorant output intent profile
             * - custom spot colors defined with makespotcolor()
             * 
             * Spot colors listed in the 7-color ICC profile don't have to be
             * defined explicitly since PDFlib reads the definitions from the
             * ICC profile.
             */
            devicen = p.create_devicen(
                "names={{Orange} {Violet}} alternate=lab transform={{" + transform2
                    + "}} errorpolicy=exception");
            p.set_graphics_option("fillcolor={devicen " + devicen + " 0.9 0.2}");

            /* Fill a circle with the DeviceN color defined above */
            p.circle(400, 600, 100);
            p.fill();

            /* Load a CMYK image without embedded ICC profile*/
            image = p.load_image("auto", imagefile, "");

            if (image == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }
            
            String scaleopt="scale=0.75";

            /* Place a diagonal stamp across the image area */
            width = p.info_image(image, "width", scaleopt);
            height = p.info_image(image, "height", scaleopt);

            /* Place the image on the page and close it */
            p.fit_image(image, (double) 0.0, (double) 0.0, scaleopt);
            p.close_image(image);

            p.fit_textline("CMYK image", 0, 0,
                "fontsize=48 fontname=NotoSerif-Regular " + 
                " boxsize={" + width + " " + height + "} stamp=ll2ur");

            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);
        }
    }
}