How to size a table to the page width in MigraDoc?
Asked Answered
S

3

24

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.

Stoller answered 27/7, 2014 at 22:49 Comment(1)
posting some of you current code might help us.....Androgyne
B
32

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 = document.DefaultPageSetup.Clone();
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");
Barlow answered 23/6, 2015 at 20:53 Comment(2)
It should be noted that this works only if PageSetup.PageWidth, PageSetup.LeftMargin and PageSetup.RightMargin where previously assigned. If you want to use the default values for PageSetup, assign DefaultPageSetup.Clone() to the PageSetup of the section.Silicium
Add section.PageSetup = document.DefaultPageSetup.Clone(); before section.PageSetup.PageFormat = PageFormat.A4;Illustrate
S
2

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.

Silicium answered 28/7, 2014 at 12:5 Comment(0)
E
0

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;
Erbium answered 4/8, 2017 at 12:50 Comment(1)
The DefaultPaegSetup should never be modified. A Clone() of DefaultPageSetup can be assigned to the PageSetup of the session and can be modified as needed.Silicium

© 2022 - 2024 — McMap. All rights reserved.