This project has moved and is read-only. For the latest updates, please go here.

How to set the Page top margin to less than 1"

Apr 8, 2013 at 10:57 PM
Is there a way to change the page layout top margin to a custom value example .25"
Apr 8, 2013 at 11:06 PM
Nevermind i found the answer:

SectionProperties sectionProps = new SectionProperties();
PageMargin pageMargin = new PageMargin() { Top = 1008, Right = (UInt32Value)1008U, Bottom = 1008, Left = (UInt32Value)1008U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
sectionProps.Append(pageMargin);
mainPart.Document.Body.Append(sectionProps);
Jul 4, 2013 at 12:33 PM
Is there a way to give the margin in millimeters (or in inches, for americans)? I find it quite unusable to use those UInt32Value as units...

For now what came in my mind is to build my own helper method to convert mm to UInt32 but it would be cool to have an implementation directly in this library...
Coordinator
Jul 4, 2013 at 1:27 PM
can't you use the Unit class ?
There is some conversion utilities but yes the class is private so you would need to tweak the library yourself.
Jul 4, 2013 at 3:07 PM
I couldn't find a converter so I created one (actually 2 because top and bottom use int and left and right UInt32):
        /// <summary>
        /// Converts millimeters to int
        /// </summary>
        /// <param name="mm">millimeters</param>
        /// <returns>int</returns>
        private int mmToInt(int mm)
        {
            double multiplier = 5.67;

            double _int = Convert.ToDouble(mm) * multiplier;

            return Convert.ToInt32(Math.Round(_int));
        }

        /// <summary>
        /// Converts millimeters to UInt32
        /// </summary>
        /// <param name="mm">millimeters</param>
        /// <returns>UInt32</returns>
        private UInt32 mmToUInt32(int mm)
        {
            return Convert.ToUInt32(mmToInt(mm));
        }
And how I use it, like in advienr post (without gutter, header, footer):
PageMargin pageMargin = new PageMargin() { Top = mmToInt(475), Right = mmToUInt32(200), Bottom = mmToInt(50), Left = mmToUInt32(230) };
I guess (my humble suggestion) it would be easier/a bit more elegant to have e.g. a "unit" property in the PageMargin object and let the developer use integers for top, right, bottom, left, header, footer, gutter, like this:
PageMargin pageMargin = new PageMargin() { Unit = UnitMetric.Millimeter, Top = 475, Right = 200, Bottom = 50, Left = 230 };
My converter works (for me) but as said before would be cool to support inches, points, etc. (the units supported by OpenXML). Would be cool if you consider this as one of the future improvements...

Thanks again and cheers
Coordinator
Jul 4, 2013 at 3:54 PM
I'm talking about this class: Unit.cs
left = new Unit(UnitMetric.Millimeter, 475).ValueInDxa;

Dxa = twentieths of a point and is the unit expected according to MSDN.
Jul 4, 2013 at 4:08 PM
Cool, didn't know that class ;)

...and now I see that PageMargin is not in your library but in the OpenXML library... so you can't change it.

Thank you for sharing the info and again for this awesome library, using just "pure" OpenXML is quite a pain... ;)