How to get rid of top padding in PdfPCell, iText 5

I create labels (as in Avery labels) using iText 5 tables. The positioning of label elements requires very tight tolerances to fit everything on the label. My problem is that I have different zones on the label like PdfPCells. I need to insert text into these 0 wasted space zones. But I always have extra space at the top of the camera. This is best illustrated by using .setVerticalAlignment (Element.ALIGN_TOP); which doesn't bring the text to the top of my cell.

I would have shown the image, but apparently I was not allowed.

How to get rid of this space?

package actions.test;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfCellTest
{
  public static void main(String[] args) throws Exception {

    System.out.println("Cell Test");
    BaseFont bf = BaseFont.createFont
(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
    Font companyFont =new Font(bf);
    companyFont.setSize(10.5f);
    companyFont.setColor(BaseColor.BLUE);
    companyFont.setStyle(Font.BOLD);

    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document,
 new FileOutputStream("c:\\temp\\celltest.pdf"));

    document.open();

    PdfPTable main =  new PdfPTable(1);
    main.setWidthPercentage(30);

    Phrase companyPhrase = new Phrase("My Company Name, LLC",companyFont);

    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);
    main.addCell(companyCell);

    document.add(main);
    document.close();

  }
}

      

+3


source to share


1 answer


You are very close to a solution. All the properties you are configuring are ok, now try adding:

companyCell.setUseAscender(true);
companyCell.setUseDescender(true);

      

What do these methods do? They take into account the metrics that are stored in the font used. You are talking about top padding, but you will notice that "descender" will also have a nice effect on bottom padding.



The top "shim" is not really an add-on. This is the "presenter". You are using the default font, which is Helvetica 12pt. The default is 1.5 times the font size. It's 18pt. You are in text mode, which means you can define a cell-level pivot (as opposed to composite mode, in which you define an element-level pivot). For example: you can remove 4pt from the top padding like this:

companyCell.setLeading(14);

      

Important: this will also reduce the space between different lines. If that's not an option, you can switch to composite mode.

+4


source







All Articles