2009-08-27 89 views
8

我使用iText 2.1.0版创建PDF。我必须在表格的单元格中创建一个“细节”单元格。我在这个单元格内嵌入了一张桌子。这种方法的问题是嵌套表的边框不会触及容器单元的边界。我在寻找的是嵌套在单元格内的表格,这些单元格的边界与嵌套的表格边界没有区别。单元格内的iText嵌套表

我有一个这样的测试。我在一个循环内部将一个单元格中的表格添加到外部表格中:

PdfPCell testCell = new PdfPCell(new Paragraph("Test")); 
//I want this border to touch the containerCell borders. 
testCell.setBorder(PdfPCell.BOTTOM); 
testTable = new PdfPTable(2); 

testTable.addCell(testCell); 
testTable.addCell(testCell); 
testTable.addCell(testCell); 
testTable.addCell(testCell); 

PdfPCell containerCell = new PdfPCell(); 
containerCell.addElement(testTable); 
outerTable.addCell(containerCell); 

谢谢。

回答

16

我想我终于找到了:

testTable = new PdfPTable(1); 
PdfPCell c2; 
testTable.addCell("aaaa"); 
testTable.addCell("bbbb"); 

c2 = new PdfPCell (testTable);//this line made the difference 
c2.setPadding(0); 
outerTable.addCell(c2); 

这里的窍门是在PdfPCell构造一个使用表。

+0

为什么是C1那里,但没有提到?这是需要的吗? – 2014-08-21 11:51:06

+0

我想是一个错字。它应该是最可能的c2。我会修好它。 – Averroes 2014-08-22 06:11:30

2

当你确定,

cell.setPadding(0); 

是你需要的东西。

+1

是的,但我发现这样做 c2 = new PdfPCell(); c2.addElement(testTable); c2.setPadding(0); 工作方式与 不一样c2 = new PdfPCell(testTable); c2.setPadding(0); outerTable.addCell(c2); 在第一种情况下,您可以看到嵌套的表格边框。 – Averroes 2009-08-28 15:48:40

+1

是的,使用AddElement时,会使用添加的元素的属性。使用构造函数时,将使用表格单元格的属性,而不是所添加元素的属性。 – 2011-03-25 11:37:06

3

我发现,是什么原因导致我的表比封闭细胞是,我并没有加入以下代码表中较小:

table.setWidthPercentage(100);