PSWriteOffice icon indicating copy to clipboard operation
PSWriteOffice copied to clipboard

Underline spaces/tabs

Open v-bulynkin opened this issue 1 year ago • 12 comments

How to underline spaces or tabs? When I set

$p2 = New-OfficeWordText -Document $doc -Bold 1,0 -Underline 0,1 -Text `
"We are ","`t`tJohn Doe and Jane Doe`t`t" -ReturnObject

The result is изображение

But I'd like to have изображение

v-bulynkin avatar Mar 10 '23 09:03 v-bulynkin

I believe Tabs are TabStops and require different approach.

  • TabStops are implemented in the development version of OfficeIMO https://github.com/EvotecIT/OfficeIMO/issues/89

The way I think it would work one would have to change tab stops per paragraph and change TabStopLeaderCharValue to match underline - i think. Alternatively you could just add empty spaces and those will be treated as normal text.

If you can please attach example DOCX with the expectation (minimal example) and I'll check how the XML looks like and whether this is really the case.

PrzemyslawKlys avatar Mar 10 '23 09:03 PrzemyslawKlys

Ok, it's not a TabStop. So it seems TabStops just control behavior of a TabStop, but when you use "\t" it's actually creating a separate run with TabChar.

image

So I need to add support for it in OfficeIMO.

Seeing how it's built

image

You can try doing:

$document.Paragraphs[1].UnderLine = ...

While when you use ReturnObject it will return a WordParagraph, it actually just returns the "last" one. So a complicated sentence like you have created probably 7 WordParagraph. Each on their own...

I only wonder if OfficeIMO will see this..., it should be it will not be aware that it's TabChar

PrzemyslawKlys avatar Mar 10 '23 09:03 PrzemyslawKlys

Hmm, it means that I can underline only the whole paragraph, not a part of a string? "We are" shouldn't be underlined.

Maybe, I can use something like non-breaking space? But how to type it in Powershell?

v-bulynkin avatar Mar 10 '23 10:03 v-bulynkin

I found the workaround! It works when I use

$p2.Underline = 'Single'

Initially, the value of $p2.Underline was Words.

v-bulynkin avatar Mar 10 '23 10:03 v-bulynkin

It's not like that. OpenXML has Paragraph that can contain multiple Runs.

However, in OfficeIMO I've simplified this idea and basically every Run is WordParagraph (different class). That means from a XML level you still have 1 Paragraph that contains 7 Runs, but from OfficeIMO/PSWriteOffice you are dealing with 7 WordParagraphs (not carrying or knowing about Paragraph/Runs). So what you just did with New-OfficeWordText is created 7 WordParagraphs, but we only return the "last" one to you when you use ReturnObject. This is because, the use case is - you can use it to attach other things to it if needed.

But if you want to see what is already there you can just go back and see $Document.Paragraphs[0] to $Document.Paragraphs[7] and modify every single one of them. And again $Document.Paragraphs is actually WordParagraph and not Paragraph.

PrzemyslawKlys avatar Mar 10 '23 10:03 PrzemyslawKlys

Right before Save-OfficeWord cmdlet:

# Set to underline spaces
$doc.Paragraphs |? underline |% {$_.underline = 'Single'}

v-bulynkin avatar Mar 10 '23 10:03 v-bulynkin

I would not go and set it on all tho unless you want to underline everything.

PrzemyslawKlys avatar Mar 10 '23 10:03 PrzemyslawKlys

Why everything? It simply set the underline mode I need only in the paragraphs have underline set. Perhaps I can't figure out the whole thing.

v-bulynkin avatar Mar 10 '23 10:03 v-bulynkin

Maybe I don't understand what you're doing. Anyways - you do you ;) If it works, it works.

PrzemyslawKlys avatar Mar 10 '23 10:03 PrzemyslawKlys

So after some initial testing, it seems that the behavior is not as you described it.

$Document = New-OfficeWord -FilePath $PSScriptRoot\Documents\BasicDocument.docx

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, \t\t very big test ', 'and this should be bold' -Bold $null, $true -Underline Dash, $null

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

$p2 = New-OfficeWordText -Document $Document -Bold 1, 0 -Underline 0, 1 -Text "We are ", "`t`tJohn Doe and Jane Doe`t`t" -ReturnObject

$p3 = New-OfficeWordText -Document $Document -Bold 1, 0 -Underline $null, Dash -Text "We are ", "`t`tJohn Doe and Jane Doe`t`t" -ReturnObject

Save-OfficeWord -Document $Document -Show

image

I have not noticed that you used 0 and 1 instead one of many other possible values.

image

And it seems that if you use proper values, it will act as required. I've also noticed in the document it doesn't have TabChar, but "\t"

image

which is consistent with what you did. So the example you sent me looks like created with Word, where it actually adds TabChar as opposed to \t.

PrzemyslawKlys avatar Mar 11 '23 10:03 PrzemyslawKlys

Of course it was created with Word, because I hadn't been able to do it without Word and I needed to illustrate what I wanted. Dashed underline works, but I need Single, which doesn't.

$Document = New-OfficeWord -FilePath c:\temp\doc.docx

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

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

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

$p2 = New-OfficeWordText -Document $Document -Bold 1, 0 -Underline 0, 1 -Text "We are ", "`t`tJohn Doe and Jane Doe`t`t" -ReturnObject

$p3 = New-OfficeWordText -Document $Document -Bold 1, 0 -Underline $null, Single -Text "We are ", "`t`tJohn Doe and Jane Doe`t`t" -ReturnObject

Save-OfficeWord -Document $Document -Show

изображение

v-bulynkin avatar Mar 12 '23 14:03 v-bulynkin