PDFlib

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

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

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

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

カード上のイメージ

PDFlib で、インポートされた PDF カードにイメージを配置するサンプルプログラムです。

インポートされた PDF のページの中央に 80% のサイズでイメージを配置します。イメージの高さは、イメージの縦横比を保ったままのサイズに調整されます。

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


/*
 * Center image on card:
 * Place an image on an imported PDF card
 *
 * The image will be placed in the center on the imported PDF page and scaled
 * to 80% of the page size. The height of the image will be scaled accordingly
 * so that the picture will stay in ratio.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF document, image file
 */
package com.pdflib.cookbook.pdflib.images;

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

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

        pdflib p = null;
        String imagefile = "nesrin.jpg";
        String cardfile = "card.pdf";
        int image, card, cardpage;
        double cardwidth, cardheight;
        int exitcode = 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);

            /* Load the image */
            image = p.load_image("auto", imagefile, "");

            if (image == -1)
            throw new Exception("Error: " + p.get_errmsg());

            /* Load the card file */
            card = p.open_pdi_document(cardfile, "");
            if (card == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Load the first page of the card file */
            cardpage = p.open_pdi_page(card, 1, "");
            if (cardpage == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Query the width and height of the first page of the card */
            cardwidth = p.pcos_get_number(card, "pages[0]/width");
            cardheight = p.pcos_get_number(card, "pages[0]/height");

            /* Create a page with the width and height of the card */
            p.begin_page_ext(0, 0, "width=" + cardwidth + " height=" + cardheight);

            /* Place the card page */
            p.fit_pdi_page(cardpage, 0, 0, "");

            /* Place the image in the center of a box which covers 80% of the card
             * size and starts from 10 percent of the card width and height. Fit
             * the image proportionally into the box so that it will cover 80% of
             * the card size as well.
             */
            p.fit_image(image, cardwidth * 0.1, cardheight * 0.1,
                    "boxsize={" + cardwidth * 0.8 + " " + cardheight * 0.8 +
                    "} position=center fitmethod=meet");

            p.end_page_ext("");
            p.close_image(image);
            p.close_pdi_page(cardpage);
            p.close_pdi_document(card);
            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)