Search code examples
c#pdfpdfsharpmarginsmigradoc

C# MigraDoc margins incorrect size?


Left and top margins are not 7mm. Why?

Document document = new Document();

Section sec = document.AddSection();
sec.PageSetup.PageWidth = Unit.FromMillimeter(210);
sec.PageSetup.PageHeight = Unit.FromMillimeter(297);
sec.PageSetup.LeftMargin = Unit.FromMillimeter(7);
sec.PageSetup.TopMargin = Unit.FromMillimeter(7);
sec.PageSetup.RightMargin = Unit.FromMillimeter(7);
sec.PageSetup.BottomMargin = Unit.FromMillimeter(7);

Table table = sec.AddTable();
table.AddColumn(Unit.FromMillimeter(196));

Row row = table.AddRow();
row.HeightRule = RowHeightRule.Exactly;
row.Height = Unit.FromPoint(70);

row.Cells[0].AddParagraph("TABLE TEXT");

Color blackColor = new Color(0, 0, 0);
row.Shading.Color = blackColor;

In result PDF, left margin is 5.95mm and top margin is 6.86mm.

image of result PDF


Solution

  • You should set table.Rows.LeftIndent to 0. This will increase the left margin.

    By default, text in the table is exactly at 7 mm (in your case) - thus the left margin is a bit smaller if you measure from the edge of the table.

    Setting table.Rows.LeftIndent to 0 will bring the edge of the table to 7 mm.

    Maybe you have to set the width of the table border to 0 to get exactly 7 mm. The border is drawn on both sides, so half of the border width will be subtracted from top and left margins.