2016-12-06 50 views
0

我試圖用Java創建一個利用iText 7 PDF文檔。但是表格內容沒有顯示在pdf中。iText的7:表的內容是沒有得到顯示

代碼段:

private void addReceiptDetails(Document doc) { 
     Table table = new Table(2, true); 
     table.addCell(new Cell().add("C1")); 
     table.addCell(new Cell().add("C2")); 
     for (TaxTypeModel taxType : sale.getTaxModel().getTaxTypeModelList()) { 
      table.addCell(new Cell().add("C9")); 
      table.addCell(new Cell().add("C10")); 
     } 
     table.addCell(new Cell().add("C3")); 
     table.addCell(new Cell().add("C4")); 

     table.addCell(new Cell().add("C5")); 
     table.addCell(new Cell().add("C6")); 

     table.addCell(new Cell().add("C7")); 
     table.addCell(new Cell().add("C8")); 

     doc.add(table); 
     table.complete(); 
     doc.close(); 
    } 

PDF表的內容:

enter image description here

有人可以請幫我在這裏。我不確定這裏到底出了什麼問題。

編輯:我嘗試了代碼示例從here

Table table = new Table(8); 
     for (int i = 0; i < 16; i++) { 
      table.addCell("hi"); 
     } 
     doc.add(table); 

而這也只是創造空單元格。

回答

0

我無法重現您的問題。

我跑與明顯的變化代碼:

  • 我不得不刪除for循環,因爲我沒有這樣的TaxTypeModel在這裏做。
  • 我不得不添加實例化代碼的文件。

即像這樣:

try ( OutputStream result = new FileOutputStream("simpleTableRahulDevMishra.pdf"); 
     PdfWriter writer = new PdfWriter(result); 
     PdfDocument pdfDocument = new PdfDocument(writer); 
     Document doc = new Document(pdfDocument) ) 
{ 
    Table table = new Table(2, true); 
    table.addCell(new Cell().add("C1")); 
    table.addCell(new Cell().add("C2")); 
    /* 
    for (TaxTypeModel taxType : sale.getTaxModel().getTaxTypeModelList()) { 
     table.addCell(new Cell().add("C9")); 
     table.addCell(new Cell().add("C10")); 
    } 
    */ 
    table.addCell(new Cell().add("C3")); 
    table.addCell(new Cell().add("C4")); 

    table.addCell(new Cell().add("C5")); 
    table.addCell(new Cell().add("C6")); 

    table.addCell(new Cell().add("C7")); 
    table.addCell(new Cell().add("C8")); 

    doc.add(table); 
    table.complete(); 
    doc.close(); 
} 

結果看起來像這樣在Adobe Reader:

Screenshot

你的問題的原因,因此,是不是在您所提供的代碼。所以請提供允許重現您的問題的代碼。


試圖重現該問題與其他代碼示例

Table table = new Table(8); 
for (int i = 0; i < 16; i++) { 
    table.addCell("hi"); 
} 
doc.add(table); 

也導致細胞內容:

Screenshot

+0

非常感謝你的幫助。我花了很多時間一個可怕的量來解決這個問題。我現在可以解決它。 我不得不爲每個單元添加'.setFontColor(Color.BLACK)'。 我將頁面背景圖片設置爲LIGHT_GRAY,也許這就是爲什麼默認顏色不可見的原因。 –

+0

*「我必須爲每個單元格添加'.setFontColor(Color.BLACK)」* - 我假設有一個更簡單的解決方案,或許您只需在正確的位置將填充顏色設置爲黑色即可...... – mkl