PDFlib

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

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

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

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

トップダウン座標系

トップダウン座標系を使用して、テキストを出力するためのプログラムです。

PDFlibのデフォルトの座標は、ページの左下隅に原点がありますが、これを左上隅を原点とし、x座標は右方向に、y座標は下方向に増加するように設定します。

また、座標単位をポイントからセンチメール単位で使用できるよう設定します。

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

 
/*
 * トップダウン座標系:
 * トップダウン座標系を使用してテキストを出力します。
 * 
 * 左下の代わりに左上を起点とする座標系を使用するよう指定します。また、1cm を一行
 * とするよう配置します。
 * 
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 10
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.general;

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

public class metric_topdown_coordinates
{
    public static void main (String argv[])
    {
        /* 必要に応じてデータファイルがあるフォルダのパスを指定する */
        String searchpath = "../input";
        String outfile = "metric_topdown_coordinates.pdf";
        String title = "Metric Topdown Coordinates";

        pdflib p = null;
        int font;
        int exitcode = 0;

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            /* load_font()の戻り値を確認する */
            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("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* 出力PDFページをA4サイズで作成し、トップダウン座標系を使用する */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height topdown");
            
            /* PDFlib の座標系はDTPポイントを使用している。ポイントとmmの関係は
             * 1 pt = 1/72 inch = 25.4/72 mm = 0.3528 mm. 座標単位をポイントか
             * らセンチメールに変換すると、72/2.54 = 28.3465pt/cm 
             * この値を p.scale()に設定する。
             */
            p.scale(28.3465, 28.3465);
            
            /* これにより、PDFlibは座標はセンチメートル単位で設定される(ただし
             * 注釈などのインタラクティブ要素は除く)。またこれらは現在のページに
             * 対して有効であり、必要に応じて各ページで繰り返す必要がある。
             */
            
            /* 注釈作成等に使用する座標は、デフォルト座標で認識されるため、上記のユーザー
             * 座標が用いられていることを認識させるようtrueを設定する
             */
            p.set_option("usercoordinates=true");
            
            /* 1cm間隔で境界線を引く。3ライン目は1cm四方の正方形を描き、塗りつぶす */
            p.setlinewidth(0.01);
         
            for (int i = 1; i < 29; i++) {
                p.moveto(0, i);
                p.lineto(1, i);
                if (i == 3) {
                    p.rect(0, i, 1, 1);
                }
                    p.fill_stroke();
            }
            
            /* フォントサイズを設定(cm単位) */
            p.setfont(font, 0.5);
            
            p.fit_textline("Centimeters from the top left instead of points from " +
                "the bottom left:", 2, 5, "");
            p.fit_textline("The small lines on the left are placed 1 cm from " +
                "each other.", 2, 6, "");
            p.fit_textline("This text line is displayed at 7 cm from top and 2 " +
                "cm from the left.", 2, 7, "");
            
            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 - Feb 21, 2024)