Java iText 结合 rowspan 和 colspan - PDFPTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23989852/
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 combining rowspan and colspan - PDFPTable
提问by Pega88
Working on a calendar projcet and using iText to generate a pdf to print appointments. I can plot a cell with a colspan, and a cell with a rowspan, but I can't combine it. Table has width of 4 cells. I want to achieve something like this:
处理日历项目并使用 iText 生成 pdf 以打印约会。我可以绘制一个带有 colspan 的单元格和一个带有 rowspan 的单元格,但我不能将它组合起来。表格的宽度为 4 个单元格。我想实现这样的目标:
(A)(B)(C)(C)
(A)(B)(C)(C)
(D)(E)(C)(C)
(D)(E)(C)(C)
so (1,1), (1,2) and (2,1) (2,2) are regular cells. But there should be a cell in (1,3) covering (1,3) (1,4) (2,3) and (2,4) thus having a colspan of 2 AND a rowspan of 2.
所以 (1,1), (1,2) 和 (2,1) (2,2) 是常规单元格。但是在 (1,3) 中应该有一个单元格覆盖 (1,3) (1,4) (2,3) 和 (2,4) 因此 colspan 为 2 AND rowspan 为 2。
Current code:
当前代码:
PdfPTable table = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Phrase(" 1,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 1,2 "));
table.addCell(cell);
PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));
cell23.setColspan(2);
cell23.setRowspan(2);
table.addCell(cell23);
cell = new PdfPCell(new Phrase(" 2,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 2,2 "));
table.addCell(cell);
// 2,3 and 2,4 should be filled because 1,3 has rowspan 2 and colspan 2.
//table.completeRow(); //no effect
PdfPTable 表 = 新 PdfPTable(4);
PdfPCell 单元格 = new PdfPCell(new Phrase(" 1,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 1,2 "));
table.addCell(cell);
PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));
cell23.setColspan(2);
cell23.setRowspan(2);
table.addCell(cell23);
cell = new PdfPCell(new Phrase(" 2,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 2,2 "));
table.addCell(cell);
// 2,3 和 2,4 应该被填充,因为 1,3 有 rowspan 2 和 colspan 2。
//table.completeRow(); //没有效果
However that generates an error:
但是,这会产生错误:
ExceptionConverter: java.io.IOException: The document has no pages.
ExceptionConverter: java.io.IOException: 文档没有页面。
If i don't start creating the second row, it just plots fine ( 1 row, and cell on (1,3) has a colspan of 2. Since there is no second row, the rowspan(2) has no effect. Any help is appreciated. Thanks
如果我不开始创建第二行,它只是绘制得很好(1 行,并且 (1,3) 上的单元格的 colspan 为 2。由于没有第二行,rowspan(2) 不起作用。任何感谢帮助。谢谢
采纳答案by Bruno Lowagie
At first sight, I'd say: you get a "document has no pages" exception because you're not adding any content to the document. I don't see:
乍一看,我会说:您会收到“文档没有页面”的异常,因为您没有向文档添加任何内容。我没看到:
document.add(table);
anywhere in your code snippet.
代码片段中的任何位置。
I have copy/pasted your code into a full example and I posted the full example here: ColspanRowspan. The resulting PDF looks like this:
我已将您的代码复制/粘贴到一个完整示例中,并在此处发布了完整示例:ColspanRowspan。生成的 PDF 如下所示:
This seems to be the desired behavior. I can only think of two differences: (1) you're forgetting to add the actual table (which was my initial answer), or (2) you are using a mighty old version of iText in which rowspan wasn't fully supported.
这似乎是所需的行为。我只能想到两个区别:(1)您忘记添加实际表格(这是我最初的答案),或者(2)您使用的是强大的旧版 iText,其中不完全支持 rowspan。