2014-07-10 50 views
0

爲什麼包含段落p的單元格未被添加到表格中?在itext中添加表格的單元格

package iText; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.*; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.Font.FontFamily; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 

import java.io.FileOutputStream; 

public class NewMain1 { 
    public static void main(String[] args) { 
     Document document = new Document(); 

     try { 
      PdfWriter.getInstance(document, 
       new FileOutputStream("C:\\Users\\om\\Desktop\\pdf\\2.pdf")); 

      document.open(); 

      PdfPTable table = new PdfPTable(3); // 3 columns. 
      Font font = new Font(FontFamily.HELVETICA, 22, Font.BOLD, BaseColor.WHITE); 
     Paragraph p = new Paragraph("xyz", font); 
     table.addCell("abc");  
     table.addCell(p); 

      table.addCell("cef"); 
      table.addCell("ghi"); 
      // table.completeRow(); 
      PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); 
      PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2")); 
      PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3")); 

      table.addCell(cell1); 
      table.addCell(cell2); 
      table.addCell(cell3); 
cell1.setHorizontalAlignment(Element.ALIGN_LEFT); 

      document.add(table); 

      document.close(); 
     } catch(Exception e){ 

     } 
    } 
} 

p.s我是這個itext的新手。如果有人能夠解釋我如何通過itext生成pdf表單,那將是非常棒的。在此先感謝:)

+0

[看這裏](http://itextpdf.com/examples/iia.php?id=142)或嘗試谷歌。 – Jens

+0

@Jens我見過這些例子,但我無法找出我的錯誤。 – user3819936

回答

1

您正在創建一個白色的字體,並將其繪製在通常顯示爲白色的畫布上。它在那裏,你看不到它。嘗試將顏色更改爲BaseColor.BLACK之類的其他顏色。

編輯

爲了回答您的評論的問題,重載爲Document一個構造函數接受一個定義爲頁面的默認大小的矩形。 iText有一個名爲PageSize的幫助類,它定義了許多常見頁面,但您可以自由使用從到14,400 x 14,400的任何維度。例如,你可以說new Document(PageSize.LETTER)new Document(new RectangleReadOnly(612, 702))

一旦你知道,你有你的最大xy,除非你做一些很奇怪的最低xy均爲零。使用PDF時,左下角是原點,因此0 x 0

+0

thanx :) 嗯,你可以告訴我1件事..如果我要繪製一個畫布或矩形或地點的圖像或任何特定的X和Y座標,那麼我怎麼會知道這些座標? – user3819936

相關問題