PDFlib サンプル集(クックブック)
本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。
本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。
Type 3 ロゴ
PDFlib で、イメージから生成された簡単なロゴを含む Type 3 フォントを作成するサンプルプログラムです。
一つのグリフを含む Type 3 ロゴフォントを作成するために TIF イメージのビットマップからイメージデータをインポートします。カスタムエンコーディングでグリフを追加し、そのグリフでテキストを出力します。
/* $Id: type3_rasterlogo.java,v 1.13 2012/12/07 15:58:12 stm Exp $
* Type 3 raster logo font:
* Create a Type 3 font which contains a single logo derived from an image
*
* Import image data from a bitmap TIF image to create a Type 3 logo font
* containing one glyph. Output text with that glyph.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: bitmap TIFF image
*/
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class type3_rasterlogo {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "type3_rasterlogo.pdf";
String title = "Type 3 Raster Logo Font";
pdflib p = null;
String logofile = "phone.tif";
int normalfont, logofont, image;
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 + " $Revision: 1.13 $");
/*
* Create the font PhoneLogoFont. The matrix entries are chosen to
* create the common 1000x1000 coordinate system. These numbers are
* also used when placing the logo within the glyph box below
* (option "boxsize").
*/
p.begin_font("PhoneLogoFont", 0.001, 0.0, 0.0, 0.001, 0.0, 0.0, "");
/*
* The .notdef (fallback) glyph should be contained in all Type 3
* fonts to avoid problems with some PDF viewers. It is usually
* empty.
*/
p.begin_glyph_ext(0x0000, "width=1000");
p.end_glyph();
/*
* Add a glyph with the Unicode value U+260F WHITE TELEPHONE, glyph
* name "phone" and width 1000.
*/
p.begin_glyph_ext(0x260F, "width=1000 boundingbox={0 0 1000 1000} "
+ "glyphname=phone");
/* Load the bitmap data for the glyph from the file "phone.tif". */
image = p.load_image("auto", logofile, "mask");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
/*
* Fit the image in a box similar to the dimensions of the glyph
* box.
*/
p.fit_image(image, 0, 0, "boxsize={1000 1000} fitmethod=meet");
p.close_image(image);
p.end_glyph();
p.end_font();
/* Load the new "PhoneLogoFont" font with the encoding "unicode" */
logofont = p.load_font("PhoneLogoFont", "unicode", "");
if (logofont == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Load the "Helvetica" font */
normalfont = p.load_font("Helvetica", "unicode", "");
if (normalfont == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Start page */
p.begin_page_ext(0, 0, "width=300 height=100");
/* Output the character U+260F of the "PhoneLogoFont" font */
p.fit_textline("\u260f", 20, 50, "font=" + logofont
+ " fontsize=14");
double textxpos = p.get_option("textx", "");
/* Output standard text */
p.fit_textline("This is the phone logo", textxpos + 4, 50, "font="
+ normalfont + " fontsize=14");
textxpos = p.get_option("textx", "");
/*
* Alternatively, we can select the logo glyph via a character
* reference which refers to the glyph name "phone". Use the
* "charref" option to enable character referencing.
*/
p.fit_textline("&.phone;", textxpos + 4, 50, "font=" + logofont
+ " fontsize=14 charref");
/* Finish page */
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();
}
}
}
}
(Apr 3, 2007 - Feb 20, 2014)