PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib サンプル集(クックブック)

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。

ウェブカラー名称

カラー名称と PDFlib で使用される RGB 値を表わす 16 進数を含む全てのクロスブラウザカラーを一覧します。

クロスブラウザカラー名称は、全てのメジャーなブラウザによりサポートされる 150 近くのカラー名称からなります。

必要な製品:PDFlib または PDFlib+PDI または PPS


/*
 * Web color names:
 * Create a list of Web colors with color names and RGB color patches.
 * 
 * The color names are a collection of nearly 150 color names which
 * are supported in many Web contexts. Names and RGB values are built
 * into PDFlib. They are taken from http://www.w3.org/TR/SVG/types.html.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 *    
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.color;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class web_colornames {

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

        pdflib p = null;
        int row, font, tbl = -1;
        String optlist, cellopts, result;

        final int margin = 4;
        final int fontsize = 10;
        int exitcode = 0;
        /* number of columns */
        final int nocol = 3*2;
        String vertline = "";

        /*
         * Height of a table row which is the sum of a font size of 12 and the
         * upper and lower cell margin of 3 each
         */
        int rowheight = 18;

        /* Width of the first, second, and third column of the table */
        final int colwidth[] = {150, 100};

        /* Coordinates of the lower left corner of the table fitbox */
        final double llx = 35, lly = 30;
        /* Coordinates of the upper right corner of the table fitbox */
        final double urx = 565, ury = 807;

        final String header[] = {"Web Color name", "color"};

        /* HTML color names */
        final String colors[] = {
            "aliceblue",
            "antiquewhite",
            "aqua",
            "aquamarine",
            "azure",
            "beige",
            "bisque",
            "black",
            "blanchedalmond",
            "blue",
            "blueviolet",
            "brown",
            "burlywood",
            "cadetblue",
            "chartreuse",
            "chocolate",
            "coral",
            "cornflowerblue",
            "cornsilk",
            "crimson",
            "cyan",
            "darkblue",
            "darkcyan",
            "darkgoldenrod",
            "darkgray",
            "darkgreen",
            "darkgrey",
            "darkkhaki",
            "darkmagenta",
            "darkolivegreen",
            "darkorange",
            "darkorchid",
            "darkred",
            "darksalmon",
            "darkseagreen",
            "darkslateblue",
            "darkslategray",
            "darkslategrey",
            "darkturquoise",
            "darkviolet",
            "deeppink",
            "deepskyblue",
            "dimgray",
            "dimgrey",
            "dodgerblue",
            "firebrick",
            "floralwhite",
            "forestgreen",
            "fuchsia",
            "gainsboro",
            "ghostwhite",
            "gold",
            "goldenrod",
            "gray",
            "grey",
            "green",
            "greenyellow",
            "honeydew",
            "hotpink",
            "indianred",
            "indigo",
            "ivory",
            "khaki",
            "lavender",
            "lavenderblush",
            "lawngreen",
            "lemonchiffon",
            "lightblue",
            "lightcoral",
            "lightcyan",
            "lightgoldenrodyellow",
            "lightgray",
            "lightgreen",
            "lightgrey",
            "lightpink",
            "lightsalmon",
            "lightseagreen",
            "lightskyblue",
            "lightslategray",
            "lightslategrey",
            "lightsteelblue",
            "lightyellow",
            "lime",
            "limegreen",
            "linen",
            "magenta",
            "maroon",
            "mediumaquamarine",
            "mediumblue",
            "mediumorchid",
            "mediumpurple",
            "mediumseagreen",
            "mediumslateblue",
            "mediumspringgreen",
            "mediumturquoise",
            "mediumvioletred",
            "midnightblue",
            "mintcream",
            "mistyrose",
            "moccasin",
            "navajowhite",
            "navy",
            "oldlace",
            "olive",
            "olivedrab",
            "orange",
            "orangered",
            "orchid",
            "palegoldenrod",
            "palegreen",
            "paleturquoise",
            "palevioletred",
            "papayawhip",
            "peachpuff",
            "peru",
            "pink",
            "plum",
            "powderblue",
            "purple",
            "red",
            "rosybrown",
            "royalblue",
            "saddlebrown",
            "salmon",
            "sandybrown",
            "seagreen",
            "seashell",
            "sienna",
            "silver",
            "skyblue",
            "slateblue",
            "slategray",
            "slategrey",
            "snow",
            "springgreen",
            "steelblue",
            "tan",
            "teal",
            "thistle",
            "tomato",
            "turquoise",
            "violet",
            "wheat",
            "white",
            "whitesmoke",
            "yellow",
            "yellowgreen"
        };

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

            /* Load the font */
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Set some general cell options shared between all cells */
            optlist = "fittextline={font=" + font + " fontsize=" + fontsize
                    + " position={left center}} rowheight=" + rowheight
                    + " margin=" + margin + " ";

            /*
             * ------------
             * Add a header 
             * ------------
             */

            /* Set the current row */
            row = 1;
            for (int i=0; i< nocol; i++){

                cellopts = optlist + "colwidth=" + colwidth[i%2];

                tbl = p.add_table_cell(tbl, i+1, row, header[i%2], cellopts);
                if (tbl == -1)
                    throw new Exception("Error: " + p.get_errmsg());

            }
            /*
             * ------------------------------------------------ 
             * For each color, add a row containing three cells
             * ------------------------------------------------
             */

            for (int i = 0; i < colors.length; i += 1) {
                int col = 1 + 2*(i % (nocol/2));
                if (col == 1 ){
                    /* when colo is 1 we are in a new row */
                    row++;
                }
                
                /*
                 * -------------------------------------------------------------
                 * Add the HTML color name in the odd column
                 * -------------------------------------------------------------
                 */
                cellopts = optlist + "colwidth=" + colwidth[col % 2];

                tbl = p.add_table_cell(tbl, col, row, colors[i],
                        cellopts);
                if (tbl == -1)
                    throw new Exception("Error: " + p.get_errmsg());

                /*
                 * -------------------------------------------------------------
                 * Add a rectangle filled with the HTML color in the even
                 * column
                 * -------------------------------------------------------------
                 * 
                 * Since the cell doesn't cover a complete row but only one
                 * column it cannot be filled with color using one of the
                 * row-based shading options. We apply the Matchbox feature
                 * instead to fill the rectangle covered by the cell with the
                 * color specified by the HTML color name.
                 */
                cellopts = "colwidth=" + colwidth[col % 2] + " matchbox={fillcolor="
                        + colors[i] + "}";

                tbl = p.add_table_cell(tbl, col+1, row, "", cellopts);
                if (tbl == -1)
                    throw new Exception("Error: " + p.get_errmsg());

            } /* 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.
             * also draw a vertical line after every second column
             * -----------------------------------------------------------------
             */
            for (int i = 2; i < (nocol); i += 2){
                vertline += "{line=vert" + i + "} ";
            }

            optlist = "header=1 stroke={ {line=horother linewidth=0.3} " + vertline + "}";

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

                /* Place the table instance */
                result = p.fit_table(tbl, llx, lly, urx, ury, optlist);

                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.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);
        }
    }
}
(Apr 3, 2007 - Jun 24, 2024)