PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib サンプル集(クックブック)

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。

イメージの大きさ

PDFlib で、さまざまな大きさのイメージを配置するサンプルプログラムです。

matchbox オプションを使用してイメージのサイズを取得し、そのオリジナルサイズでイメージを配置します。また、イメージの右に直接テキストを表示します。

ボックス内にイメージをフィットさせ、ボックスのサイズを使って、イメージに合うよう直接中央に配置します。

イメージのサイズを持つページを作成して、ページを完全に覆うようにイメージを配置します。


/* $Id: image_dimensions.java,v 1.8 2013/01/15 10:11:59 stm Exp $
 * Image dimensions:
 * Get the dimensions of an image for various purposes
 *
 * Place an image in its original size, retrieve its dimensions using the
 * "matchbox" option and place text directly to the right of the image. Fit an
 * image into a box and use the box dimensions to center text directly below
 * the image. Create a page with the dimensions of an image and place the image
 * so that it covers the page completely.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class image_dimensions
{
    public static void main (String argv[])
    {
    /* This is where the data files are. Adjust as necessary. */
    String searchpath = "../input";
    String outfile = "image_dimensions.pdf";
    String title = "Image Dimensions";

    pdflib p = null;
    String imagefile = "websurfer.jpg";
    String imagefile2 = "nesrin.jpg";
    int font, image;
    double x2 = 0.0, y2 = 0.0, y3 = 0.0;
    double imagewidth, imageheight;

    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.8 $");

        /* On page 1, place an image in its original size, retrieve its
         * dimensions using the "matchbox" option and place text directly
         * to the right of the image
         */
        p.begin_page_ext(0, 0, "width=100 height=100");

        /* Load the image */
        image = p.load_image("auto", imagefile, "");
        if (image == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Fit the image with a matchbox called "web" respresenting
         * the image dimensions */
        p.fit_image(image, 20, 20, "matchbox={name=web}");
        p.close_image(image);

        /* Retrieve the coordinates of the lower right (x2, y2) and upper right
         * (x3, Y3) image corners via the matchbox "web" specified above
         */
        if ((int) p.info_matchbox("web", 1, "exists") == 1)
        {
            x2 = p.info_matchbox("web", 1, "x2");
            y2 = p.info_matchbox("web", 1, "y2");
            y3 = p.info_matchbox("web", 1, "y3");
        }

        font = p.load_font("Helvetica", "unicode", "");
        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Start the text line in x direction at the lower right corner of the
         * image and in y direction 1/4 of the image height up
         */
        p.fit_textline("www.kraxi.com", x2, y2 + (y3 - y2)/4, "font=" + font +
            " fontsize=8");

        p.end_page_ext("");

        /* On page 2, fit an image into a box and use the box dimensions to
         * center text directly below the image
         */
        p.begin_page_ext(0, 0, "width=100 height=100");

        /* Load the image */
        image = p.load_image("auto", imagefile2, "");
        if (image == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Fit the image with a matchbox called "web" respresenting
         * the image dimension
         */
        p.fit_image(image, 10, 20, "boxsize={80 50} position={center bottom} " +
            "fitmethod=meet");
        p.close_image(image);

        font = p.load_font("Helvetica", "unicode", "");
        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Start the text line in x direction at the lower right corner of the
         * image and in y direction 1/4 of the image height up
         */
        p.fit_textline("My holiday photo", 10, 10,
            "boxsize={80 50} position={center bottom} font=" + font +
            " fontsize=8");

        p.end_page_ext("");

        /* Create page 3 with the original dimensions of the image and place
         * the image so that it covers the page completely
         *
         * Load the image
         */
        image = p.load_image("auto", imagefile2, "");
        if (image == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Get the width and height of the image */
        imagewidth = p.info_image(image, "imagewidth", "");
        imageheight = p.info_image(image, "imageheight", "");

        /* Start page 3 with the dimensions of the image to be placed */
        p.begin_page_ext(imagewidth, imageheight, "");

        /* Fit the image */
        p.fit_image(image, 0, 0, "");

        p.close_image(image);

        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)