3

I am creating labels (as in Avery labels) using iText 5 tables. Positioning of label elements requires some very tight tolerances in order to fit everything on the label. My problem is that I have various zones on the label as PdfPCells. 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 does not bring the text to the top of my cell.

I'd show image but apparently I'm not allowed.

How do I 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();

  }
}
Richard Roman
  • 43
  • 1
  • 4

1 Answers1

6

You are very close to the solution. All the properties you're setting are OK, now try adding:

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

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

The top "padding" 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. You are working in text mode, which means that you can define the leading at the level of the cell (as opposed to composite mode where you define the leading at the level of the elements). For instance: you can remove 4pt from the top "padding" like this:

companyCell.setLeading(14);

Important: this will also reduce the spacing between the different lines. If that i not an option, you may want to switch to composite mode.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165