PDFlib

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

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

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

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

イメージ上のテキストの配置

PDFlib で、イメージ上にテキストを配置するサンプルプログラムです。

イメージマッチボックスによって抽出されるイメージの左向きの右下にテキストを配置します。左向きのイメージに左向きの右下にテキストを配置します。


/* $Id: align_text_at_image.java,v 1.3 2013/01/15 10:11:59 stm Exp $
 * Align text at image:
 * Align text at an image
 *
 * Align text orientated to the west at the lower right corner of an image by
 * retrieving the coordinates of the image matchbox.
 * Align text orientated to the west at the lower right corner of an image
 * orientated to the west.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

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

    pdflib p = null;
    String imagefile = "kraxi_logo.tif";
    String optlist;
    int font, image;
    double x1 = 0, x2 = 0, y1 = 0, y2 = 0;

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

        /* Start page */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
        
        font = p.load_font("Helvetica", "unicode", "");
        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

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

        /* -----------------------------------------------------------------
         * Align text orientated to the west at the lower right corner of an
         * image
         * -----------------------------------------------------------------
         */
        
        /* Place the image in the center of a box using the "boxsize" and 
         * "position" options. Maintain its proportions using "fitmethod=meet".
         * Use the "matchbox" option with the "borderwidth" suboption to draw a
         * small rectangle around the image with the "strokecolor" suboption
         * determining the border color. 
         */
        
        /* Fit the image */
        optlist = "boxsize={300 200} position={center} " +
            "fitmethod=meet matchbox={name=giantwing borderwidth=3 " +
            "strokecolor={rgb 0.85 0.83 0.85}}";
        
        p.fit_image(image, 100, 500, optlist);
        
        /* Retrieve the coordinates of the second (lower right) matchbox corner.
         * The parameter "1" indicates the first instance of the "giantwing"
         * matchbox.
         */
        if ((int) p.info_matchbox("giantwing", 1, "exists") == 1) {
        	x2 = p.info_matchbox("giantwing", 1, "x2");
            y2 = p.info_matchbox("giantwing", 1, "y2");
        }
        
        /* Start the text line orientated to the west at the corner coordinates
         * retrieved (x2, y2) with a small offset of 3 or 2, respectively.
         */
        optlist = "font=" + font + " fontsize=12 orientate=west";
        
        p.fit_textline("Foto: Kraxi", x2+3, y2+2, optlist);
        
        
        /* -----------------------------------------------------------------
         * Align text orientated to the west at the lower right corner of an
         * image orientated to the west as well.
         * -----------------------------------------------------------------
         */
        
        /* Place the image in the center of a box using the "boxsize" and 
         * "position" options. Maintain its proportions using "fitmethod=meet".
         * Using the "orientate" option orientate the image to the west. Use the
         * "matchbox" option with the "borderwidth" suboption to draw a small
         * rectangle around the image with the "strokecolor" suboption
         * determining the border color. 
         */
        optlist = "boxsize={200 300} position={center} fitmethod=meet " +
            "orientate=west matchbox={name=giantwing borderwidth=3 " +
            "strokecolor={rgb 0.85 0.83 0.85}}";
        
        p.fit_image(image, 100, 100, optlist);
        
        /* Retrieve the coordinates of the first matchbox corner; usually this
         * will be the lower left corner but with being orientated to the west
         * it will be moved to the bottom right. The parameter "2" indicates the
         * second instance of the "giantwing" matchbox.
         */
        if ((int) p.info_matchbox("giantwing", 2, "exists") == 1) {
        	x1 = p.info_matchbox("giantwing", 2, "x1");
            y1 = p.info_matchbox("giantwing", 2, "y1");
        }
        
        /* Start the text line orientated to the west at the corner coordinates
         * retrieved (x1, y1) with a small offset of 3 or 2, respectively.
         */
        optlist = "font=" + font + " fontsize=12 orientate=west";
        
        p.fit_textline("Foto: Kraxi", x1+3, y1+2, optlist);
        
        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)