Java バッチビルド : iText(PDF) で、MSGOTHIC.TTC を使って4つのレイヤーを使うサンプル

  ダウンロード



2009/10/05


ブラウザでダウンロード

Javaitext


フォントはフルパス指定でフリーフォントでも表示できます。

4つのレイヤーがあって、high level のレイヤー以外は位置指定ができますし、
high level でも、画像は位置指定ができます。
その表示状態を確認する為ののサンプルコードです。

フォントは以下のようになります。

※ 実際は、PC の環境変数から Windows ディレクトリを取得したほうが良いでしょう
( 実行時に取得するので、存在しないとエラーになります )

  
// MS GOTHIC
Font font = new Font(
	BaseFont.createFont(
	  "C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC,0",
	  BaseFont.IDENTITY_H,
	  BaseFont.EMBEDDED));

// MS P GOTHIC
Font font2 = new Font(
	BaseFont.createFont(
	  "C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC,1",
	  BaseFont.IDENTITY_H,
	  BaseFont.EMBEDDED));

  

2009/10/07
VB.net : Text(PDF) で、MSGOTHIC.TTC を使って
4つのレイヤーを使うサンプル (要 itextsharp.dll)
を作成しました

API JavaDoc

iText ®: where to find examples




  主となるソースコード



Java はインデントが多いので、処理部分のみです。
※ この上には try 〜 catch があります

テキストの位置指定には、ColumnText といクラスを使っています。
テキストを位置指定する為のレイヤーだと思えばいいと思います。
わざと重ねて書き込んでみましたが、特に問題は無かったです。
( 領域の指定はきちんとしないと、テキストが折り返されて、重なったりしますので大き目で重ねたほうが無難 )

  
// 出力先を指定し、文書をPDFとして出力
PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream(pdf));
// 出力開始
doc.open();

// 日本語フォントの設定

// MS GOTHIC
Font font = new Font(
	BaseFont.createFont(
	  "C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC,0",
	  BaseFont.IDENTITY_H,
	  BaseFont.EMBEDDED));

// MS P GOTHIC
Font font2 = new Font(
	BaseFont.createFont(
	  "C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC,1",
	  BaseFont.IDENTITY_H,
	  BaseFont.EMBEDDED));


// *****************************************************************
// 座標指定する為のオブジェクト
// *****************************************************************
// 一番上のレイヤー
PdfContentByte pcb = pw.getDirectContent();

// 一番上のレイヤーに直線を引く
pcb.moveTo( 250, 10 );
pcb.lineTo( 250, 800 );
pcb.stroke();

ColumnText ct = new ColumnText(pcb);

Phrase myText = null;

// MS GOTHIC
myText = new Phrase("ABCDEFGHIJKLMN",font);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, 820,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

myText = new Phrase("漢字表示",font);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, 810,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

// MS P GOTHIC
myText = new Phrase("ABCDEFGHIJKLMN",font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, 800,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

myText = new Phrase("漢字表示",font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, 790,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

int top = 750;
myText = new Phrase("left:"+doc.getPageSize().getLeft(),font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, top,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

top -= 10;
myText = new Phrase("right:"+doc.getPageSize().getRight(),font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, top,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

top -= 10;
myText = new Phrase("top:"+doc.getPageSize().getTop(),font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, top,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

top -= 10;
myText = new Phrase("bottom:"+doc.getPageSize().getBottom(),font2);
ct.setSimpleColumn(
	myText,
	100,0,		// ページの左下の座標
	300, top,		// ページの右上の座標
	0,
	Element.ALIGN_LEFT
);
ct.go();

// 真ん中のレイヤー( 画像レイヤーの上 )
Paragraph pg = new Paragraph("標準", font);
doc.add(pg);
doc.add(pg);
doc.add(pg);
doc.add(pg);
doc.add(pg);
// 空文字は無視されるので " "
pg = new Paragraph(" ");
doc.add(pg);
doc.add(pg);

Paragraph p = new Paragraph();
for (int i = 0; i < 25; i++) {
	p.add(
		new Chunk(
			"一般テキスト   一般テキスト   一般テキスト"
			,font
		)
	);
}
doc.add(p);

// 真ん中のレイヤー( 画像レイヤー )
Image img = Image.getInstance("sample.png");
img.setAbsolutePosition(120, 500);
doc.add(img);

// 一番上のレイヤー( 灰色の小さな円 )
pcb.setRGBColorFill(0xAA, 0xAA, 0xAA);
pcb.circle(250.0f, 500.0f, 50.0f);
pcb.fill();
pcb.sanityCheck();

// 一番下のレイヤー( 赤いおおきな円 )
PdfContentByte cbu = pw.getDirectContentUnder();
cbu.setRGBColorFill(0xFF, 0x00, 0x00);
cbu.circle(250.0f, 500.0f, 100.0f);
cbu.fill();
cbu.sanityCheck();

// 一番上のレイヤーに直線を引く
pcb.moveTo( 255, 10 );
pcb.lineTo( 255, 800 );
pcb.stroke();

// 出力終了
doc.close();

String command = 
	"RunDLL32.EXE shell32.dll,ShellExec_RunDLL \"" + 
	System.getProperty("user.dir") + "\\" + pdf + "\"";
Process process = Runtime.getRuntime().exec(command);
  

2009/10/06
Vista で、RunDLL32.EXE shell32.dll,ShellExec_RunDLL
に引渡すパスがフルパスでないと動きませんでした












  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