PDFlib サンプル集(クックブック)
PDFlib で、印刷時にのみ表示されるレイヤー上にスタンプを出力するサンプルプログラムです。
「stamp」というコンテンツを持つレイヤーを作成します。レイヤープロパティに印刷を設定し、また、画面上への表示をしないように設定します。デフォルトで表示、印刷されるいくつかの標準的なテキストを作成します。
必要な製品:PDFlib または PDFlib+PDI または PPS
/*
* Stamp on print layer:
* Place a stamp on a layer which is only visible upon printing
*
* Create a layer "stamp" which contains the stamp. The layer properties are set
* to print, but not display the layer contents on screen.
* Create some standard text which is displayed and printed by default.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: none
*/
package com.pdflib.cookbook.pdflib.graphics;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class stamp_on_print_layer
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "stamp_on_print_layer.pdf";
String title = "Stamp on Print Layer";
pdflib p = null;
final int x = 50, y = 50;
int tf = -1, font, stamplayer;
int exitcode = 0;
final String textflow =
"To fold the famous rocket looper proceed as follows:\n" +
"Take a DIN A4 sheet.\nFold it lenghtwise in the middle.\nThen, fold " +
"the upper corners down.\nFold the long sides inwards that the " +
"points A and B meet on the central fold.\nFold the points C and D " +
"that the upper corners meet with the central fold as well." +
"\nFold the plane in the middle. Fold the wings down that they close " +
"with the lower border of the plane.";
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");
/* Open the document with the "Layers" navigation tab visible */
if (p.begin_document(outfile, "openmode=layers") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* Load the font */
font = p.load_font("Helvetica", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Start page */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* ---------------------------------------------------------------
* Define the layer "stamp" to be hidden when opening the document
* (initialviewstate=false) and place the stamp on it.
* ---------------------------------------------------------------
*/
stamplayer = p.define_layer("stamp",
"initialviewstate=false initialprintstate=true");
p.begin_layer(stamplayer);
/* Fit a text line as outlines like a stamp in the specified box. The
* stamp will be placed diagonally from the upper left to the lower
* right (stamp=ul2lr).
*/
p.fit_textline("The Famous Rocket Looper", x, y, "font=" + font +
" strokecolor={rgb 1 0 0} textrendering=1 boxsize={500 750} " +
"strokecolor={rgb 0.5 0 1} stamp=ul2lr");
/* End all layers */
p.end_layer();
/* ------------------------------------
* Place some standard text on the page
* ------------------------------------
*/
String optlist = "fontname=Helvetica fontsize=24 encoding=unicode " +
"leading=120% charref";
tf = p.add_textflow(tf, textflow, optlist);
if (tf == -1)
throw new Exception("Error: " + p.get_errmsg());
String result = p.fit_textflow(tf, 100, 100, 400, 700, "");
if (!result.equals("_stop"))
{
/* Check for errors or more text to be placed */
}
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);
}
}
}
(Apr 3, 2007 - Oct 21, 2022)