Search code examples
c#.netmigradoc

MigraDoc 6.1.1 image aligns off the top of the page


Screenshot of image

This is .NET Framework 4.8 using MigraDoc 6.1.1 (from the NuGet package).

As you can see in the screenshot, the "header" image is cut off by the top of the page, there is no top margin.

Here is my current code

Document document = new Document();
Style style = document.Styles["Normal"];
style.Font.Name = "Arial";
style.Font.Size = "10";
style.ParagraphFormat.LineSpacing = 15;
style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;

Section section = document.AddSection();
section.PageSetup.TopMargin = 50;

var para1 = section.AddParagraph();
var image = para1.AddImage(HttpContext.Current.Server.MapPath("~/Content/Images/PdfHeader.png"));
para1.Format.SpaceBefore = 25;
image.RelativeVertical = RelativeVertical.Margin;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.WrapFormat.Style = WrapStyle.TopBottom;
para1.AddLineBreak();

I have tried various things (separately and together):

section.PageSetup.TopMargin = 50;
image.RelativeVertical = RelativeVertical.Margin;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.WrapFormat.Style = WrapStyle.TopBottom;

and

para1.Format.SpaceBefore = 25;

All to no avail


Solution

  • I removed

    style.ParagraphFormat.LineSpacing = 15; 
    style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Exactly;
    

    and now the image is aligned properly inside the top margin