24

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kyle
  • 17,317
  • 32
  • 140
  • 246

7 Answers7

56

In iTextSharp latest version (5.0.4) the PdfPTable has a WidthPercentage property.

To set a static value the property is TotalWidth.

Jla
  • 11,304
  • 14
  • 61
  • 84
36

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

table.Width = 100;

Worked like a charm.

Kyle
  • 17,317
  • 32
  • 140
  • 246
6

Users can also set table width by Percentage.

t.WidthPercentage = 100f;
Blaise
  • 21,314
  • 28
  • 108
  • 169
mihir patel
  • 181
  • 1
  • 6
3

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

table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
Kols
  • 3,641
  • 2
  • 34
  • 42
1

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

Sachin Poreyana
  • 1,947
  • 15
  • 12
  • The question and the other answers focus on itext for .Net in c# syntax. You use itext for java in java syntax. That's all the difference. – mkl Jul 31 '19 at 13:46
0

In c# for itext7

Table details = new Table(4, false).UseAllAvailableWidth();
blind Skwirl
  • 321
  • 3
  • 6
0
table.setWidthPercentage(100f);

will work

Amit K Kushwaha
  • 113
  • 1
  • 7