C# iTextSharp 表格宽度 100% 的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1481139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:56:54  来源:igfitidea点击:

iTextSharp table width 100% of page

c#pdfitextwidth

提问by Kyle

I'm trying to add a table to a document using iTextSharp. Here is an example:

我正在尝试使用 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 ( );

I'm setting the width of the table so that it should extend the margin of the page. But when the pdf is created the table only takes about 80% of the space between the margin's. Am I doing something incorrectly here?

我正在设置表格的宽度,以便它应该扩展页面的边距。但是当创建 pdf 时,表格只占用边距之间大约 80% 的空间。我在这里做错了吗?

采纳答案by Jla

In iTextSharp latest version (5.0.4) the PdfPTablehas a WidthPercentageproperty.

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

To set a static value the property is TotalWidth.

要设置静态值,属性是TotalWidth

回答by Kyle

Figured it out. Apparently table.Widthis a percent and not the width in pixels. So using:

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

table.Width = 100;

Worked like a charm.

像魅力一样工作。

回答by mihir patel

Users can also set table width by Percentage.

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

t.WidthPercentage = 100f;

回答by Kolappan N

The WidthPercentage property is no longer available in iText7. Use the following instead

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

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

回答by Sachin Poreyana

In Java table.setWidthPercentage(100);Works in 5.5.8 version

在 Java table.setWidthPercentage(100);Works 5.5.8 版本中