MartinaLabMath MartinaLabMath - 3 months ago 25
C# Question

How to put a table in the center of the page?

With MigraDoc I'm trying to put a table in the center of the page.
I'm using this code (c#, VS).

Section secondPage = new Section();
Table table = new Table();

I get a table aligned to the right of the page; how can I obtain a table in the center of the page?


You can set table.Rows.LeftIndent to indent the table. To get a centered table, calculate the indent based on paper size, page margins, and table width.

Example: Paper size is A4 (21 cm wide), left and right margins are 2.5 cm each. Thus we have a page body of 16 cm.
To center a table that is 12 cm wide, table.Rows.LeftIndent must be set to 2 cm (16 cm body width minus 12 cm table width gives 4 cm remaining space - half of the remaining space must be set as the LeftIndent).

Note that this will also work if the table is slightly wider than the body, but still within page edges. Using the page setup from the previous example, a table that is 18 cm wide can be centered with a LeftIndent of -1 cm.