PDFlib

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

複数色でのグラデーション表示

Q.複数色でグラデーションを表示することはできますか?

A.PDFlibでは通常 2色間でのグラデーションを表示することができますが、グラデーションのパターンを組み合わせることにより、2色以上のグラデーションを表示することが可能です。

※2色間でのグラデーション表示については、PDFlibサンプル集(クックブック)のグラデーションをご覧ください。

出力イメージ

このように、複数色のグラデーションパターンを用いて、色々な図形や文字を塗りつぶしたり、線を書くこともできます。

処理手順

7色のグラデーションで "Hello World! (says PDFlib GmbH)" を出力する方法を解説します。

  1. パターン定義を開始します。あらかじめ、グラデーションの使用範囲を想定してパターンのサイズを指定します。
  2. 
    /* パターン定義を開始する。 サイズ:595×150 */
    pattern3 = p.begin_pattern_ext(595, 150, "");
    						

  3. 上記で作成したパターンサイズの範囲内で、グラデーションのパターンを作成します。
  4. 図1 のように、白→赤、赤→黄、黄→緑、緑→水、水→青、青→黒 の6種類のパターンをsetcolor() 関数と shading() 関数を用いて作成します。

    setcolor() 関数の第3~6パラメータにはパターンの開始色の色を指定します。shading()関数の第2・3パラメータにはパターン色の開始位置の座標(x,y)、第4・5パラメータには終了位置の座標(x,y)、第6~9パラメータにはパターンの終了色を指定します。

    また、今回は線形のグラデーションを表示しますので、第1パラメータには"axial"を指定します。

    
    /* 開始色”白”*/
    p.setcolor("fill", "rgb", 1.0, 1.0, 1.0, 0.0);
    /* 終了色”赤” 開始位置(0,0)、終了位置(100,0)*/
    sh = p.shading("axial", 0, 0, 100, 0, 1.0, 0.0, 0.0, 0.0, "");
    /* 塗りつぶす */
    p.shfill(sh);
    
    /* 開始色”赤”*/
    p.setcolor("fill", "rgb", 1.0, 0.0, 0.0, 0.0);
    /* 終了色”黄色”開始位置(100,0)、終了位置(200,0) */
    sh = p.shading("axial", 100, 0, 200, 0, 1.0, 1.0, 0.0, 0.0, "");
    /* 塗りつぶす */
    p.shfill(sh);
                                   ・
                                   ・
                                   ・

  5. パターン定義を終了します。
  6. 
    /* パターン定義を終了する */
    p.end_pattern();

  7. テキストを出力します。
    作成したパターンを使用するには、setcolor()関数の第2パラメータに"pattern"を、第3パラメータにはbegin_pattern_ext( )によって返されたパターンハンドルを指定します。
  8. 
    /* 使用するグラデーションパターンを設定 */
    p.setcolor("fill", "pattern", pattern3, 0, 0, 0);
    /* テキストの出力 */
    p.fit_textline("Hello World!", 50, 100, "");
    p.fit_textline("(says PDFlib GmbH)", 200, 100 - fontsize, "");

斜め方向や上下方向のグラデーション表示

左右方向のグラデーションパターンを作成し、パターンを回転させることで、斜め方向や上下方向のグラデーションが表示できます。

パターン定義を開始する際、begin_pattern_ext()関数のオプションで、キーワードrotateより、パターンの向きを指定します。またキーワードtranslateでパターンの位置の微調整もできます。

    
    /* パターンを反時計回りに90度回転、パターン位置をx方向に40、y方向に移動 */
    p.begin_pattern_ext(160, 160, "transform={rotate=90 translate={40 0}}");

サンプルソースコード

ソースコードのダウンロード(Java)


/*
 * 複数色でグラデーションを表示するサンプルプログラムです。
 *
 * 必要な製品:PDFlib/PDFlib+PDI/PPS
 *
 * 2018 Copyright (c) infoTek K.K. all rights reserved.
 * 当ソースコードにより生じたすべての不利益について、当社は責任を負いません。
 *
 */

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

