Closed v-bulynkin closed 1 year ago
document.Sections[0].SetMargins(WordMargin.Normal);
Sections[0] is basically the first section and it always exists.
document.Margins.Type = WordMargin.Narrow;
Should also work I guess. Also keep in mind that the values are kind of much larger.
Thank you!
Those works both in Powershell:
$doc.Margins.Type = 'Narrow'
# or
$doc.Sections[0].SetMargins('Narrow')
And how could I set only left margin, for example, 5cm?
$doc.Sections[0].SetMargins(5)
works, but I can't understand its logic because no one margin is not equal 5 afterwards.
I'm sorry for asking so many questions but there's no much information and examples in the OfficeIMO repository are not Powershell ones so it's difficult for me to learn from it.
Don't worry. I don't even understand half of the stuff I wrote and even more how to understand the numbers.
According to docs the values are in twips or twentieths of a point
I am not sure you really want 5 cm?
Probably in OfficeIMO 2.0 someone will have to sit down and add some methods that allow setting margins and other things using similar way you can do it in Word. I was a bit too lazy to try and do it right away, especially that I am not fully sure if what I am doing makes sense ;)
Works with this crutch:
function ConvertTo-Twips {
param($cm)
$cm*567 -as [uint32]
}
$docFile = "C:\temp\doc.docx"
rm $docFile
$doc = New-OfficeWord -FilePath $docFile
$doc.PageSettings.PageSize = 'A4'
$doc.Settings.FontFamily = 'Times New Roman'
$doc.Settings.FontFamilyHighAnsi = 'Times New Roman'
$doc.Settings.FontSize = 11
$doc.Sections[0].Margins.Left = ConvertTo-Twips 5
$doc.Sections[0].Margins.Top = ConvertTo-Twips 7
New-OfficeWordText -Document $doc -Text "Hello world"
Save-OfficeWord -Document $doc -Show
Cool, I'll see if this could be natively implemented to work with CM, but then I guess some people will want inches... and it never ends :)
It will probably demand to read from operating system's regional parameters. In the end, you'll write the second Word but for Powershell :)
Autodetect
function ConvertTo-Twips {
param($num)
if ((New-Object System.Globalization.RegionInfo (get-culture).name).IsMetric) {
$num*567 -as [uint32]
}
else {
$num*1440 -as [uint32]
}
}
It has to be a bit more static. What if you as a developer are given must be 3 inches, but live in Europe.
function ConvertTo-Twips {
param($num)
if ($num -match 'cm$') {
(($num -replace '\D') -as [uint32])*567
}
elseif ($num -match 'in$') {
(($num -replace '\D') -as [uint32])*1440
}
else {
Write-Host -fore red "Wrong units!"
}
}
Hello, how to set margins?
I try
but it doesn't work.
Can I set margins preset like 'Narrow'?