iTextでPDF出力を試す

戻る

出力結果PDF

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
* $Id: iTextTest.html,v 1.1 2009/06/22 16:12:14 kishi Exp kishi $
* @author KISHI Yasuhiro
* 
* <pre>
* To complie:
*     javac -classpath iTextAsian.jar\;itext-1.3.jar *.java && astyle -a -j -P *.java  && vu.sh *.java  && rm -f *.orig *.bak
* To execute:
*     java -cp .\;iTextAsian.jar\;itext-1.3.jar iTextTest
* </pre>
*/

public class iTextTest {

    /** 出力ファイル名 */
    private static final String outputFileName = "iTextTest.pdf";

    /** 明細用データ */
    private static String[][] data =
        { { "XYZ-01", "プサイズドライヤー", "2", "個", "300", "600" }, {
              "XYZ-02", "プサイズシェイバー", "1", "個", "300", "300" }, {
              "ABC-01", "プサイズフォン(ミッションメモリー付)", "2", "個", "200", "400" }, {
              "ABC-02", "プサイズハンカチ", "1", "枚", "200", "200" }, {
              "ABC-03", "プサイズ歯ブラシ", "1", "本", "200", "200" }, {
              "ABC-04", "スマッシュパッド", "1", "個", "200", "200" }
        };

    public static void main( String[] args ) {
        try {
            // 明朝体,ゴシック体フォントを設定
            BaseFont mincho =
                BaseFont.createFont( "HeiseiMin-W3", "UniJIS-UCS2-HW-H", false );
            BaseFont gothic =
                BaseFont.createFont( "HeiseiKakuGo-W5", "UniJIS-UCS2-H", false );
            Font min10 = new Font( mincho, 10 );
            Font goth10 = new Font( gothic, 10 );

            // Documentオブジェクトを生成
            Document document = new Document( PageSize.A4 );
            // PdfWriterオブジェクトを生成
            PdfWriter writer =
                PdfWriter.getInstance(
                    document,
                    new FileOutputStream( outputFileName ) );

            // ヘッダー/フッターを設定
            String YMD = "2004/04/30";
            HeaderFooter header =
                new HeaderFooter(
                    new Phrase(
                        new Chunk(
                            YMD,
                            new Font( mincho, 10, Font.UNDERLINE ) ) ),
                    false );
            header.setAlignment( Element.ALIGN_RIGHT );
            header.setBorderColor( Color.white );
            document.setHeader( header );

            HeaderFooter footer =
                new HeaderFooter(
                    new Paragraph( "株式会社かとちゃん", new Font( mincho, 12 ) ),
                    false );
            footer.setAlignment( Element.ALIGN_CENTER );
            document.setFooter( footer );

            // ドキュメントをオープン
            document.open();

            // 文を挿入
            Paragraph para1 =
                new Paragraph( "請 求 書", new Font( gothic, 20, Font.BOLD ) );
            para1.setAlignment( Element.ALIGN_CENTER );
            document.add( para1 );
            Paragraph para2 =
                new Paragraph( 50, "中曽根 信介 様", new Font( mincho, 15, Font.UNDERLINE ) );
            para2.setAlignment( Element.ALIGN_LEFT );
            document.add( para2 );
            Paragraph para3 =
                new Paragraph( 40, "下記の通り請求いたします。", new Font( mincho, 12 ) );
            para3.setAlignment( Element.ALIGN_CENTER );
            document.add( para3 );

            // 表を出力
            Table tbl_hd = new Table( 2 );
            tbl_hd.setDefaultHorizontalAlignment( Element.ALIGN_CENTER );
            tbl_hd.setDefaultVerticalAlignment( Element.ALIGN_MIDDLE );
            tbl_hd.setPadding( 4 );

            int widthA[] = { 35, 65 };
            tbl_hd.setWidths( widthA ); // 各セル幅の比率を設定(パーセント)
            tbl_hd.setWidth( 50 ); // テーブル全体の大きさを設定(パーセント)

            Cell cellA1 = new Cell( new Phrase( "件名", new Font( gothic, 15 ) ) );
            cellA1.setGrayFill( 0.9f );
            Cell cellA2 = new Cell( new Phrase( "合計金額", new Font( gothic, 15 ) ) );
            cellA2.setGrayFill( 0.9f );
            Cell cellA3 = new Cell( new Phrase( "1,995円", new Font( mincho, 15 ) ) );
            cellA3.setHorizontalAlignment( Element.ALIGN_RIGHT );

            tbl_hd.addCell( cellA1 );
            tbl_hd.addCell( new Phrase( "プサイズグッズ代", new Font( mincho, 15 ) ) );
            tbl_hd.addCell( cellA2 );
            tbl_hd.addCell( cellA3 );

            document.add( tbl_hd );

            Paragraph para4 = new Paragraph( "■明細書■\n", new Font( gothic, 13 ) );
            para4.setAlignment( Element.ALIGN_CENTER );
            document.add( para4 );

            Table tbl_dt = new Table( 6 );
            tbl_dt.setDefaultHorizontalAlignment( Element.ALIGN_CENTER );
            tbl_dt.setDefaultVerticalAlignment( Element.ALIGN_MIDDLE );
            tbl_dt.setPadding( 4 );
            tbl_dt.setOffset( 10 );

            int widthC[] = { 10, 40, 10, 10, 15, 15 };
            tbl_dt.setWidths( widthC );
            tbl_dt.setWidth( 100 );

            Cell cellC1 = new Cell( new Phrase( "コード", goth10 ) );
            cellC1.setGrayFill( 0.9f );
            Cell cellC2 = new Cell( new Phrase( "品目・摘要", goth10 ) );
            cellC2.setGrayFill( 0.9f );
            Cell cellC3 = new Cell( new Phrase( "数量", goth10 ) );
            cellC3.setGrayFill( 0.9f );
            Cell cellC4 = new Cell( new Phrase( "単位", goth10 ) );
            cellC4.setGrayFill( 0.9f );
            Cell cellC5 = new Cell( new Phrase( "単価", goth10 ) );
            cellC5.setGrayFill( 0.9f );
            Cell cellC6 = new Cell( new Phrase( "金額", goth10 ) );
            cellC6.setGrayFill( 0.9f );

            tbl_dt.addCell( cellC1 );
            tbl_dt.addCell( cellC2 );
            tbl_dt.addCell( cellC3 );
            tbl_dt.addCell( cellC4 );
            tbl_dt.addCell( cellC5 );
            tbl_dt.addCell( cellC6 );

            // 明細行を埋める
            for ( int i = 0; i < data.length; i++ ) {
                Cell cell = new Cell( new Phrase( data[ i ][ 0 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_CENTER );
                tbl_dt.addCell( cell );
                cell = new Cell( new Phrase( data[ i ][ 1 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_LEFT );
                tbl_dt.addCell( cell );
                cell = new Cell( new Phrase( data[ i ][ 2 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
                tbl_dt.addCell( cell );
                cell = new Cell( new Phrase( data[ i ][ 3 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_CENTER );
                tbl_dt.addCell( cell );
                cell = new Cell( new Phrase( data[ i ][ 4 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
                tbl_dt.addCell( cell );
                cell = new Cell( new Phrase( data[ i ][ 5 ], min10 ) );
                cell.setHorizontalAlignment( Element.ALIGN_RIGHT );
                tbl_dt.addCell( cell );
            }

            //空白行を作成
            for ( int i = data.length; i < 10; i++ ) {
                for ( int j = 0; j < 6; j++ ) {
                    tbl_dt.addCell( "" );
                }
            }

            Cell cellC7 = new Cell( new Phrase( "小計", goth10 ) );
            cellC7.setGrayFill( 0.9f );
            cellC7.setColspan( 5 );
            Cell cellC8 = new Cell( new Phrase( "1,900", min10 ) );
            cellC8.setHorizontalAlignment( Element.ALIGN_RIGHT );
            Cell cellC9 = new Cell( new Phrase( "消費税等", goth10 ) );
            cellC9.setGrayFill( 0.9f );
            cellC9.setColspan( 5 );
            Cell cellC10 = new Cell( new Phrase( "95", min10 ) );
            cellC10.setHorizontalAlignment( Element.ALIGN_RIGHT );
            Cell cellC11 = new Cell( new Phrase( "合計", goth10 ) );
            cellC11.setGrayFill( 0.9f );
            cellC11.setColspan( 5 );
            Cell cellC12 = new Cell( new Phrase( "1,995", min10 ) );
            cellC12.setHorizontalAlignment( Element.ALIGN_RIGHT );

            // 備考
            Cell cellC13 = new Cell( new Phrase( "備考", goth10 ) );
            cellC13.setGrayFill( 0.9f );
            cellC13.setRowspan( 3 );
            cellC13.setVerticalAlignment( Element.ALIGN_TOP );
            Cell cellC14 = new Cell( new Phrase( "/*あいうえおかきくけこさしすせそたちつてと*/", goth10 ) );
            cellC14.setRowspan( 3 );
            cellC14.setColspan( 5 );
            cellC14.setHorizontalAlignment( Element.ALIGN_LEFT );

            tbl_dt.addCell( cellC7 );
            tbl_dt.addCell( cellC8 );
            tbl_dt.addCell( cellC9 );
            tbl_dt.addCell( cellC10 );
            tbl_dt.addCell( cellC11 );
            tbl_dt.addCell( cellC12 );
            tbl_dt.addCell( cellC13 );
            tbl_dt.addCell( cellC14 );

            document.add( tbl_dt );

            // ドキュメントをクローズ
            document.close();
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
}


戻る inserted by FC2 system