/* * イメージによるテキストの塗潰し: * アウトラインテキストを作成して、イメージによりグリフの内部を塗り潰します。 * * イメージを含んだパターンを定義して、これを塗り潰し色としてテキストを埋めます。 * * 必要な製品: PDFlib/PDFlib+PDI/PPS 9.0.2 * 必要なデータ: 無し */ package com.pdflib.cookbook.pdflib.text_output; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class image_as_text_fill_color { public static void main(String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "image_as_text_fill_color.pdf"; String title = "Image as Text Fill Color"; int exitcode = 0; pdflib p = null; String imagefile = "text_filling.tif"; int font, pattern, image; double imageheight, imagewidth, resx, resy; try { p = new pdflib(); p.set_option("searchpath={" + searchpath + "}"); /* load_font() 等でエラーが起きた場合、-1を返す */ 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); /* フォントをロードする */ font = p.load_font("Helvetica-Bold", "unicode", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); /* 背景画像をロードする */ image = p.load_image("auto", imagefile, ""); if (image == -1) throw new Exception("Error: " + p.get_errmsg()); /* 画像サイズを取得する */ imagewidth = p.info_image(image, "imagewidth", ""); imageheight = p.info_image(image, "imageheight", ""); /* 画像の解像度が 72 dpi を超えることがある。もし、取得した寸法 *(ポイント単位)をパターンの高さと幅に使用し、画像をパターンに * 配置した場合、パターンのサイズに一致せず、白い余白が残る。 * 画像とパターンを同じサイズにするために、解像度に基づいて画像 * サイズを計算する。 * * 元の画像の解像度を取得する */ resx = p.info_image(image, "resx", ""); resy = p.info_image(image, "resy", ""); /* 画像の寸法を計算する */ if (resx > 0) { imagewidth = imagewidth * 72 / resx; imageheight = imageheight * 72 / resy; } /* 取得した画像の寸法を使用してパターンを生成する */ pattern = p.begin_pattern_ext(imagewidth, imageheight, ""); p.fit_image(image, 0, 0, ""); p.end_pattern(); p.close_image(image); /* ページを始める */ p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* パターンを現在の塗り色として設定する。setcolor()を、save() とrestore() * で挟むことで、元の色を引き継ぐことができる。 */ p.save(); /* テキストの塗りつぶし(デフォルト)に加え、textrendering=2 を設定することで * テキストを描線し、黒以外の描線色を設定することもできる。 */ p.set_text_option("textrendering=2"); p.setcolor("stroke", "rgb", 0.5, 0.2, 0.1, 0); p.setcolor("fill", "pattern", pattern, 0, 0, 0); /* 現在の塗りつぶし色(つまりパターンカラー)でテキストを出力する */ int fontsize = 50; p.set_text_option("font=" + font + " fontsize=" + fontsize); p.fit_textline("Hello World!", 50, 500, ""); p.fit_textline("(says PDFlib GmbH)", 50, 500 - fontsize, ""); p.restore(); 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); } } }