/* * ページサイズ: * 様々なページサイズで PDF のページを作成します。 * * A4 サイズで縦向き、横向き、レターサイズで縦向き、横向き、 * ページに配置する画像の寸法に応じたサイズでページを作成します。 * * 必要な製品: PDFlib/PDFlib+PDI/PPS 10 * 必要なデータ: 画像ファイル */ package com.pdflib.cookbook.pdflib.pagination; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class page_sizes { public static void main (String argv[]) { /* 必要に応じてファイルの場所を指定する */ String searchpath = "../input"; String outfile = "page_sizes.pdf"; String title = "Page Sizes"; pdflib p = null; String imagefile = "nesrin.jpg"; int image; double imagewidth, imageheight; int exitcode = 0; 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); /* A4サイズで縦向きのページを作成する */ p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); p.fit_textline("A4 format in Portrait orientation.", 50, 50, "fontname=NotoSerif-Bold fontsize=20"); p.end_page_ext(""); /* A4サイズで横向きのページを作成する */ p.begin_page_ext(0, 0, "width=a4.height height=a4.width"); p.fit_textline("A4 format in Landscape orientation.", 50, 50, "fontname=NotoSerif-Bold fontsize=20"); p.end_page_ext(""); /* レターサイズで縦向きのページを作成する */ p.begin_page_ext(0, 0, "width=letter.width height=letter.height"); p.fit_textline("Letter format in Portrait orientation.", 50, 50, "fontname=NotoSerif-Bold fontsize=20"); p.end_page_ext(""); /* レターサイズで横向きのページを作成する */ p.begin_page_ext(0, 0, "width=letter.height height=letter.width"); p.fit_textline("Letter format in Landscape orientation.", 50, 50, "fontname=NotoSerif-Bold fontsize=20"); p.end_page_ext(""); /* ページに配置する画像のサイズに合わせてページを作成する */ 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", ""); /* ページに収まる画像のサイズでページを開始する */ p.begin_page_ext(imagewidth, imageheight, ""); /* 画像を配置する */ p.fit_image(image, 0, 0, ""); p.close_image(image); 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); } } }