Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp table width 100% of page

Tags:

c#

width

pdf

itext

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?

like image 924
Kyle Avatar asked Sep 26 '09 12:09

Kyle


4 Answers

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

To set a static value the property is TotalWidth.

like image 191
Jla Avatar answered Nov 10 '22 01:11

Jla


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

table.Width = 100;

Worked like a charm.

like image 31
Kyle Avatar answered Nov 10 '22 02:11

Kyle


Users can also set table width by Percentage.

t.WidthPercentage = 100f;
like image 6
mihir patel Avatar answered Nov 10 '22 02:11

mihir patel


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

table.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
like image 3
Kolappan N Avatar answered Nov 10 '22 01:11

Kolappan N