iText は PDF ファイル等を生成可能な Java のライブラリ。

iText, a Free Java-PDF Library からダウンロードした JAR ファイル(itext-2.0.4.jar) をクラスパスに通すだけで使える。最新は 2.0.4 (itext-2.0.4.jar) らしい。

サンプルソースコード (PdfSample.java)


import java.awt.*;
import java.io.*;
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
 
public class PdfSample {
 
  public static void main(String[] args) throws Exception {
    byte[] pdfdata = getPdfData();
    OutputStream os =
      new BufferedOutputStream(
        new FileOutputStream("20070802itext.pdf"));
    os.write(pdfdata);
    os.flush();
  }
 
  public static byte[] getPdfData() throws Exception {
 
    // Rectangle: 595.0x842.0 (rot: 0 degrees)
    // って出力された。
    System.out.println(PageSize.A4);
 
    // DPI(どっと・ぱー・いんち)
    float dpi = 72.0f;
 
    // 左端のマージン: 10ミリメートル
    // 右端のマージン: 20ミリメートル
    // 上端のマージン: 30ミリメートル
    // 下端のマージン: 40ミリメートル
    float left = mm2pixel(10f, dpi);
    float right = mm2pixel(20f, dpi);
    float top = mm2pixel(30f, dpi);
    float bottom = mm2pixel(40f, dpi);
 
    // 描画範囲のサイズ
    float width = PageSize.A4.getWidth() - (left + right);
    float height = PageSize.A4.getHeight() - (top + bottom);
 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
    // creation of the document with a certain size and certain margins
    // PdfContentByte#createGraphics を使うのなら、マージン指定は意味がないかも。
    Document document = new Document(PageSize.A4, left, right, top, bottom);
 
    // creation of the writer 
    PdfWriter writer = PdfWriter.getInstance(document, baos);
 
    // we add some meta information to the document
    // PDF文書情報を設定
    document.addAuthor("おーさー");
    document.addTitle("たいとる");
    document.addSubject("さぶじぇくと");
    document.addKeywords("きーわーど");
    document.addCreator("くりえーたー");
 
    // we open the document for writing
    // PDF内容記述のためにオープン
    document.open();
 
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
 
    // PDFは左下が原点
    // 左端と下端を空けるために
    // PdfContentByte#concatCTM で平行移動のアフィン変換
    // java.awt.geom.AffineTransform のコンストラクタと同じ引数の順番
    // m00, m10, m01, m11, m02, m12 = a, b, c, d, e, f
    cb.concatCTM(1, 0, 0, 1, left, bottom);
 
    // 描画対象範囲を Graphics2D オブジェクトにて描画
    // 範囲外には描画されない
    Graphics2D g = cb.createGraphics(width, height);
 
    // いろいろと描画
    g.setColor(Color.BLUE);
    g.drawLine(0, 0, 100, 200);
    g.drawRect(0, 0, 100, 200);
    g.drawString("A(0,0)", 0, 0);
    g.drawString("B(10,10)", 10, 10);
    g.drawString("C(20,20)", 20, 20);
    g.drawString("D(30,30)", 30, 30);
    g.drawString("E(100,200)", 100, 200);
    g.setColor(Color.RED);
    g.drawLine(-100, -200, 200, 400);
    g.drawRect(-100, -200, 200, 400);
    g.setColor(Color.PINK);
    g.drawLine(0, 0, (int)width, (int)height);
    g.drawRect(0, 0, (int)width, (int)height);
    g.drawString("F(" + (int)width + "," + (int)height + ")", (int)width, (int)height);
    g.dispose();
 
    cb.restoreState();
    document.close();
 
    return baos.toByteArray();
  }
 
  /**
   * 印刷上の長さ(ミリメートル)を Graphics2D 上の長さ(ピクセル)へ変換します。
   * @param mm 印刷上の長さ(単位: ミリメートル)
   * @param dpi DPI(dot per inch) 1インチ当りのドット数 (ピクセル数)
   * @return Graphics2D オブジェクト上での長さ (単位: ピクセル)
   *
   * 参考:
   * - インチ - Wikipedia
   *   http://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%81
   * 
   */
  private static float mm2pixel(float mm, float dpi){
    // 国際インチ(international inch): 1 インチ = 25.4 ミリメートル
    return mm * dpi / 25.4f;
  }
}

出力したPDFファイル → 20070802itext.pdf

tags: zlashdot Java Java PDF iText

Posted by NI-Lab. (@nilab)