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
に引渡すパスがフルパスでないと動きませんでした






yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[javaSwing]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 12:59:27
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube