I am building a vertical list of months with a horizontal list of days in each month. To each day I am adding a sized and colored rectangle; the size and color is dependant on a value from a db query.
I am using PdfPTable
, PdfPCell
and cbCreateTemplate
provided in this answer
Everything else works fine (the size of the rectangle, the color of the rectangle), apart from the position of the rectangle: it is always positioned at 0,0 even though I (think) I have set the V & H positioning. An excerpt of the code is below; please advise.
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 a graphic of what is shown:
It should be Center / Center. Where have I gone wrong?
This is the updated code part using the accepted solution:
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);
You're mixing text mode
and composite mode
.
Alignment properties set on the level of the PdfPCell
work in text mode only. As soon as you switch to composite mode (you do so by using the addElement()
method), iText ignores the alignment defined for the cell in favor of the aligment defined for the contents of the cell.
In text mode all the content has the same alignment. In composite mode, you can have different elements with different alignments.
You have different options: you can put the Chunk
in a Paragraph
and define the alignment for the paragraph instead of for the cell. You can create the cell in text mode using a Phrase
that contains the Chunk. It may even be possible to create a cell with an Image
without using a Chunk, etc...
This is all explained in the "iText in Action" book I wrote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With