2016-02-12 43 views
0

我正在使用iText 5.5.4生成pdf。分页符中行跨越单元格中的文本

我有一堆行应该组合在一起,并有一个rowspan单元格来命名组。但是,在分页符中,“GRP”一词被分解为“G” 和“RP”。有没有办法让团体牢不可破,这样如果它不适合当前的那个,它会在下一页绘制出来?

我试过keepRowsTogethersetBreakpoints,但没有得到一致的结果。

------------------- 
| G | row 1 | 
| R | row 2 | 
| P | row 3 | 
------------------- 


版面图:

Layout Image

Document document = new Document(new Rectangle(300, 125)); 
PdfWriter.getInstance(document, new FileOutputStream(dest)); 
document.open(); 
document.add(new Paragraph("Table with setSplitLate(true):")); 
PdfPTable table = new PdfPTable(2); 
table.setWidths(new float[]{1,5}); 
table.setSpacingBefore(10); 
PdfPCell cell = new PdfPCell(); 
cell.addElement(new Paragraph("G")); 
cell.addElement(new Paragraph("R")); 
cell.addElement(new Paragraph("O")); 
cell.addElement(new Paragraph("U")); 
cell.addElement(new Paragraph("P")); 

//PdfPCell cell = new PdfPCell(new Phrase("GROUP")); 
//cell.setNoWrap(true); 
//cell.setRotation(90); 
//cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
//cell.setHorizontalAlignment(Element.ALIGN_CENTER); 

cell.setRowspan(5); 
table.addCell(cell); 
table.addCell("row 1"); 
table.addCell("row 2"); 
table.addCell("row 3"); 
table.addCell("row 4"); 
table.addCell("row 5"); 
document.add(table); 
document.add(new Paragraph("Table with setSplitLate(false):")); 
table.setSplitLate(false); 
document.add(table); 
document.close(); 

PDF Output from above code

回答

2

由于您没有提供任何代码,当你发布在计算器上的一个问题这是一种强制性的(除非你对获得答案不感兴趣),我试图写一个例子来重现(不希望的)结果。

我没有成功。见我SplittingAndRowspan example

document.add(new Paragraph("Table with setSplitLate(true):")); 
PdfPTable table = new PdfPTable(2); 
table.setSpacingBefore(10); 
PdfPCell cell = new PdfPCell(); 
cell.addElement(new Paragraph("G")); 
cell.addElement(new Paragraph("R")); 
cell.addElement(new Paragraph("P")); 
cell.setRowspan(3); 
table.addCell(cell); 
table.addCell("row 1"); 
table.addCell("row 2"); 
table.addCell("row 3"); 
document.add(table); 

我真的不使用setSplitLate(true);,因为分裂后期参数的默认值是true。结果是这样的:

enter image description here

由于含有摹; R P不适合页面的细胞,该表被转发到下一个页面(这是你想要什么)。

当然,当我使用setSplitLate(false)这种情况不会发生:

document.add(new Paragraph("Table with setSplitLate(false):")); 
table.setSplitLate(false); 
document.add(table); 

在这种情况下,含有GRP所述细胞是分裂:

enter image description here

基于共享稀缺信息在你的问题中,我假设你的代码中有一行table.setSplitLate(false);。如果是这样,请删除它。如果没有,请分享您的代码。

+0

我更新了您分享的代码以重现我面临的问题。评论部分是我如何实际添加文本到行跨单元。 –

+0

如果您能够重现代码示例中的问题,请让我知道。我没有使用“setSplitLate”标志。我想要的只是如果组名不能适合当前页面在下一页上绘制它。动态地从数据源中提取组的名称和组的行号。 –

+0

嗨,我回顾了我的代码,我犯了一个错误。我分享的例子突破了,因为它无法适应下一页的表格。我的代码的问题是由于多级表。外表无法完全适合一页,因此无论内表中的数据是否适合下一页或不。它的工作很好,一旦我删除了外部表格,对版面进行了轻微的修改。 –