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

纵饮孤独 提交于 2019-12-05 00:03:42
Kidquick

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");

You cannot use percent values with MigraDoc.
You can set the absolute width of each column.

So when using DIN A4 with 2.5 cm margin at each side, you have 16 cm left for the table and so you have to create two columns of 8 cm each.

You can set the left indent of the table to move tables horizontally.

If the answer from Kidquick does not work, use document.DefaultPageSetup:

document.DefaultPageSetup.PageFormat = PageFormat.A4;

int sectionWidth = (int)document.DefaultPageSetup.PageWidth - (int)document.DefaultPageSetup.LeftMargin - (int)document.DefaultPageSetup.RightMargin;

Table table = section.AddTable();
table.AddColumn(sectionWidth);
Row row = table.AddRow();
row.Height = 60;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!