PDFlib

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

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

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

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

イメージの切り抜き

PDFlib で、イメージが保有するクリッピングパス情報に基づき、イメージを切り抜いて表示するサンプルプログラムです。

クリッピングパスを含んだイメージを使います。クリッピングパスを使わずに配置し、クリッピングパスに従ってイメージを切り抜きます。


/*
 * Integrated clipping path:
 * Place an image with its integrated clipping path being applied to it
 * 
 * Use an image containing a clipping path. Place it without any clipping 
 * taken place. Then, place the image clipped according to its clipping path.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.images;

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

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

        pdflib p = null;
        String imagefile = "child_clipped.jpg";
        int font, image, imageclipped;
        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 with its integrated clipping path being ignored */
            image = p.load_image("auto", imagefile, "honorclippingpath=false");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Load the same image with its integrated clipping path being
             * considered (which is the default setting)
             */
            imageclipped = p.load_image("auto", imagefile, "");
            if (imageclipped == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Load font */
            font = p.load_font("Helvetica", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Start a page with the image dimensions */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Place the image without the clipping path being applied to it */
            p.fit_image(image, 100, 450, "boxsize {400 300} fitmethod=meet");
            p.fit_textline("Image without its integrated clipping path being " +
                "applied.", 100, 430, "font=" + font + " fontsize=14");

            /* Place the image with the clipping path being applied to it */
            p.fit_image(imageclipped, 100, 100, "boxsize {400 300} fitmethod=meet");
            p.fit_textline("Image with its integrated clipping path being applied.",
                100, 80, "font=" + font + " fontsize=14");
            
            /* Close the images */
            p.close_image(image);
            p.close_image(imageclipped);
         
            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);
        }
    }
}
(Apr 3, 2007 - May 23, 2019)