Skip to content Skip to sidebar Skip to footer

How To Center Align Template Element In Pdfpcell

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 va

Solution 1:

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.

Solution 2:

cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER) align the text of the cell in the center. You can see an example.

PdfPTabletablaTotal=newPdfPTable(2);
PdfPCellcell1=newPdfPCell(newPhrase("TOTAL"));     
cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell1.setPadding(5);
tablaTotal.addCell(cell1);
PdfPCellcell2=newPdfPCell(newPhrase(contrato.getPrecioInmueble() + " €"));
cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell2.setPadding(5);
tablaTotal.addCell(cell2);

We create tablaTotal with two columns, cell1 with the text Total, we align the text of the cell1 and add and padding to the cell1. We do the same to cell2 and we add the two cell to tablaTotal.

Post a Comment for "How To Center Align Template Element In Pdfpcell"