2012-09-24 4 views
5

月の縦リストを作成し、毎月の水平日のリストを作成します。私は毎日、大きさと色のついた矩形を追加しています。サイズと色はdbクエリの値に依存します。PdfPCellでテンプレート要素を整列する方法

私は他のすべてが離れて長方形の位置から、(長方形の大きさ、矩形の色)正常に動作this answer

で提供PdfPTablePdfPCellcbCreateTemplateを使用しています:それは常にに位置しています0,0でも私はV & Hポジショニングを設定しました。コードの抜粋は以下の通りです。お知らせ下さい。

int Severity = args.getPLR().get(i).getItems().get(j).getItems().get(itemIndex).getSeverity(); 
Severity = Severity + 5; //plump up, so that max (10) should fill the cell 
PdfPCell cell = new PdfPCell(); 
cell.setPadding(0); 
template = cb.createTemplate(Severity, Severity); 
template.setLineWidth(0.5f); 
template.rectangle(0, 0, Severity, Severity); 
//we should switch the color 
//based on the Severity 
switch ((Severity-5)) { 
    case 0: 
     template.setColorFill(Color.GREEN); 
     break; 
    case 1: 
     template.setColorFill(Color.GREEN); 
     break; 
    case 2: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 3: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 4: 
     template.setColorFill(Color.YELLOW); 
     break; 
    case 5: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 6: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 7: 
     template.setColorFill(Color.ORANGE); 
     break; 
    case 8: 
     template.setColorFill(Color.RED); 
     break; 
    case 9: 
     template.setColorFill(Color.RED); 
     break; 
    case 10: 
     template.setColorFill(Color.RED); 
     break; 
} 
template.fill(); 
img = Image.getInstance(template);   
chunk = new Chunk(img, 0f, 0f); 
cell.addElement(chunk); 
cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
painTable.addCell(cell); 

これが示されているもののグラフィックです: should be aligned center/center

それはセンター/センターでなければなりません。どこが間違っていたのですか?

これが受け入れられた溶液を使用して更新されたコードの一部です:

img = Image.getInstance(template);   
chunk = new Chunk(img, 0f, 0f); 
Phrase severityChunk = new Phrase(chunk); 
PdfPCell cell = new PdfPCell(severityChunk); 
cell.setPadding(0); 
cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
painTable.addCell(cell); 

答えて

10

あなたがtext modecomposite mode混合しています。

アライメントプロパティをPdfPCellのレベルに設定すると、テキストモードでのみ機能します。コンポジットモードに切り替えると(addElement()メソッドを使用すると)、iTextはセルの内容に対して定義されているアライメントを優先してセルに対して定義されたアライメントを無視します。

テキストモードでは、すべてのコンテンツの配置が同じです。コンポジットモードでは、異なるアラインメントで異なる要素を持つことができます。

異なるオプションがあります。ChunkParagraphに配置して、セルではなく段落の配置を定義できます。チャンクを含むPhraseを使用して、テキストモードでセルを作成できます。チャンクなどを使用せずにImageのセルを作成することも可能です。

これは、私が書いた「iText in Action」の本ですべて説明されています。

+0

ありがとうございました。私はCellソリューションでPhraseを使用し、上記の更新されたコードを追加しました。 – DaveSav

関連する問題