iTextを使ったJavaによるPDFの作成

今回はTomcatを使わずにやった.
思ったよりずっと簡単にできた.


こんな感じのソースを作成

import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;

public class SamplePdf
{
    public static void main( String[] args )
    {
        Document doc = new Document();
        String pdf_filename  = "sample.pdf";
        String html_filename = "sample.html";

        try{
            // PDF文章作成の準備
            PdfWriter.getInstance( doc, new FileOutputStream( pdf_filename ) );
            // HTML文章作成の準備
            //            HtmlWriter.getInstance( doc, new FileOutputStream( html_filename ) );

            System.out.println( "簡単なPDFファイルの出力のテスト" );

            doc.open();

            // falseはフォントの埋め込みなしということ.
            BaseFont base = BaseFont.createFont( "HeiseiMin-W3", "UniJIS-UCS2-HW-H", false );

            // フォントの作成
            Font font12  = new Font( base, 12 );
            Font font_12 = new Font( base, 12, Font.UNDERLINE );
            Font font16  = new Font( base, 16 );
            Font font_16 = new Font( base, 16, Font.UNDERLINE );

            // 文章の作成
            Paragraph para1 = new Paragraph( "タイトル(下線つき)", font_16 );
            para1.setAlignment( Element.ALIGN_CENTER );
            doc.add( para1 );

            Paragraph para2 = new Paragraph( "左寄せ(下線つき)", font_12 );
            para2.setAlignment( Element.ALIGN_LEFT );
            doc.add( para2 );

            Paragraph para3 = new Paragraph( "右寄せ", font12 );
            para3.setAlignment( Element.ALIGN_RIGHT );
            doc.add( para3 );

            // 表の作成
            Table table = new Table(4);
            table.setWidth( 100 );

            // 各列の幅をパーセンテージで指定
            int tableWidth[] = { 25, 25, 25, 25 };

            // 横の表示位置
            table.setDefaultHorizontalAlignment( Element.ALIGN_CENTER );
            // 縦の表示位置
            table.setDefaultVerticalAlignment( Element.ALIGN_MIDDLE );
            // 表の余白を指定
            table.setPadding(3);
            // 表のセル間の感覚を指定
            table.setSpacing(0);
            // 表の線の色を指定
            table.setBorderColor( new Color( 0, 0, 0 ) );

            Cell cell11 = new Cell( new Phrase( "1-1個目", font12) );
            Cell cell12 = new Cell( new Phrase( "1-2個目", font12) );
            Cell cell13 = new Cell( new Phrase( "1-3個目", font12) );
            Cell cell14 = new Cell( new Phrase( "1-4個目", font12) );
            Cell cell21 = new Cell( new Phrase( "2-1個目", font12) );
            Cell cell22 = new Cell( new Phrase( "2-2個目", font12) );
            Cell cell23 = new Cell( new Phrase( "2-3個目", font12) );
            Cell cell24 = new Cell( new Phrase( "2-4個目", font12) );

            table.addCell( cell11 );
            table.addCell( cell12 );
            table.addCell( cell13 );
            table.addCell( cell14 );
            table.addCell( cell21 );
            table.addCell( cell22 );
            table.addCell( cell23 );
            table.addCell( cell24 );

            doc.add( table );

        } catch ( DocumentException e ){
            System.out.println( e.getMessage() );
        } catch ( IOException e ){
            System.out.println( e.getMessage() );
        } catch ( Exception e ){
            System.out.println( e.getMessage() );
        } finally {
            doc.close();
        }
    }
}

これで本当に簡単なPDFファイルを吐いてくれる.
また, HTMLも吐けるとのことなので, それらしいの加えてみたら問題なくできた.
このソースではコメントアウトしている部分(HtmlWriter...)を外してコンパイルするとPDFに加え, HTMLも吐いてくれる.
見た目はかなりよく似ているのでこれはイイ.


あと調べることは, 1行に左寄せした文章と右寄せした文章, 両方を書くにはどうしたらいいかってことかなあ.
自分はこうゆう書き方, よくするんだよなあ.
ヘッダ, フッタとかでも左右両方に書けた方が嬉しいだろうし.