public class many_colors_gradient {
    public static void main(String argv[]) {
        String outfile = "many_colors_gradient.pdf";
        String title = "Many Color Gradient";

        pdflib p = null;
        int font, sh, pattern1, pattern2, pattern3;
        int fontsize = 36;
        try {
            p = new pdflib();

            p.set_option("errorpolicy=return");

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

            p.set_info("Creator", "infoTek K.K.");
            p.set_info("Title", title);

            /* ページを開始する */
            p.begin_page_ext(595, 842, "");


            //--- パターン1 :左下から右上方向のグラデーション(4色)

            /* パターン定義を開始する */
            pattern1 = p.begin_pattern_ext(360, 360, "transform={rotate=45 translate={-80 0}}");

            /* 開始色"緑" */
            p.setcolor("fill", "rgb", 0.0, 1.0, 0.0, 0.0);
            /* 終了色"水色" */
            sh = p.shading("axial", 0, 0, 120, 0, 0.0, 1.0, 1.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"水色" */
            p.setcolor("fill", "rgb", 0.0, 1.0, 1.0, 0.0);
            /* 終了色"青" */
            sh = p.shading("axial", 120, 0, 240, 0, 0.0, 0.0, 1.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"青" */
            p.setcolor("fill", "rgb", 0.0, 0.0, 1.0, 0.0);
            /* 終了色"黒" */
            sh = p.shading("axial", 240, 0, 360, 0, 0.0, 0.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* パターン定義を終了する */
            p.end_pattern();


            //--- パターン2 : 下から上方向のグラデーション(4色)

            /* パターン定義を開始する */
            pattern2 = p.begin_pattern_ext(160, 160, "transform={rotate=90 translate={40 0}}");

            /* 開始色"白" */
            p.setcolor("fill", "rgb", 1.0, 1.0, 1.0, 0.0);
            /* 終了色"赤" */
            sh = p.shading("axial", 0, 0, 50, 0, 1.0, 0.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"赤" */
            p.setcolor("fill", "rgb", 1.0, 0.0, 0.0, 0.0);
            /* 終了色"黄" */
            sh = p.shading("axial", 50, 0, 80, 0, 1.0, 1.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"黄" */
            p.setcolor("fill", "rgb", 1.0, 1.0, 0.0, 0.0);
            /* 終了色"緑" */
            sh = p.shading("axial", 80, 0, 160, 0, 0.0, 1.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* パターン定義を終了する */
            p.end_pattern();


            //---パターン3 : 左から右方向のグラデーション(7色)

            /* パターン定義を開始する */
            pattern3 = p.begin_pattern_ext(595, 150, "");

            /* 開始色"白" */
            p.setcolor("fill", "rgb", 1.0, 1.0, 1.0, 0.0);
            /* 終了色"赤" */
            sh = p.shading("axial", 0, 0, 100, 0, 1.0, 0.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"赤" */
            p.setcolor("fill", "rgb", 1.0, 0.0, 0.0, 0.0);
            /* 終了色"黄" */
            sh = p.shading("axial", 100, 0, 200, 0, 1.0, 1.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"黄" */
            p.setcolor("fill", "rgb", 1.0, 1.0, 0.0, 0.0);
            /* 終了色"緑" */
            sh = p.shading("axial", 200, 0, 300, 0, 0.0, 1.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"緑" */
            p.setcolor("fill", "rgb", 0.0, 1.0, 0.0, 0.0);
            /* 終了色"水色" */
            sh = p.shading("axial", 300, 0, 400, 0, 0.0, 1.0, 1.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"水色" */
            p.setcolor("fill", "rgb", 0.0, 1.0, 1.0, 0.0);
            /* 終了色"青" */
            sh = p.shading("axial", 400, 0, 500, 0, 0.0, 0.0, 1.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* 開始色"青" */
            p.setcolor("fill", "rgb", 0.0, 0.0, 1.0, 0.0);
            /* 終了色"黒" */
            sh = p.shading("axial", 500, 0, 600, 0, 0.0, 0.0, 0.0, 0.0, "");
            /* 塗りつぶす */
            p.shfill(sh);

            /* パターン定義を終了する */
            p.end_pattern();


            //--- 矩形を描く

            /* 使用するグラデーションパターンを設定 */
            p.setcolor("fill", "pattern", pattern1, 0, 0, 0);
            p.rect(200, 200, 250, 250);
            p.fill();


            //---円を描く

            /* 使用するグラデーションパターンを設定 */
            p.setcolor("fill", "pattern", pattern2, 0, 0, 0);
            p.circle(400, 600, 80);
            p.fill();


            //---テキストを出力する

            /* フォントをロードする */
            font = p.load_font("Helvetica-Bold", "unicode", "");

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

            p.setfont(font, fontsize);

            /* 使用するグラデーションパターンを設定 */
            p.setcolor("fill", "pattern", pattern3, 0, 0, 0);
            p.fit_textline("Hello World!", 50, 100, "");
            p.fit_textline("(says PDFlib GmbH)", 200, 100 - fontsize, "");


            //--- 線を描く

            /* 使用するグラデーションパターンを設定 */
            p.setcolor("stroke", "pattern", pattern3, 0, 0, 0);
            p.moveto(30,40);
            p.lineto(550,40);
            p.stroke();


            /* ページ終了する */
            p.end_page_ext("");

            /* ドキュメントを終了する */
            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");
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
        finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}
Java 1.7 / PDFlib 9
(Jan 30, 2018 - )