PDFlib

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

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

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

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

バレット付きリスト

PDFlib で、バレットまたは番号付きのリストを出力するためのサンプルプログラムです。

それぞれバレットおよび番号付きのリストを作成するために add_textflow 関数の leftindent オプションを使用します。leftindent=0 は、バレットのために、leftindent=22 は番号のために使用します。

 
/*
 * Bulleted list:
 * Output a bulleted list 
 * 
 * Use the "leftindent" option of add_textflow() to create a bulleted or 
 * numbered list, respectively. Use "leftindent=0" for adding the bullet or
 * number and "leftindent=22" for adding the list item.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

public class bulleted_list
{
    public static void main (String argv[])
    {
        pdflib p = null;
        int exitcode = 0;

        String outfile = "bulleted_list.pdf";
        String title = "Bulleted List";
        int tf = -1, i;
        final double llx=50, lly=200, urx=500, ury=700;
        String result;
        String num_optlist, item_optlist;
        
        final String head_optlist =
            "fontname=Helvetica-Bold fontsize=14 encoding=unicode " +
            "fillcolor={cmyk 1 0.5 0.2 0}";
        
        final String items [] = {
            "Long Distance Glider\nWith this paper rocket you can send all your " +
            "messages even when sitting in a hall or in the cinema pretty near " +
            "the back.",

            "Giant Wing\nAn unbelievable sailplane! It is amazingly robust and " +
            "can even do aerobatics. But it is best suited to gliding.",

            "Cone Head Rocket\nThis paper arrow can be thrown with big swing. " +
            "We launched it from the roof of a hotel. It stayed in the air a " +
            "long time and covered a considerable distance.",

            "Super Dart\nThe super dart can fly giant loops with a radius of 4 " +
            "or 5 meters and cover very long distances. Its heavy cone point is " +
            "slightly bowed upwards to get the lift required for loops."
        };
            
        try {
            p = new pdflib();

            /* 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);
            
            
            /* ----------------------
             * Create a bulleted list
             * ----------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Create the Textflow. First, add a heading */
            tf = p.add_textflow(tf, "Page 1: Bulleted list\n\n\n", head_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add list bullets and items. For the list bullets, use "leftindent=0"
             * and for the items, use "leftindent=22" to indent the item text.
             * For list bullets, use the "escapesequence" option to enable PDFlib to
             * resolve "\\xB7" as the hexadecimal code "0xB7" for the bullet in the
             * Symbol font. 
             */
            num_optlist =
                "fontname=Symbol fontsize=12 encoding=builtin escapesequence " +
                "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0";
            
            item_optlist =
                "fontname=Helvetica fontsize=12 encoding=unicode " +
                "fillcolor={gray 0} alignment=justify leading=140% leftindent=22";
            
            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, "\\xB7", num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
                
                tf = p.add_textflow(tf, items[i] + "\n\n", item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }
            
            /* Place the Textflow */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                /* Check for further action */
            }
            
            p.delete_textflow(tf);
            
            p.end_page_ext("");
            
             
            /* ----------------------
             * Create a numbered list
             * ----------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* Create the Textflow. First, add a heading */
            tf = -1;
            
            tf = p.add_textflow(tf, "Page 2: Numbered list\n\n\n", head_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add list numbers and list items. For the list numbers, use 
             * "leftindent=0" and for the items, use "leftindent=22" to indent the
             * item text.
             */
            num_optlist =
                "fontname=Helvetica-Bold fontsize=12 encoding=unicode " +
                "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0";
            
            item_optlist =
                "fontname=Helvetica fontsize=12 encoding=unicode " +
                "fillcolor={gray 0} alignment=justify leading=140% leftindent=22";
            
            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, Integer.toString(i + 1), 
                    num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
                
                tf = p.add_textflow(tf, items[i] + "\n\n", item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }
            
            /* Place the Textflow */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                    /* Check for further action */
            }
            
            p.delete_textflow(tf);
                    
            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)