Java 单元格内的 iText 嵌套表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1340623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iText nested table inside a cell
提问by Averroes
I'm creating a PDF with iText version 2.1.0. I have to create a "detail" cell in a cell of a table. I did this nesting a table inside that cell. The problem with this approach is that the borders of the nested table don't touch the borders of the container cell. What I am looking for is for a table nested inside a cell whose borders don't differenciate from the nested table borders.
我正在使用 iText 2.1.0 版创建 PDF。我必须在表格的单元格中创建一个“详细信息”单元格。我在那个单元格中嵌套了一个表格。这种方法的问题在于嵌套表格的边框不接触容器单元格的边框。我正在寻找的是嵌套在单元格内的表格,该单元格的边框与嵌套的表格边框没有区别。
I have a test like this one. I do this inside a loop to add tables inside a cell to the outer table:
我有一个这样的测试。我在循环中执行此操作以将单元格内的表格添加到外部表格:
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);
Thanks.
谢谢。
采纳答案by Averroes
I think I finally found it:
我想我终于找到了:
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);
The trick here is using the table in one of the PdfPCell constructor.
这里的技巧是在 PdfPCell 构造函数之一中使用该表。
回答by cagcowboy
As you identified,
正如你所确定的,
cell.setPadding(0);
is what you needed.
是你所需要的。
回答by schlow
I found that what was causing my tables to be smaller than the enclosing cell was that I was not adding the following code to the table:
我发现导致我的表格小于封闭单元格的原因是我没有将以下代码添加到表格中:
table.setWidthPercentage(100);