2009-09-26 249 views
16

我想用iTextSharp將表格添加到文檔。這裏有一個例子:iTextSharp表格寬度100%的頁面

Document document = new Document(PageSize.LETTER,72, 72, 72, 72); 
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C:\\test.pdf", FileMode.Create)); 

document.Open(); 
Table table = new Table (2, 1); 
table.Width = document.RightMargin - document.LeftMargin; 

// Cell placeholder 
Cell cell = new Cell (new Paragraph ("Some Text")); 
table.AddCell (cell); 
cell = new Cell (new Paragraph ("More Text")); 
table.AddCell (cell); 
document.Add (table); 
document.Close (); 

我設置表格的寬度,以便它應該擴展頁邊距。但是,當pdf創建時,表格只佔邊距之間80%的空間。我在這裏做錯了什麼嗎?

回答

50

在iTextSharp最新版本(5.0.4)中,PdfPTable具有WidthPercentage屬性。

要設置靜態值,該屬性爲TotalWidth

+1

[5.5.9] TotalWidth不是一個靜態值 - 它只是對象屬性。 – MaLiN2223 2016-05-03 21:11:15

30

想通了。顯然table.Width是一個百分比,而不是以像素爲單位的寬度。所以使用:

table.Width = 100; 

工作就像一個魅力。

2

用戶還可以通過百分比設置表格寬度。

t.WidthPercentage = 100f; 
+2

接受的答案已經說過了。 – Kyle 2017-05-26 11:20:08