我正在建立一个月份的垂直列表,每个月都有一个水平的天数列表.每天我都会添加一个大小有色的矩形;大小和颜色取决于db查询的值.
我正在使用this answer中提供的PdfPTable,PdfPCell和cbCreateTemplate
除了矩形的位置之外,其他一切工作正常(矩形的大小,矩形的颜色):它始终位于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);
这是显示的图形:
它应该是中心/中心.我哪里出错了?
这是使用已接受的解决方案的更新代码部分:
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);