PDFlib

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

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

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

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

複数ページ TIFF

PDFlib で、入力された TIFF イメージを一つ以上の PDF フレームに変換するサンプルプログラムです。

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


/*
 * Multi-page TIFF to PDF converter
 * 
 * Convert an input TIFF image containing one or more frames to PDF.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: multi-page TIFF image
 */
package com.pdflib.cookbook.pdflib.images;

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

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

        pdflib p = null;
        int exitcode = 0;
        String imagefile = "multi_page.tif";
        int font, image, frame;
        int x = 50, y = 700; 
       
        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);
            
            font = p.load_font("Helvetica-Bold", "unicode", "");
            if (font == -1)
                    throw new Exception ("Error: " + p.get_errmsg());
            
            /* Loop over all frames of the multi-page TIFF image */
            for (frame = 1; /* */ ; frame++)
            {
                    /* Load the next frame of the image */
                image = p.load_image("tiff", imagefile, "page=" + frame);
                if (image == -1)
                {
                    if (frame == 1)
                        /* Image not found */
                        throw new Exception("Error: " + p.get_errmsg());
                    else
                        /* no more frames available */
                        break;
                }
                
                /* Start page and place the frame on the page,
                 * placed at the bottom center.
                 */
                p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
                p.fit_image(image, 0.0, 0.0, "adjustpage");
                p.close_image(image);
                
                /* Output the number of the frame */
                p.fit_textline("Frame " + frame + " of the TIFF image", x, y,
                    "font= " + font + " fontsize=12");
                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 - Oct 21, 2022)