イメージとテキストを保持し、ビジネスレターのヘッダーとして PDFlib テンプレート(PDF の XObject フォーム)を作成します。これをテンプレートとして配置します。
複数のページにテンプレートを配置する場合でも、PDF ファイルにテンプレートとして構築されるのは一度だけで済むため、出力されるファイルサイズを小さくすることができます。
/*
* コンテンツの繰返し:
* 固定ヘッダーやフッターなど、複数のページで同じように使用されるコンテンツを
* 作成します。
*
* 画像とテキストを含んだビジネスレターのヘッダーを PDFlib テンプレート
* (PDF Form XObject になる) として作成し、配置します。
* テンプレートを複数のページに配置する (または同じページに複数配置する) 場合、
* テンプレートを構築するための実際の PDF 演算子は PDF ファイルに 1 回だけ
* 含まれるため、PDF 出力ファイルのサイズを節約することができます。
*
* 必要な製品: PDFlib/PDFlib+PDI/PPS 10
* 必要なデータ: 画像ファイル
*/
package com.pdflib.cookbook.pdflib.general;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class repeated_contents
{
public static void main (String argv[])
{
/* 必要に応じてデータファイルがあるフォルダのパスを指定する */
String searchpath = "../input";
String outfile = "repeated_contents.pdf";
String title = "Repeated Contents";
pdflib p = null;
String imagefile = "kraxi_header.tif";
int i, j, templ, image;
int x = 30, y;
int pagewidth=595, pageheight=842;
int exitcode = 0;
String[][] addresses =
{
{ "Mr Bee", "3, Rose Gardens", "London" },
{ "Miss Hopper", "26, Shakespeare Road", "Hopperfield" },
{ "Mr Duck", "128, Chapel Hill", "Bournemouth" },
{ "Miss Haley", "50, Virginia Street", "Southport" }
};
try {
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
/* load_font()等の戻り値を確認する */
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);
/* テンプレートに含める画像を読み込む */
image = p.load_image("auto", imagefile, "");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
/* ビジネスレターのヘッダーを含むテンプレートを定義する。
* 画像と、送信者アドレスのテキストを含むビジネスレターの
* ヘッダーを定義する。
*/
templ = p.begin_template_ext(pagewidth, pageheight, "");
/* 画像をテンプレートに配置する */
p.fit_image(image, pagewidth-230, pageheight-140,
"boxsize={200 100} fitmethod=meet");
/* テキストをテンプレートに配置する */
p.fit_textline("Kraxi Systems, Inc., 17, Aviation Road, Paperfield",
30, pageheight-160, "fontname=NotoSerif-Regular fontsize=8");
/* テンプレートを完成させる */
p.end_template_ext(0, 0);
/* 画像をクローズする */
p.close_image(image);
/* 1ページずつ4通のレターを作成する。
* 各ページに、差出人の住所、顧客の住所、説明文などを記載したテンプレート
* を配置する。
*/
for (i = 0; i < 4; i++) {
p.begin_page_ext(pagewidth, pageheight, "");
y = pageheight - 165;
/* 画像を使用するのと同じように、ページ上にテンプレートを
* 配置する
*/
p.fit_image(templ, 0.0, 0.0, "");
/* 顧客のアドレスを配置する */
for (j = 0; j < 3; j++) {
p.fit_textline(addresses[i][j], x, y-=15,
"fontname=NotoSerif-Regular fontsize=12");
}
/* 実際のページコンテンツをページ上に配置する */
p.fit_textline("Dear customer, this is the actual page contents " +
"of page " + (i+1) + ".", x, y-=80,
"fontname=NotoSerif-Regular fontsize=12");
p.fit_textline("The image and the sender's address above are " +
"part of a template.", x, y -=20,
"fontname=NotoSerif-Regular fontsize=12");
p.end_page_ext("");
}
/* 必要であれば、さらに別のページにもテンプレートを配置する。
* その後、クローズする。
*/
p.close_image(templ);
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);
}
}
}