PDFlib

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

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

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

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

目次の追加

PDFlib で、複数のページを作成し、それぞれにラベルを割り当て、目次を文書の最初のページに挿入するサンプルプログラムです。

ページは文書で表示される順序とは異なる順序で作られます。ページグループはいくつかのページを作成し、ページラベルを提供します。最後のページを作成した後で、文書の最初に戻って、目次のためのページを挿入します。


/* $Id: insert_toc.java,v 1.12 2013/01/15 10:12:01 stm Exp $
 * Insert table of contents:
 * Create some pages, assign labels to them and insert a table of contents at
 * the beginning of the document
 *
 * Pages are created in a different chronological order than the order in
 * which they appear in the document. Using page groups create several pages
 * and provide them with page labels. After creating the last page, go back
 * to the start of the document and insert the pages for the table of contents.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class insert_toc
{
    public static void main (String argv[])
    {
    /* This is where the data files are. Adjust as necessary. */
    String searchpath = "../input";
    String outfile = "insert_toc.pdf";
    String title = "Insert Table of Contents";

    pdflib p = null;
    int pageno, font, docsize = 0;

    /* Using the "groups" option, define several page groups for the
     * title (title), the table of contents (toc), the individual chapters
     * (body), and the index (index). List them in the order they will appear
     * in the document. Using the "labels" option, assign a page label to each
     * page group.
     */
    String optlist =
        "groups={title toc content index} " +
        "labels={{group=title prefix=title} " +
                "{group=toc prefix={toc } start=1 style=r} " +
                "{group=content start=1 style=D} " +
                "{group=index prefix={index } start=1 style=r}}";

    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, optlist) == -1)
            throw new Exception("Error: " + p.get_errmsg());

        p.set_info("Creator", "PDFlib Cookbook");
        p.set_info("Title", title + " $Revision: 1.12 $");

        font = p.load_font("Helvetica-Bold", "unicode", "");
        if (font == -1)
            throw new Exception("Error: " + p.get_errmsg());

        /* Create a page in the page group "title" */
        p.begin_page_ext(595, 842, "group title");
        docsize++;
        p.fit_textline("Title", 50, 700, "font=" + font + " fontsize=36");
        p.end_page_ext("");

        /* Loop over all pages in the page group "content" */
        for (pageno = 1; pageno <= 5; pageno++)
        {
            p.begin_page_ext(595, 842, "group content");
            docsize++;
            p.fit_textline("Chapter " + pageno, 50, 700, "font=" + font +
                " fontsize=36");
            p.end_page_ext("");
        }

        /* Insert two pages in the page group "index" */
        p.begin_page_ext(595, 842, "group index");
        docsize++;
        p.fit_textline("Index I", 50, 700, "font=" + font + " fontsize=36");
        p.end_page_ext("");

        p.begin_page_ext(595, 842, "group index");
        docsize++;
        p.fit_textline("Index II", 50, 700, "font=" + font + " fontsize=36");
        p.end_page_ext("");

        /* Insert two pages in the page group "toc" for a table of contents */
        p.begin_page_ext(595, 842, "group toc");
        docsize++;
        p.fit_textline("Table of Contents I", 50, 700, "font=" + font +
            " fontsize=36");
        p.end_page_ext("");

        p.begin_page_ext(595, 842, "group toc");
        docsize++;
        p.fit_textline("Table of Contents II", 50, 700, "font=" + font +
            " fontsize=36");
        p.fit_textline("for a Document of " + docsize + " Pages.", 50, 600,
            "font=" + font + " fontsize=24");
        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)