iTextSharp表格宽度为页面的100%

我正在尝试使用iTextSharp为文档添加表格。 这是一个例子:

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%的空间。 我在这里做错了吗?

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

要设置静态值,属性为TotalWidth

弄清楚了。 显然table.Width是百分比而不是宽度(以像素为单位)。 所以使用:

 table.Width = 100; 

工作就像一个魅力。

用户还可以按百分比设置表格宽度。

 t.WidthPercentage = 100f; 

iText7中不再提供WidthPercentage属性。 请改用以下内容

 table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));