How to get rid of the top padding in a PdfPCell?
I have various zones on the label as PdfPCell
s. I need to fit text into these zones with 0 wasted space. But I always seem to have extra space at the top of the cell. This is best illustrated by using setVerticalAlignment(Element.ALIGN_TOP)
which doesn't result in positioning the text near the top border of my cell, even with padding 0. How can I get rid of that top padding?
This is my code so far: PdfPCell companyCell = new PdfPCell(companyPhrase); companyCell.setHorizontalAlignment(Element.ALIGN_CENTER); companyCell.setVerticalAlignment(Element.ALIGN_TOP); companyCell.setBorder(Rectangle.BOX); companyCell.setBorderColor(BaseColor.RED); companyCell.setPadding(0); companyCell.setFixedHeight(10.5f);
companyCell.setBackgroundColor(BaseColor.WHITE);
Posted on StackOverflow on Jul 17, 2015 by Richard Roman
You talk about the top padding, but it isn't really a padding. It's the "leading". You are using the default font, which is Helvetica 12pt. The default leading is 1.5 times the font size. That's 18pt. The solution is to change the leading of a Paragraph
(there is no Phrase
class anymore). In iText 7 this is done in the following way:
Cell cell = new Cell().add(new Paragraph("Company phrase")
.setPadding(0)
.setMultipliedLeading(0.5f))
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.setVerticalAlignment(VerticalAlignment.TOP)
.setHeight(10.5f);
table.addCell(cell);
Click How to get rid of the top padding in a PdfPCell? if you want to see how to answer this question in iText 5.