How to center alignment of a template element in PdfPCell

I am creating a vertical list of months with a horizontal list of days in each month. Every day I add a dimensional and color rectangle; size and color depend on the value from the db request.

I use PdfPTable, PdfPCelland cbCreateTemplatein this answer

Everything else works fine (the size of the rectangle, the color of the rectangle), except for the position of the rectangle: it is always positioned at 0.0, although I (think) I set V and H positioning. Below is a snippet of code; please inform.

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);

This is an image of what is shown: should be aligned center / center

It should be a Center / Center. Where am I wrong?

This is the updated part of the code using the decision made:

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);
+5
source share
1 answer

text mode composite mode.

, PdfPCell, . ( addElement()), iText , , , .

. .

: Chunk Paragraph , . , Phrase, Chunk. Image Chunk ..

"iText in Action", .

+10

All Articles