[Solved] Line Spacing in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

[Solved] Line Spacing in Writer

Post by Cogito ergo sum »

Can someone please help with this?

Code: Select all

' =========================
' Line Spacing.vbs
' =========================

 Dim arg()

  Input1 = "I want this text written in larger Segoe Font characters and to be blue, " &_
  "and the line spacing to be normal single line. " 

  Input2 = "This text will be written in italic with the Cambria font and in a size usual " &_
  "for printed text, but with expanded (double) line spacing. The colour is a faded reddish " &_
  "sandy brown. The problem here seems to be that to adjust line spacing you need to " &_
  "control positioning  of the view cursor because line spacing is a property of paragraph " &_
  "styles, the creation of which seems to require application of the view cursor, but " &_
  "bizarrely the view cursor  ""knows nothing""  about which paragraph it is currently in; " &_
  "this is apparently ""known"" only to the text cursor !!! So what happens when I try to " &_
  "adjust line spacing is that all paragraphs get adjusted instead of this  particular one. " &_
  "I think that what I may be lacking is a method to create extra paragraph stylenames " &_
  "as the one I'm using seems to behave as a default, so I then could apply specific style " &_
  "names to  specific paragraphs."

  Input3 = "The final text will be written in smaller characters with the Segoe UI Semibold font " &_
  "and coloured in a minty blueish-green. Line spacing here should return to normal" 

  Set oSM = CreateObject ("com.sun.star.ServiceManager")
  Set oDsk = oSM.createInstance(_
  "com.sun.star.frame.Desktop")

  Set oDoc = oDsk.loadComponentFromURL( _
  "private:factory/swriter", "_blank", 0, arg)

  Set oViewCursor = oDoc.CurrentController.getViewCursor()
  Set oParaStyles = oDoc.StyleFamilies.getByName("ParagraphStyles")
  oParaStyleName = oViewCursor.ParaStyleName
  Set oParaStyle = oParaStyles.getByName(oParaStyleName)

  Set objText= oDoc.getText
  Set objCursor= objText.createTextCursor
  objCursor.CharHeight="15"
  objCursor.CharColor = RGB(185,70,45)
  objCursor.CharFontName="Segoe Print"
  objText.insertString objCursor, Input1 , false
  objCursor.gotoEndOfParagraph False 

  objText.insertControlCharacter oViewCursor, 0, False 
  objText.insertControlCharacter oViewCursor, 0, False 

  objCursor.CharHeight="13"
  objCursor.CharPosture=1
  objCursor.CharColor = RGB(10,110,205)
  objCursor.CharFontName="Cambria"
  objText.insertString objCursor, Input2 , false
  oViewCursor.gotoRange objCursor, False

  Set v = oParaStyle.ParaLineSpacing
  v.height = 200
  oParaStyle.ParaLineSpacing = v
  objCursor.gotoEndOfParagraph False 

  objText.insertControlCharacter oViewCursor, 0, False 
  objText.insertControlCharacter oViewCursor, 0, False 

  objCursor.CharHeight="10"
  objCursor.CharPosture=0
  objCursor.CharColor = RGB(180,155,20)
  objCursor.CharFontName="Segoe UI Semibold"
  objText.insertString objCursor, Input3 , false
Last edited by robleyd on Mon May 14, 2018 8:19 am, edited 1 time in total.
Reason: Tagged [Solved] [robleyd, Moderator]
OpenOffice 5.3.6.1 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Post by Zizi64 »

My first tip is:
Use the Styles (the Paragraph styles in this case) instead of the direct formatting properties.

You can predefine them in a template file, or you can create and apply a new paragraph style by macro too.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Post by Cogito ergo sum »

I don't think I'd know how to do that. Can you demonstrate in code how?
OpenOffice 5.3.6.1 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Post by Zizi64 »

My Second tip is:
Download, install, and use one of the object inspection tools like the MRI and the XrayTool. They can list all of the properties, methods, interfaces ... etc of the programming objects.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Post by Zizi64 »

viewtopic.php?f=45&t=28012
viewtopic.php?f=20&t=77563
viewtopic.php?f=20&t=76871

My third tip is:
Download, and study the Andrew Pitonyak's free macro books.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Line Spacing in Writer

Post by JeJe »

Instead of using objtext.insertstring use

objCursor.String = Input2

the range of the text cursor will then cover Input2 and you can apply your attributes

objCursor.ParaLineSpacing = v
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Post by Cogito ergo sum »

Magic. THank you so much!
OpenOffice 5.3.6.1 on Windows 7
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Line Spacing in Writer

Post by JeJe »

Here's an example of creating a custom style with the attrubutes you want and applying it.
As Zizi64 said - its usually the best way.

Code: Select all


newstylename = "NewStyle" 'name for style
vStyle =ThisComponent.createInstance("com.sun.star.style.ParagraphStyle") 'get a style instance

with vstyle 'set properties
Set pspacing = .ParaLineSpacing
  pspacing.height = 200
 .ParaLineSpacing =  pspacing
 .CharFontName="Cambria"
end with

ThisComponent.StyleFamilies.getByName("ParagraphStyles").insertByName(newstylename,vStyle) 'add style to document

'set the style in the document with the viewcursor or a textcursor
oVC = thisComponent.getCurrentController.getViewCursor 
oVC.parastylename = newstylename
for i = 0 to 100
st =st & "In new style "
next
oVC.string =st


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Post by Cogito ergo sum »

Problem solved - many thanks!
OpenOffice 5.3.6.1 on Windows 7
Post Reply