How to size a table to the page width in MigraDoc?

jstuardo picture jstuardo · Jul 28, 2014 · Viewed 12.1k times · Source

I am trying to resize a table automatically to full width of the page. That table should have 2 columns, 50% width each.

How can I achieve this? I tried LeftIndent and RightIndent properties with no luck.

Answer

Kidquick picture Kidquick · Jun 23, 2015

Here's an approach that avoids hardcoding widths and allows for more flexible paper formats. Make sure to include the using MigraDoc.DocumentObjectModel; statement in your class.

Document document = new Document();

Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.A4;

Table table = section.AddTable();

float sectionWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
float columnWidth = sectionWidth / 2;

Column column = table.AddColumn();
column.Width = columnWidth;
Column column2 = table.AddColumn();
column2.Width = columnWidth;

Row row = table.AddRow();
row.Cells[0].AddParagraph("Row 1, Column A");
row.Cells[1].AddParagraph("Row 1, Column B");