BASKET
Search
PDFlib

color/blendmode

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: blendmode.java,v 1.6 2017/01/25 15:56:01 stm Exp $

 *

 * Blend modes:

 * Demonstrate all blend modes which are available in PDF

 *

 * The following items are used for demonstration:

 * - an RGB image

 * - a black and white SVG logo (with white background)

 *

 * Each blend mode is demonstrated twice, once with the image as background

 * and once with the image as foreground.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 9.1

 *   

 * Required data: image, SVG graphics

 */

package com.pdflib.cookbook.pdflib.color;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class blendmode {


    static final double WIDTH = 170, HEIGHT = 170;


    static void place_image(pdflib p, int image) throws PDFlibException {

        /* Place image */

        p.fit_image(image, 0, 0, "boxsize={" + WIDTH + " " + HEIGHT + "} "

            + "fitmethod=slice position={0 0}");

    }


    static void paint(pdflib p, int shp, int graphics) throws PDFlibException {

        /* Paint with color */

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


        p.rect(0, 0, WIDTH, HEIGHT / 2);

        p.fill();


        /* Place SVG graphics */

        p.fit_graphics(graphics, 0.2 * WIDTH, 0.6 * HEIGHT,

            "boxsize={ " + 0.6 * WIDTH + " " + 0.3 * HEIGHT

                + "} position={center} fitmethod=meet");

    }


    /*

     * Create a template which demonstrates a particular blend mode

     */

    static int create_blendmode_sample(pdflib p, String blendmode,

        boolean image_in_background, int shp, int image, int graphics)

        throws PDFlibException {

        /* Possible optimization: maintain cache of gstates with blend modes */

        int gs = p.create_gstate("blendmode=" + blendmode);


        int templ = p.begin_template_ext(WIDTH, HEIGHT,

            "transparencygroup={ }");


        if (image_in_background) {

            place_image(p, image);

            p.set_gstate(gs); /* Set gstate with selected blend mode */

            paint(p, shp, graphics);

        }

        else {

            paint(p, shp, graphics);

            p.set_gstate(gs); /* Set gstate with selected blend mode */

            place_image(p, image);

        }


        p.end_template_ext(0, 0);


        return templ;

    }


    public static void main(String argv[]) {

        /* This is where the data files are. Adjust as necessary. */

        String searchpath = "../input";

        String graphicsFileName = "PDFlib-blue-logo-white-background.svg";

        String imageFileName = "Lucy.jpg";

        String outFileName = "blendmode.pdf";

        String title = "Blend mode";

        int exitcode = 0;


        /* Borders for the table fitbox */

        final double border = 30;


        class blendmoderecord {

            String name;

            String notes;


            blendmoderecord(String name, String notes) {

                this.name = name;

                this.notes = notes;

            }

        }


        /* Records with all blend modes */

        final blendmoderecord blendmodes[] = {

            new blendmoderecord("Normal",

                "no blending effect: foreground obscures background"),

            new blendmoderecord("Darken", "symmetric mode"),

            new blendmoderecord("Multiply", "symmetric mode"),

            new blendmoderecord("ColorBurn", ""),

            new blendmoderecord("Lighten", "symmetric mode"),

            new blendmoderecord("Screen", "symmetric mode"),

            new blendmoderecord("ColorDodge", ""),

            new blendmoderecord("HardLight", ""),

            new blendmoderecord("SoftLight", ""),

            new blendmoderecord("Overlay", ""),

            new blendmoderecord("Difference", "symmetric mode"),

            new blendmoderecord("Exclusion", "symmetric mode"),

            new blendmoderecord("Hue", ""),

            new blendmoderecord("Saturation", ""),

            new blendmoderecord("Color", ""), new blendmoderecord("Luminosity",

                "Like \"Color\", but with foreground and background exchanged") };


        pdflib p = null;


        try {

            p = new pdflib();

            int i, row;

            int tf = -1, tbl = -1;

            String cellopts, result;


            double pagewidth = 595, pageheight = 842;

            final double margin = 6, rowheight = 180;

            final double fontsize = 12;


            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(outFileName, "") == -1)

                throw new Exception("Error: " + p.get_errmsg());


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

            p.set_info("Title", title + " $Revision: 1.6 $");


            /* Load the font */

            int font = p.load_font("Helvetica", "unicode", "");


            String tf_optlist = "fontname=Helvetica encoding=unicode fontsize="

                + fontsize;


            /* Set some general text options shared among all cells */

            String textoptlist = "fittextline={font=" + font + " fontsize="

                + fontsize + " position={left center}} " + " margin=" + margin

                + " ";


            /* Load image */

            int image = p.load_image("auto", imageFileName, "");


            /*

             * Load the SVG graphics Create a transparency group to ensure that

             * the whole graphic contents interact as an entity with the

             * background.

             */

            int graphics = p.load_graphics("auto", graphicsFileName,

                "templateoptions={transparencygroup={ }}");


            /* Create axial shading */

            int sh = p.shading("axial", 0, HEIGHT / 2, WIDTH, HEIGHT / 2, 0, 0,

                0, 0, "startcolor=black endcolor=white");


            /* shading pattern handle */

            int shp = p.shading_pattern(sh, "");


            /*

             * ----------------------------------

             * Create table header for all cells

             * ----------------------------------

             */


            row = 1;

            cellopts = textoptlist;

            tbl = p.add_table_cell(tbl, 1, row, "Blend mode", cellopts);


            cellopts = textoptlist;

            tbl = p.add_table_cell(tbl, 2, row, "Notes", cellopts);


            tf = p.add_textflow(-1,

                "Image in background,\nlogo and shading in foreground",

                tf_optlist + " lastalignment=center alignment=center");


            cellopts = textoptlist + " textflow=" + tf;

            tbl = p.add_table_cell(tbl, 3, row, "", cellopts);


            tf = p.add_textflow(-1,

                "Logo and shading in background,\nimage in foreground",

                tf_optlist + " lastalignment=center alignment=center");


            cellopts = textoptlist + " textflow=" + tf;

            tbl = p.add_table_cell(tbl, 4, row, "", cellopts);


            /*

             * ------------------------------------------------

             * Add a row for each blend mode

             * ------------------------------------------------

             */

            for (i = 0, row = 2; i < blendmodes.length; i++, row++) {

                /*

                 * -------------------------------------------------------------

                 * Column 1: Blend mode name

                 * -------------------------------------------------------------

                 */

                cellopts = "colwidth=15% " + textoptlist;


                tbl = p.add_table_cell(tbl, 1, row, blendmodes[i].name,

                    cellopts);


                /*

                 * ---------------------------------------------

                 * Column 2: Notes

                 * ---------------------------------------------

                 */


                tf = p.add_textflow(-1, blendmodes[i].notes, tf_optlist);


                cellopts = "colwidth=20% " + textoptlist + " textflow=" + tf;

                tbl = p.add_table_cell(tbl, 2, row, "", cellopts);


                /*

                 * -------------------------------------------------------------

                 * Column 3: image in background

                 * -------------------------------------------------------------

                 */

                int templ = create_blendmode_sample(p, blendmodes[i].name, true,

                    shp, image, graphics);


                cellopts = "rowheight=" + rowheight + " margin=3 image="

                    + templ;


                tbl = p.add_table_cell(tbl, 3, row, "", cellopts);


                /*

                 * -------------------------------------------------------------

                 * Column 4: image in foreground

                 * -------------------------------------------------------------

                 */

                templ = create_blendmode_sample(p, blendmodes[i].name, false,

                    shp, image, graphics);


                cellopts = "rowheight=" + rowheight + " margin=3 image="

                    + templ;


                tbl = p.add_table_cell(tbl, 4, row, "", cellopts);


            } /* for */


            /*

             * -----------------------------------------------------------------

             * Fit the table. Using "header=1" the table header will include the

             * first line. Using "line=horother linewidth=0.3" the ruling is

             * specified with a line width of 0.3 for all horizontal lines.

             * -----------------------------------------------------------------

             */

            String tableoptlist =

                "showgrid header=1 stroke={ {line=horother linewidth=0.3}}";


            do {

                p.begin_page_ext(pagewidth, pageheight, "");


                /* Place the table instance */

                result = p.fit_table(tbl, border, border, pagewidth - border,

                    pageheight - border, tableoptlist);

                if (result.equals("_error"))

                    throw new Exception(

                        "Couldn't place table : " + p.get_errmsg());


                p.end_page_ext("");

            }

            while (result.equals("_boxfull"));


            /* Check the result; "_stop" means all is ok. */

            if (!result.equals("_stop")) {

                if (result.equals("_error")) {

                    throw new Exception(

                        "Error when placing table: " + p.get_errmsg());

                }

            }


            /* This will also delete Textflow handles used in the table */

            p.delete_table(tbl, "");


            p.close_image(image);


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

        }

    }

}