PDFlib Cookbook

cookbook

color/svg_color_extension

Demonstrate SVG color extensions: Load SVG graphics with non-sRGB color spaces including CMYK and spot color.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Demonstrate SVG color extensions:
 * Load SVG graphics with non-sRGB color spaces including CMYK and spot color.
 * 
 * The SVG uses color extension for device-gray/rgb/cmyk, cielab,
 * icc-color (RGB and CMYK), icc-named-color for known (Pantone) spot color
 * as well as custom spot color, and device-nchannel color.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: SVG graphics
 */
package com.pdflib.cookbook.pdflib.color;

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

public class svg_color_extension {

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

        pdflib p = null;
        String graphicsfile = "svg_color_extension.svg";
        int graphics;
        int x = 50, y = 200;
        int boxwidth = 500, boxheight = 600;
        String optlist;
        int devicen;
        int exitcode = 0;

        try {
            p = new pdflib();

            p.set_option("SearchPath=" + searchpath);

            /* This means we must check return values of load_graphics() 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", "svg_color_extension");

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            // Define custom spot color "CompanyRed" with Lab alternate values
            // This color is used in the SVG file; its definition must be
            // supplied in the code.
            p.set_graphics_option("fillcolor={spotname {CompanyRed} 1.0 {lab 60 65 65}}");
            
            // Define DeviceN color based on two process colors
            devicen = p.create_devicen(
                "names={Magenta Yellow} alternate=devicecmyk transform={{0 0 4 1 roll}}");

            /* Load the graphics. The option "devicencolors" contains the
             * prepared DeviceN color space handle.
             */
            graphics = p.load_graphics("auto", graphicsfile,
                    "devicencolors={" + devicen + "}");

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

            /* Fit SVG graphics into the specified box */
            optlist = "boxsize={ " + boxwidth + " " + boxheight
                + "} position={center} fitmethod=meet ";

            /*
             * Before actually fitting the graphics we check whether fitting is
             * possible.
             */
            if (p.info_graphics(graphics, "fittingpossible", optlist) == 1) {
                p.fit_graphics(graphics, x, y, optlist);
            }
            else {
                throw new Exception("Cannot place graphics: " + p.get_errmsg());
            }

            p.end_page_ext("");

            p.close_graphics(graphics);

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