Hi Paulo,

an UJAC user had a interesting question about vertical alignment in
PdfPCells. The problem he discovered is that you can't really perform a
top or middle alignment, not even with fixed leading set to zero and
multiplied leading set to 1.0. There will always be a little extra space
between the top of the cell and the contents.

And the workaround by manipulating the paddingTop attribute is no ideal
solution too because it depends on the font size that's used. 

Is there a real solution for that problem?

I've attached an test programm, which produces the problem.

Best regards,
Christian

-- 
Christian Lauer <[EMAIL PROTECTED]>
package ujac.test.itext;

import java.io.FileOutputStream;

import com.lowagie.text.Document;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;

import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class AlignmentTest {
  
  public static void main(String[] args) {
    
    try {
      FileOutputStream os = new FileOutputStream("test.pdf");
      Document document = new Document(PageSize.A4, 25, 25, 25, 25);
      PdfWriter documentWriter = PdfWriter.getInstance(document, os);
      
      document.open();
      Font font = new Font(Font.HELVETICA, 10);

      PdfPTable tab = new PdfPTable(1);
      PdfPCell c = new PdfPCell(new Phrase("[middle]", font));
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_MIDDLE);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);

      c = new PdfPCell(new Phrase("[top]", font));
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_TOP);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      c = new PdfPCell(new Phrase("[bottom]", font));
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_BOTTOM);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      document.add(tab);

      document.add(new Paragraph(" "));

      
      tab = new PdfPTable(1);
      c = new PdfPCell(new Phrase("[middle paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_MIDDLE);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      c = new PdfPCell(new Phrase("[top paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_TOP);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      c = new PdfPCell(new Phrase("[bottom paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_BOTTOM);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);

      document.add(tab);

      
      font = new Font(Font.HELVETICA, 16);

      document.add(new Paragraph(" "));

      
      tab = new PdfPTable(1);
      c = new PdfPCell(new Phrase("[middle paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_MIDDLE);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      c = new PdfPCell(new Phrase("[top paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_TOP);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);
      
      c = new PdfPCell(new Phrase("[bottom paddingTop=-3]", font));
      c.setPaddingTop(-3.0F);
      c.setLeading(0.0F, 1.0F);
      c.setVerticalAlignment(Element.ALIGN_BOTTOM);
      c.setBorderWidth(0.5F);
      c.setBorder(Rectangle.BOX);
      c.setFixedHeight(30.0F);
      tab.addCell(c);

      document.add(tab);

      document.close();
      documentWriter.close();
      
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
}

Attachment: test.pdf
Description: Adobe PDF document

Reply via email to