EvotecIT / PSWriteOffice

Experimental PowerShell Module to create and edit Microsoft Word, Microsoft Excel, and Microsoft PowerPoint documents without having Microsoft Office installed.
110 stars 9 forks source link

Set margins size #11

Closed v-bulynkin closed 1 year ago

v-bulynkin commented 1 year ago

Hello, how to set margins?

I try

$doc.Margins.Left = 1

but it doesn't work.

Can I set margins preset like 'Narrow'?

PrzemyslawKlys commented 1 year ago
document.Sections[0].SetMargins(WordMargin.Normal);

Sections[0] is basically the first section and it always exists.

image

document.Margins.Type = WordMargin.Narrow;

Should also work I guess. Also keep in mind that the values are kind of much larger.

image

v-bulynkin commented 1 year ago

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.

PrzemyslawKlys commented 1 year ago

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

image

I am not sure you really want 5 cm?

image

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 ;)

v-bulynkin commented 1 year ago

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
PrzemyslawKlys commented 1 year ago

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 :)

v-bulynkin commented 1 year ago

It will probably demand to read from operating system's regional parameters. In the end, you'll write the second Word but for Powershell :)

v-bulynkin commented 1 year ago

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]
    }
}
PrzemyslawKlys commented 1 year ago

It has to be a bit more static. What if you as a developer are given must be 3 inches, but live in Europe.

v-bulynkin commented 1 year ago
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!"
    }
}