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 font name #10

Open v-bulynkin opened 1 year ago

v-bulynkin commented 1 year ago

How to set font of document or paragraph?

I could change font size:

$doc.Settings.FontSize = 15
# or
$paragraph.FontSize = 15

However, I can't set the font itself. The settings below don't work:

$doc.Settings.FontFamily = 'Times New Roman'
# or
$paragraph.FontFamily = 'Times New Roman'
PrzemyslawKlys commented 1 year ago
$Document = New-OfficeWord -FilePath $PSScriptRoot\Documents\BasicDocument.docx

$Document.Settings.FontFamily = 'Times New Roman'

New-OfficeWordText -Document $Document -Text 'This is a test, very big test ', 'and this should be bold' -Bold $null, $true -Underline Dash, $null

New-OfficeWordText -Document $Document -Text 'This is a test, very big test', 'ooops' -Color Blue, Gold -Alignment Right

$Paragraph = New-OfficeWordText -Document $Document -Text 'Centered' -Color Blue, Gold -Alignment Center -ReturnObject

New-OfficeWordText -Document $Document -Text ' Attached to existing paragraph', ' continue' -Paragraph $Paragraph -Color Blue

$Paragraph.FontFamily = 'Arial'

Save-OfficeWord -Document $Document -Show

Works for me?

v-bulynkin commented 1 year ago

It works only with English characters

$docFile = "C:\temp\doc.docx"
rm $docFile
$doc = New-OfficeWord -FilePath $docFile
$doc.Settings.FontFamily = 'Times New Roman'
$doc.Settings.FontSize = 11

$p1 = New-OfficeWordText -Document $doc -Bold $true -Alignment Center -Text `
'Абвг' -ReturnObject
$p1.AddBreak()
New-OfficeWordText -Document $doc -Bold $true -Paragraph $p1 -Text `
"Abcd"

Save-OfficeWord -Document $doc -Show
PrzemyslawKlys commented 1 year ago

Then use FontFamilyHighAnsi

$Document.Settings.FontFamily = 'Times New Roman'
$Document.Settings.FontFamilyHighAnsi = 'Times New Roman'

I think we don't set HighAnsi for paragraph directly which may need fixing in OfficeIMO.

v-bulynkin commented 1 year ago

FontFamilyHighAnsi works, thank you. But there is an interesting thing - spaces, numbers and everything except letters are resistant to font changing.

Default font is Arial, so I have <Word (Times New Roman)><Space (Arial)><Word (Times New Roman)><Space (Arial)><Word (Times New Roman)> etc.

Upd: sorry, the options FontFamilyHighAnsi and FontFamily must be used simultaneously.

PrzemyslawKlys commented 1 year ago

I will change the logic in OfficeIMO and PSWriteOffice and change the logic:

Same for Paragraph FontFamily will set both, and FontFamilyHighAnsi will just set HighAnsi. I believe this is the most common use case where both entries are the same for majority of the people and only in certain languages it will be different so they will have to ensure they run things in proper order.