Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

migradoc

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?

like image 845
user1526912 Avatar asked Aug 22 '14 16:08

user1526912


3 Answers

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.

like image 147
I liked the old Stack Overflow Avatar answered Oct 17 '22 12:10

I liked the old Stack Overflow


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);
like image 25
user3357141 Avatar answered Oct 17 '22 14:10

user3357141


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.

like image 1
MeanJerry Avatar answered Oct 17 '22 12:10

MeanJerry