通常、PDFlibでは2色間のグラデーションをサポートしていますが、グラデーションパターンを組み合わせることにより、2色以上のグラデーションの表示も可能です。
Q & A 「
複数色でのグラデーション表示」で、処理手順やソースコードをご紹介しておりますのでご覧ください。
/*
* グラデーション:
* PDFlib で、エリアやテキストをグラデーションカラーで塗り潰すサンプル
* プログラムです。
*
* 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
* 必要なデータ : none
*/
package com.pdflib.cookbook.pdflib.color;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class color_gradient {
public static void main(String argv[]) {
/* 必要に応じてデータファイルがあるフォルダのパスを指定する */
String searchpath = "../input";
String outfile = "color_gradient.pdf";
String title = "Color Gradient";
pdflib p = null;
int font, sh, pattern;
int fontsize = 36;
int exitcode = 0;
try {
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
/* load_font() 等でエラーが起きた場合、-1を返す */
p.set_option("errorpolicy=return");
if (p.begin_document(outfile, "destination={type=fitwindow}") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* 出力PDFページを作成する */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/*
* ページを2色(緑から黒)のグラデーションで塗りつぶす:
*
* グラデーション開始色の緑をオプション'startcolor'に指定する
*
* グラデーション終了色の黒をパラーメータ c1、c2、c3にセットする
*
* PDFlib 9.1以降では、グラデーションの終了色を新しいオプション
* 'endcolor'で設定できるようになった。
* 指定方法は下記の通り
*
* sh = p.shading("axial", 0, 0, 595, 842, 0.0, 0.0, 0.0, 0.0,
* "startcolor={rgb 0.0 0.5 0.5} endcolor={rgb 0 0 0}");
*
* この場合、パラメータ c1、c2、c3 は無視される
*/
sh = p.shading("axial", 0, 0, 595, 842, 0.0, 0.0, 0.0, 0.0,
"startcolor={rgb 0.0 0.5 0.5}");
/* ページ全体にグラデーションを適用する */
p.shfill(sh);
/*
* 矩形のグラデーション表示:
*
* グラデーションの開始色としてオレンジを設定する。
* 終了色としてライトオレンジを指定する。
* 塗りつぶす矩形と同じサイズの線形グラデーションを定義する
* シェーディングパターンを生成する。
*/
sh = p.shading("axial", 200, 200, 450, 450, 0.9, 0.8, 0.8, 0.0,
"startcolor={rgb 1.0 0.5 0.1}");
pattern = p.shading_pattern(sh, "");
/*
* 塗りつぶし色としてシェーディングパターンを使用し、矩形を描く
*/
p.set_graphics_option("fillcolor={pattern " + pattern + "}");
p.rect(200, 200, 250, 250);
p.fill();
/*
* 円のグラデーション表示:
*
* グラデーションの開始色として白を設定する。
* 終了色としてオレンジを指定する。
* 塗りつぶす円と同じサイズの円状のグラデーションを定義する。
* シェーディングパターンを生成する。
*/
sh = p.shading("radial", 400, 600, 400, 600, 1.0, 0.5, 0.1, 0.0,
"r0=0 r1=60 startcolor={rgb 1.0 1.0 1.0}");
pattern = p.shading_pattern(sh, "");
/*
* 塗りつぶし色としてシェーディングパターンを使用し、円を描く
*/
p.set_graphics_option("fillcolor={pattern " + pattern + "}");
p.circle(400, 600, 50);
p.fill();
/*
* グラデーションパターンでテキストを出力: フォントをロードする
*/
font = p.load_font("Helvetica-Bold", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/*
* グラデーションの開始色として白を設定する。
* 終了色としてオレンジを指定する。
* *塗りつぶすテキストと同じサイズの線形グラデーションを定義する
* シェーディングパターンを生成する。
*/
sh = p.shading("axial", 50, 50, 50, 200, 1.0, 0.5, 0.1, 0.0,
"startcolor={rgb 1.0 1.0 1.0}");
pattern = p.shading_pattern(sh, "");
/*
* テキストの塗りつぶし色として、シェーディングパターンを使用する
* ためのオプションリスト。フォントとフォントサイズも指定する。
*/
String optlist = "fillcolor={pattern " + pattern + "} "
+ "font=" + font + " fontsize=" + fontsize;
/*
* 塗りつぶし色としてシェーディングパターンを適用し、テキストを出力する
*/
p.fit_textline("Hello World!", 50, 100, optlist);
p.fit_textline("(says PDFlib GmbH)", 50, 100 - fontsize, optlist);
p.end_page_ext("");
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);
}
}
}