5

How can I reduce the top margin of a MigraDoc document?

I added an image to the top right of my document however the space between the top of the document and the image is too big.

Here is how I'm setting the image:

Section section = document.AddSection();
Image image = section.AddImage(@"C:\img\mentSmallLogo.png");
image.Height = "1.5cm";
image.Width = "4cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

And the document style:

Style style = document.Styles["Normal"];
style.Font.Name = "Verdana";

style = document.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);

style = document.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);

// Create a new style called Table based on style Normal
style = document.Styles.AddStyle("Table", "Normal");
style.Font.Name = "Verdana";
style.Font.Name = "Times New Roman";
style.Font.Size = 8;

// Create a new style called Reference based on style Normal
style = document.Styles.AddStyle("Reference", "Normal");
style.ParagraphFormat.SpaceBefore = "5mm";
style.ParagraphFormat.SpaceAfter = "5mm";
style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
style.ParagraphFormat.Font.Size = 8;

How can I reduce the space between the image and the top of the page?

user1526912
  • 15,818
  • 14
  • 57
  • 92

3 Answers3

2

Set image.WrapFormat.DistanceTop to set the top position of the image. If you set the image position this way, there is no need to modify the PageSetup.

There are different values for RelativeVertical that can be used. And you can also use negative values.

See also:
http://forum.pdfsharp.net/viewtopic.php?p=5267#p5267

With respect to the other answer: it is good practice not to modify DefaultPageSetup. Instead make a Clone() of DefaultPageSetup and modify that. The PageSetup of your documentis specific to your document. The DefaultPageSetup is shared by all documents; modifying it can lead to strange effects when you persist documents in MDDDL format or when your application has different documents with different page setups.

Code that creates a clone can look like this:

var doc = new Document();
var section = doc.AddSection();
section.PageSetup = doc.DefaultPageSetup.Clone();

Then you can make all necessary changes to section.PageSetup. If you need different settings for other sections, you can use either doc.DefaultPageSetup.Clone() or section.PageSetup.Clone() to get started.

1

something like this

document.DefaultPageSetup.LeftMargin = MigraDoc.DocumentObjectModel.Unit.FromCentimeter(.8);
        document.DefaultPageSetup.TopMargin = MigraDoc.DocumentObjectModel.Unit.FromCentimeter(.5);
        document.DefaultPageSetup.PageWidth = MigraDoc.DocumentObjectModel.Unit.FromCentimeter(11);
user3357141
  • 199
  • 1
  • 3
  • 16
1

My solution to this involved having the image in my header section of the document, and controlling the position within the header by enclosing it in a borderless table.

Actual header and footer sections of the document are not effected by the margins applied to the document, they have their own Unit properties. There is a HeaderDistance property in the PageSetup object of the document. Same thing exists for Footers (FooterDistance).

https://forum.pdfsharp.net/viewtopic.php?f=2&t=3076

document.DefaultPageSetup.HeaderDistance = "0.20in";
document.DefaultPageSetup.FooterDistance = "0.20in";

Although other answers say to not modify DefaultPageSetup directly, it is a read-only variable in the document object, so you cannot set it equal to a clone. This is the best way.

MeanJerry
  • 79
  • 6
  • Modify the `PageSetup` of your document, do not modify the `DefaultPageSetup`. The former is specific to your document and can be set, the latter is shared by all documents. – I liked the old Stack Overflow Jan 17 '19 at 17:25
  • Interesting information, but not related to the question. – I liked the old Stack Overflow Jan 17 '19 at 17:30
  • I came upon this question looking for an answer to my document's image being too far down with a margin that was not being affected by the default margin settings. My image was in the actual header area of the document, and this is the way I was able to solve reducing the top margin of the document, as well as shift an image up. – MeanJerry Jan 17 '19 at 20:58