PDFlib Cookbook

cookbook

color/svg_color_extension

Download Java Code    Switch to PHP Code      Show Output PDF

/* $Id: svg_color_extension.java,v 1.2 2017/01/26 10:50:40 stm Exp $
 * Demonstrate SVG color extensions
 * Load SVG graphics with use of extended color and fit into a box
 *
 * 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 $Revision: 1.2 $");

    	    /*
    	     * 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());

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

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