22

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

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?

4

6 回答 6

56

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

要设置静态值,该属性是TotalWidth.

于 2010-10-18T15:02:24.153 回答
35

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

table.Width = 100;

像魅力一样工作。

于 2009-09-26T12:56:01.007 回答
5

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

t.WidthPercentage = 100f;
于 2017-05-26T11:19:07.963 回答
3

WidthPercentage 属性在 iText7 中不再可用。改用以下内容

table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
于 2018-09-17T10:25:44.427 回答
0

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

于 2019-07-31T09:57:03.167 回答
0

在 c# 中为 itext7

Table details = new Table(4, false).UseAllAvailableWidth();
于 2021-08-17T16:01:00.203 回答