我正在尝试使用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;
WidthPercentage属性在iText7中不再可用。使用以下内容代替
table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
在Java table.setWidthPercentage(100);
中适用于5.5.8版本