2016-12-03 29 views
1

我爲我的iText表設置了一個標題,它有6列,後來我想爲另一個表使用相同的標題,並將colspans設置爲更通用,但是一行rowsn然後'再工作。奇怪的setRowspan錯誤/不能正常工作

這是我原來的(工作)的代碼與6列:

public static PdfPTable createHeaderContent() { 
    PdfPTable table = new PdfPTable(6); 
    table.setWidthPercentage(100); 

    PdfPCell dobicell = new PdfPCell(); 
    dobicell.setColspan(2); 
    dobicell.addElement(new Phrase(docType, DOBIFONTADR)); 
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP); 
    table.addCell(dobicell); 

    dobicell = new PdfPCell(); 
    dobicell.setColspan(2); 
    dobicell.addElement(new Phrase("Ing. Mario J. Schwaiger", DOBIFONTADR)); 
    dobicell.setBorder(Rectangle.TOP); 
    table.addCell(dobicell); 

    dobicell = Dobilogo.getPiccell(92, 104); 
    dobicell.setBorder(Rectangle.TOP | Rectangle.RIGHT); 
    dobicell.setColspan(3); 
    dobicell.setRowspan(2); 
    table.addCell(dobicell); 

    dobicell = getKundenCol(kunde); 
    dobicell.setColspan(2); 
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); 
    table.addCell(dobicell); 

    dobicell = getUserCell(user); 
    dobicell.setColspan(2); 
    table.addCell(dobicell); 

    table.setHeaderRows(1); 
    return table; 
} 

結果看起來像它應該(我用了一些不錯的顏色來表示跨度: enter image description here

的「通用」的修改代碼幾乎相同:

public static PdfPTable createHeaderContent(int[] coldist) { 
    PdfPTable table = new PdfPTable(coldist[0] + coldist[1] + coldist[2]); //createHeaderContent(new int[]{4, 7, 4, 4, 7}); 
    table.setWidthPercentage(100); 

    PdfPCell dobicell = new PdfPCell(); 
    dobicell.setColspan(coldist[0]); //used to be 2, now 4 
    dobicell.addElement(new Phrase(doctype, DOBIFONTADR));   
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP); 
    table.addCell(dobicell); 

    dobicell = new PdfPCell(); 
    dobicell.setColspan(coldist[1]); //used to be 2, now 7 
    dobicell.addElement(new Phrase("Ing. Mario J. Schwaiger", DOBIFONTTITEL)); 
    dobicell.setBorder(Rectangle.TOP); 
    table.addCell(dobicell); 

    dobicell = Dobilogo.getPiccell(92, 104); 
    dobicell.setBorder(Rectangle.TOP | Rectangle.RIGHT); 
    dobicell.setColspan(coldist[2]); //used to be 3, now 4 
    dobicell.setRowspan(2); // <--- This is fishy, but why? 
    table.addCell(dobicell); 

    dobicell = getKundenCol(kunde); 
    dobicell.setColspan(coldist[3]); //used to be 2, now 4 
    dobicell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM); 
    table.addCell(dobicell); 

    dobicell = getUserCell(user); 
    dobicell.setColspan(coldist[4]); //used to be 2, now 7 
    table.addCell(dobicell); 

    table.setHeaderRows(1); 
    return table; 
} 

但是最後一列出現了問題: enter image description here

起初我假設有另一行隱藏和使用dobicell.setRowspan(3);但這改變了第一個數據行已經。嘗試添加另一個單元格將其放在標題後的第一行。

奇怪的是,當我把最後一部分中的用戶單元擴大到整個行分隔的單元格消失。

是否有解決方案或此問題的原因?

+1

你*。*「與6列原來的(工作)的代碼」看起來奇怪,因爲嚴格來說,需要7列:你有兩次'setColspan(2)'一次'setColspan( 3)',所以總結出你有7欄。此外,你有'setHeaderRows(1)' - 你如何期望在單個標題行中與'setRowspan(2)'結合使用?你已經刪除了你的*「修改代碼」*中的第一個問題,但保留了第二個問題。看起來好像兩個問題偶然相遇都會相互抵消,但第二個問題就會導致你的觀察。 – mkl

+0

我剛剛檢查了整個代碼,它很奇怪:是的,你說得對,我在那裏有一個'setColspan(3)'。但是把它放回'setColspan(2)'不起作用。當我寫這個問題時,我做了一些測試,可能會漏掉並留下。與'setHeaderRows(1)'相關的 - 你也注意到了。不,這是原始代碼。當我在編程時,我首先從可用的在線資源中複製了一些工作代碼,然後忘記調整它,因爲它工作正常。 'setHeaderRows(2)'什麼都沒做。顯然是由於rowspan,但這只是一個猜測。 – Qohelet

+0

你說得對。在修改後的版本中,我仍然有'setHeaderRows(1)'。用'setHeaderRows(2)替換'解決了問題 – Qohelet

回答

1

這裏有兩個問題,其中一個iText的似乎忽略和一個引起的問題:因爲他們有兩次setColspan(2)

  • 在原來代碼中的細胞需要7列,一旦setColspan(3)連續。但是該表僅構建了6列:new PdfPTable(6)

    的iText似乎忽略這裏的失蹤列...

  • 代碼聲明的第一行是表頭:table.setHeaderRows(1)。這與第一行中最後一個單元格的聲明衝突,跨越2行setRowspan(2)

    此處的iText忽略標題行中的行跨度,導致不期望的外觀。

    要解決這個問題,請不要聲明行跨度或者使用至少兩個行跨度(如果足夠的行將跟隨)。

在評論中OP證實

在修改後的版本,我仍然有setHeaderRows(1)。與setHeaderRows(2)替換它解決了這個問題

+0

Thx用於查看我的代碼=) – Qohelet

相關問題