我正在编写一个使用iText生成PDF或RTF文件,并在其中包含表格的程序。我使用了iText类table和cell,而不是更具体的RtfTable或pdfTable,以便最终可以生成任一个文件。我需要将单元格填充设置为-1的值,否则打印出来的数据表中每一行之间的空间太大了。
然而,现在我想要添加边框(特别是到PDF文件中),但是单元格与文本不对齐。每个单元格的底部边框直接穿过文本,只有当将单元格填充设置为2或更高时,才实际上围绕着文本。以下是我正在做的示例:
然而,现在我想要添加边框(特别是到PDF文件中),但是单元格与文本不对齐。每个单元格的底部边框直接穿过文本,只有当将单元格填充设置为2或更高时,才实际上围绕着文本。以下是我正在做的示例:
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();
}
有没有办法解决这个问题,即将整个边框向下移动一点(不影响文本位置),或者消除每行之间的空白(看起来只有在文本上方存在间距问题,而不是下方),而不设置单元格填充为-1?