// http://api.itextpdf.com/itext/index.html?com/itextpdf/text/pdf/ColumnText.html
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.*;
import java.io.FileOutputStream;
public class Main {
// *****************************************************
// エントリポイント
// *****************************************************
public static void main(String[] args) {
Main thisClass = new Main();
}
// *****************************************************
// コンストラクタ
// *****************************************************
public Main() {
super();
my_acton();
}
// *****************************************************
// 初期処理
// *****************************************************
private void my_acton() {
System.out.println("処理開始");
String pdf = "pdftest.pdf";
Document doc = new Document();
try {
// 出力先を指定し、文書をPDFとして出力
PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream(pdf));
// 出力開始
doc.open();
// 日本語フォントの設定
// メイリオ
Font font = new Font(
BaseFont.createFont(
"C:\\WINDOWS\\Fonts\\meiryo.ttc,0",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED));
// メイリオボールド
Font font2 = new Font(
BaseFont.createFont(
"C:\\WINDOWS\\Fonts\\meiryob.ttc,0",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED));
// *****************************************************************
// 座標指定する為のオブジェクト
// *****************************************************************
// 一番上のレイヤー
PdfContentByte pcb = pw.getDirectContent();
// 一番上のレイヤーに直線を引く
pcb.moveTo( 0, 820 );
pcb.lineTo( 595, 820 );
pcb.stroke();
ColumnText ct = new ColumnText(pcb);
Phrase myText = null;
String line_text = null;
int x1 = 400;
int x2 = 595;
line_text = "1234567890123456789012345678901234567890";
myText = new Phrase(line_text,font);
ct.setSimpleColumn(
myText,
x1,0, // 書き込み対象の左下の座標
x2, 810, // 書き込み対象の右上の座標
20, // 行間
Element.ALIGN_LEFT
);
ct.go();
line_text = "ニ行目";
myText = new Phrase(line_text,font);
ct.setSimpleColumn(
myText,
x1,0,
x2, 750,
0,
Element.ALIGN_LEFT
);
ct.go();
line_text = "三行目";
myText = new Phrase(line_text,font);
ct.setSimpleColumn(
myText,
x1,0,
x2, 735,
0,
Element.ALIGN_LEFT
);
ct.go();
line_text = "四行目";
// *****************************************************************
// 右寄せ
// *****************************************************************
int right_offset = 50;
myText = new Phrase(line_text,font);
ct.setSimpleColumn(
myText,
x1,0,
x2-right_offset, 720,
0,
Element.ALIGN_LEFT
);
ct.go();
int top = 700;
myText = new Phrase("left:"+doc.getPageSize().getLeft(),font2);
ct.setSimpleColumn(
myText,
x1, 0,
x2-right_offset, top,
0,
Element.ALIGN_RIGHT
);
ct.go();
top -= 10;
myText = new Phrase("right:"+doc.getPageSize().getRight(),font2);
ct.setSimpleColumn(
myText,
x1, 0,
x2-right_offset, top,
0,
Element.ALIGN_RIGHT
);
ct.go();
top -= 10;
myText = new Phrase("top:"+doc.getPageSize().getTop(),font2);
ct.setSimpleColumn(
myText,
x1,0,
x2-right_offset, top,
0,
Element.ALIGN_RIGHT
);
ct.go();
top -= 10;
myText = new Phrase("bottom:"+doc.getPageSize().getBottom(),font2);
ct.setSimpleColumn(
myText,
x1,0,
x2-right_offset, top,
0,
Element.ALIGN_RIGHT
);
ct.go();
// *****************************************************************
// 真ん中のレイヤー( 通常の文字列や画像・グラフィックの追加 )
// *****************************************************************
Image img = Image.getInstance("_img.jpg");
img.setAbsolutePosition(140, 620);
img.scalePercent(50);
doc.add(img);
doc.add( new Paragraph(" ", font) ); // 改行
doc.add( new Paragraph(" ", font) );
doc.add( new Paragraph(" ", font) );
doc.add( new Paragraph(" ", font) );
doc.add( new Paragraph(" ", font) );
doc.add( new Paragraph("文を 一行出力", font) );
doc.add( new Paragraph("文を 一行出力", font) );
doc.add( new Paragraph("文を 一行出力", font) );
doc.add( new Paragraph("文を 一行出力", font) );
doc.add( new Paragraph("文を 一行出力", font) );
doc.add( new Paragraph(" ", font) );
Paragraph p = new Paragraph();
for (int i = 0; i < 25; i++) {
p.add(
new Chunk(
"日本語表示の一般テキストを繰り返し表示しています。"
,font
)
);
}
doc.add(p);
// 出力終了
doc.close();
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("処理終了");
}
}