2016-07-28 25 views
0

如果您看到下表,我已將兩個單元格分開,將一個單元格添加爲左側單元格(名稱),並將另外一個單元格添加爲表格。通過刪除左側單元格重新設計輸出

我曾嘗試下面的代碼:

我用包作爲進口com.lowagie.text.pdf *;

PdfWriter.getInstance(document, 
    new FileOutputStream("C:/Temp/TableWidthAlignment.pdf")); 

document.open(); 

//Main table 
PdfPTable mainTable = new PdfPTable(2); 
mainTable.setWidths(new int[] { 10,90 }); 

//cell one is Name cell 
PdfPCell innerCellKeyName = new PdfPCell(new Phrase("Name", boldFont)); 
//innerCellKeyName.setBorder(Rectangle.NO_BORDER); 
mainTable.addCell(innerCellKeyName); 

PdfPTable table = new PdfPTable(3); 
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3")); 
cell.setColspan(3); 
table.addCell(cell); 
table.addCell("1.1"); 
table.addCell("2.1"); 
table.addCell("3.1"); 
table.addCell("1.2"); 
table.addCell("2.2"); 
table.addCell("3.2"); 
table.addCell("4.1"); 
table.addCell("4.2"); 
table.addCell("4.3"); 

//cell two is as table 
PdfPCell cell2 = new PdfPCell(table); 
mainTable.addCell(cell2); 

document.add(mainTable); 

輸出是:

Actual output what I got from above code

預期輸出是:交叉箱需要被移除形式盒在左單元。

Expected output

+0

你知道你使用的是舊版本的iText的,自2009年12月,7年前已壽命結束? –

+0

是的,這是真的,這是舊的應用程序,這是我們必須遵循舊規則的原因。 – Arun

+0

我們現在已經7年了。也許是時候升級了。使用行跨度是行之有效的方法,但在您過時的iText版本中,「PdfPTable」不支持行跨度。 –

回答

0

我嘗試過的一些事情,使工作了預期的結果

解決方案:
我有複製粘貼相同上面的表格並提出了左邊的單元格,因爲沒有邊界。

   document.open(); 

      PdfPTable mainTable = new PdfPTable(2); 
      mainTable.setWidths(new int[] { 10,90 }); 

      PdfPCell innerCellKeyName = new PdfPCell(new Phrase("Name", boldFont)); 
      //innerCellKeyName.setBorder(Rectangle.NO_BORDER); 
      mainTable.addCell(innerCellKeyName); 

      // step4 
      PdfPTable table = new PdfPTable(3); 
      PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3")); 
      cell.setColspan(3); 
      table.addCell(cell); 
      table.addCell("1.1"); 
      table.addCell("2.1"); 
      table.addCell("3.1"); 
      table.addCell("1.2"); 
      table.addCell("2.2"); 
      table.addCell("3.2"); 
      table.addCell("4.1"); 
      table.addCell("4.2"); 
      table.addCell("4.3"); 



      PdfPCell cell2 = new PdfPCell(table); 
      mainTable.addCell(cell2); 
      document.add(mainTable); 


      PdfPTable mainTable2 = new PdfPTable(2); 
      mainTable2.setWidths(new int[] { 10,90 }); 

      PdfPCell innerCellKeyName2 = new PdfPCell(new Phrase("", boldFont)); 

      innerCellKeyName2.setBorder(Rectangle.NO_BORDER); 
      mainTable2.addCell(innerCellKeyName2); 

      // step4 
      PdfPTable table2 = new PdfPTable(3); 
      PdfPCell cell3 = new PdfPCell(new Paragraph("header with colspan 3")); 
      cell3.setColspan(3); 
      table2.addCell(cell3); 
      table2.addCell("1.1"); 
      table2.addCell("2.1"); 
      table2.addCell("3.1"); 
      table2.addCell("1.2"); 
      table2.addCell("2.2"); 
      table2.addCell("3.2"); 
      table2.addCell("4.1"); 
      table2.addCell("4.2"); 
      table2.addCell("4.3"); 



      PdfPCell cell4 = new PdfPCell(table2); 
      mainTable2.addCell(cell4); 


      document.add(mainTable2); 
相關問題