Java プログラムメモ


トップ

iText で PDF の出力

PDF を出力するのに iText を使用します。 本体のクラスである iText.jar と日本語フォント用の iTextAsian.jar をダウンロードしてクラスパスを通します。

ソース記述例

import java.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class PDF { private Font font_; private Table table_; public void createFile(String[] header, String[] data) throws IOException { Document doc = new Document(PageSize.A4.rotate(), (float)5, (float)5, (float)5, (float)5); try { PdfWriter docWriter = PdfWriter.getInstance(doc, new FileOutputStream("ファイル名")); BaseFont mincyouBase = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-H", false); font_ = new Font(mincyouBase, 11); table_ = new Table(header.length); table_.setBorderWidth(1); table_.setBorder(0); table_.setPadding(1); table_.setWidth(100); doc.open(); writeHeader(header); write(data); doc.add(table_); doc.close(); } catch (DocumentException e) { throw new IOException(e.getMessage()); } } void writeHeader(String[] header) throws IOException { write(header); table_.endHeaders(); } void write(String[] data) throws IOException { try { for (int i = 0; i < data.length; i++) { Cell cell = new Cell(new Phrase(data[i], font_)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table_.addCell(cell); } } catch (BadElementException e) { throw new IOException(e.getMessage()); } } public static void main(String[] args) { String[] header = new String[]{"年月日", "データ"}; String[] data = new String[]{"20040101", "500円"}; try { new PDF().createFile(header, data); } catch (IOException e) { e.printStackTrace(); } } }

モデリングツール

直接 iText を使用して帳票を作成するのは、かなり手間がかかります。 そこで、iText 専用のモデリングツールがあるので紹介します。
かなりシンプルで使い勝手が良いので一度試してください。
OpenFunXion for iText


プログラムメモ アフリカ雑貨・珈琲豆・アールブリュット作品 マゴソスクール クラウドファンディング
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送