PDFlib

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

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

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

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

イメージの基本

PDFlib で、サイズや配置のためのさまざまなオプションを使用してイメージをロードし、表示するサンプルプログラムです。

イメージをそのオリジナルサイズで配置します。

イメージを左向きにしてそのサイズで配置します。

クリッピングのあるボックスの中にイメージをフィットさせます。

適合するようサイズ変更してボックスの中にイメージをフィットさせます。

ボックス内に完全にフィットさせます。

必要な製品:PDFlib または PDFlib+PDI または PDFlib PPS


/*
 * イメージの基本:
 * 拡大縮小や位置決めのためのさまざまなオプションを使用して、
 * 画像を読み込み配置します。
 * 
 * 画像を元のサイズで配置します。
 * 画像を左向きにして拡大縮小して配置します。
 * クリッピングのあるボックスの中に画像をフィットさせます。
 * 比例したサイズ変更で画像をボックスにフィットさせます。
 * 画像を枠内に完全に収めます。
 *
 * 必要な製品: PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ: 画像ファイル
 */
package com.pdflib.cookbook.pdflib.images;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class starter_image
{
    public static void main (String argv[])
    {
        /* 必要に応じてデータファイルがあるフォルダのパスを指定する */
        String searchpath = "../input";
        String outfile = "starter_image.pdf";
        String title = "Starter Image";

        pdflib p = null;
        int exitcode = 0;
        String imagefile = "lionel.jpg";
        int font, image;
        int bw = 400, bh = 200;
        int x = 20, y = 580, yoffset = 260;

        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);
            
            font = p.load_font("Helvetica", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* 画像をロードする */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 1 ページ目を始める */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            p.setfont(font, 12);
            
            
            /* ------------------------------------
             * 画像を元のサイズで配置する
             * ------------------------------------
             */
                    
            /* 左下隅を基準点 (20, 380) にして、元のサイズで画像を配置する。
             */
            p.fit_image(image, 20, 380, "");
            
            /* 説明テキストを出力する */
            p.fit_textline("The image is placed with the lower left corner in " +
                "its original size at reference point (20, 380):", 20, 820, "");
            p.fit_textline("fit_image(image, 20, 380, \"\");", 20, 800, "");
            
            
            /* --------------------------------------------------------
             * 画像を左向きにして拡大縮小して配置する
             * --------------------------------------------------------
             */
            
            /* 画像の右下隅が基準点 (580, 20) になるように配置する
             * "scale=0.5" は、画像を 0.5 倍に拡大する。
             * "orientate=west" は画像を左に向ける。
             */
            p.fit_image(image, 580, 20, "scale=0.5 position={right bottom} " +
                "orientate=west");
            
            /* 説明テキストを出力する */
            p.fit_textline("The image is placed with a scaling of 0.5 and an " +
                    "orientation to the west with the lower right corner", 580, 320,
                "position={right bottom}");
            p.fit_textline(" at reference point (580, 20): fit_image(image, 580, " +
                "20, \"scale=0.5 orientate=west position={right bottom}\");",
                580, 300, "position={right bottom}");
            
            p.end_page_ext("");
            
            /* 2 ページ目を始める */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            p.setfont(font, 12);
            
            
            /* -----------------------------------------------------
             * クリッピングのあるボックスの中に画像をフィットさせる
             * -----------------------------------------------------
             */
              
            /* "boxsize" オプションは、指定された幅と高さを持ち、左下隅が基準点にある
             * ボックスを定義する。
             * "position={right top}" は、画像をボックスの右上に配置する。
             * "fitmethod=clip"は、ボックスに収まるように画像をクリップする。
             */
            p.fit_image(image, x, y, "boxsize={" + bw + " " + bh + "} " +
                "position={right top} fitmethod=clip");

            /* 説明テキストを出力する */
            p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " +
                "position={right top} fitmethod=clip\");", 20, y+bh+10, "");
            
            
            /* ---------------------------------------------------
             * 比例したサイズ変更で画像をボックスにフィットさせる
             * ---------------------------------------------------
             */
            
            /* "boxsize" オプションは、指定された幅と高さを持ち、左下隅が基準点にある
             * ボックスを定義する。
             * "position={center}"は、画像を中央に配置する。
             * "fitmethod=meet" は、高さまたは幅がボックスに完全に収まるまで、画像の
             * サイズを比例的に変更する。
             * "showborder" オプションは、ボックスの境界を示すために使用される。 
             */
            p.fit_image(image, x, y-=yoffset, "boxsize={" + bw + " " + bh + "} " +
                "position={center} fitmethod=meet showborder");
            
            /* 説明テキストを出力する */
            p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " +
                "position={center} fitmethod=meet showborder\");", 20, y+bh+10, "");
         
            
            /* ---------------------------------
             * 画像を枠内に完全にフィットさせる
             * ---------------------------------
             */
               
            /* "boxsize" オプションは、指定された幅と高さを持ち、左下隅が基準点にある
             * ボックスを定義する。
             * デフォルトでは、画像はボックスの左下隅に配置される。
             */
            p.fit_image(image, x, y-=yoffset, "boxsize={" + bw + " " + bh + "} " +
                "fitmethod=entire");
            
            /* 説明テキストを出力する */
            p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " +
                "fitmethod=entire\");", 20, y+bh+10, "");
     
            p.end_page_ext("");
            
            p.close_image(image);

            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 - Aug 30, 2022)