Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iText cell borders cutting through text

Tags:

I am writing a program that generates a pdf or rtf file with a table in it, using iText. I used the iText class table and cell, rather than the more specific RtfTable or pdfTable so that either file can be generated at the end. I needed to set the cell padding to a value of -1, or else there was too much space between each row of data on the printed sheet. However, I am now trying to add borders (specifically to the pdf file), and the cells are not lining up with the text. The bottom border of each cell cuts directly through the text. It only actually surrounds the text when the cell padding is set to 2 or higher. Below is a sample of what I am doing:

  Document document = new Document();   Paragraph paragraph = new Paragraph();   Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);   try{     PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf"));     document.open();      Table table = new Table(3);     table.setPadding(-1);     table.setWidth(90);     Cell cell1 = new Cell();     cell1.setBorder(Rectangle.BOX);     cell1.setVerticalAlignment(ElementTags.ALIGN_TOP);     table.setDefaultCell(cell1);     paragraph = new Paragraph("header", iTextFont);     Cell cell = new Cell(paragraph);     cell.setHeader(true);     cell.setColspan(3);     table.addCell(cell);     paragraph = new Paragraph("example cell", iTextFont);     table.addCell(paragraph);     paragraph = new Paragraph("one", iTextFont);             table.addCell(cell);     paragraph = new Paragraph("two", iTextFont);     cell = new Cell(paragraph);     table.addCell(paragraph);     paragraph = new Paragraph("Does this start a new row?", iTextFont);     table.addCell(paragraph);     paragraph = new Paragraph("Four", iTextFont);     table.addCell(paragraph);     paragraph = new Paragraph("Five", iTextFont);     table.addCell(paragraph);     document.add(table);   } catch (Exception e) {     //handle exception   }   document.close();    } 

Is there a way to resolve this issue either by moving the whole border down a drop (without affecting the text placement), or to get rid of the spaces between each line (spacing appears to only be a problem above the text, not below) without setting the cell padding to -1?

like image 541
twpc Avatar asked Jun 12 '09 17:06

twpc


1 Answers

Write a class or common methods to build your table -- whether you're using Table or PdfPTable.

These methods will handle standard alignment, measurement based on ascenders/descenders, etc for you.. They also provide a common place to add a "3pt blank paragraph" or whatever other standard formattings, you may need.

OO software isn't meant to be about clanking out repetitive, and potentially inconsistent, sections of code.

Hope this helps.

like image 163
Thomas W Avatar answered Sep 18 '22 22:09

Thomas W