iTextSharp表宽度为页面的100%

问题描述 投票:20回答:5

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

c# pdf itext width
5个回答
55
投票

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

要设置静态值,属性为TotalWidth


33
投票

想通了。显然table.Width是百分比,而不是以像素为单位的宽度。因此使用:

table.Width = 100;

像魅力一样工作。


4
投票

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

t.WidthPercentage = 100f;

2
投票

WidthPercentage属性在iText7中不再可用。使用以下内容代替

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

0
投票

在Java table.setWidthPercentage(100);中适用于5.5.8版本

© www.soinside.com 2019 - 2024. All rights reserved.