MigraDoc section width

Viewed 938

I wanted a method to set relative column width in MigraDoc, and I found this post on the subject. The problem is, it does not work for me. I have copied the exact code from that post:

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

int sectionWidth = (int)(section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin);
int columnWidth = sectionWidth / 2;

However, if I insert a breakpoint in the code (right after int columnWidth = ...), it states that the section page width is zero:

Screenshot of Output

So obviously everything which is derived from the section width, also becomes zero. But why? As you can see, the PageFormat is correctly set to "A4". I don't get it...

1 Answers

I managed to find a solution (a bit by coincidence). This post describes a somewhat similar issue with section.PageSetup. The solutions is to create a clone of the default page setup, before modifying it. The new code looks like this:

Section section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone(); // <-- This has been added
section.PageSetup.PageFormat = PageFormat.A4;

int sectionWidth = (int)(section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin);
int columnWidth = sectionWidth / 2;
Related