2016-02-12 60 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

嗨,我回顧了我的代碼,我犯了一個錯誤。我分享的例子突破了,因爲它無法適應下一頁的表格。我的代碼的問題是由於多級表。外表無法完全適合一頁,因此無論內表中的數據是否適合下一頁或不。它的工作很好,一旦我刪除了外部表格,對版面進行了輕微的修改。 –