[Solved] Add text and formatting using Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

[Solved] Add text and formatting using Basic

Post by D.Bugger »

Hi,

There's this application layer that I developed, it is in fact a document generator that combines a document template and data from an external database. It works as follows:
- an application (not necessarily developed by me) calls my layer and requests a template to be used
- the request is accompanied by a dataset
- the layer has formulas that put specific data (from the dataset) at specific places (by means of bookmarks)
- the basic text and formatting are contained in a file, e.g. ACME-letter.odt
- at the end of the dataset, the document is automatically converted to a PDF and exported

That's the current situation, using getAnchor() and setText() in my code.

I would like to give the other developers, the users of this layer, more features. For instance, inserted text uses the style present at the bookmark. A simple extension could be writing bold or underlined text, maybe later to be followed by other font changes.

Ideally, I'd like to have a function that converts simple html and adds texts with formatting. Or some other means of markup (bbcode?), but the markup has to be inside the dataset.

Writing a long list of substitutions might be a solution, substituting e.g. <b>bold</b> to bold, but I'd prefer some code that:
- searches a bookmark
- adds text
- changes styles or formatting on-the-fly

Is there such a function, or how can I develop it myself? Any examples?

TIA!

D.Bugger

PS Prosperous 2024!
Last edited by D.Bugger on Sun Jan 07, 2024 5:12 pm, edited 2 times in total.
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add text and formatting using Basic

Post by JeJe »

Download Pitonyak's books as a starting point.

https://www.pitonyak.org/oo.php

And search here for what you want to do using the search box. Someone has likely done what you want to do before and there will likely be a thread.
Last edited by JeJe on Sat Jan 06, 2024 7:46 pm, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Add text and formatting using Basic

Post by D.Bugger »

Done that a long time ago, I used it to develop the current version. What I cannot find in there is a simple way to insert text and formatting... and I found no examples anywhere close to what I want on the internet.
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add text and formatting using Basic

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Add text and formatting using Basic

Post by D.Bugger »

Ah, thank you! Indeed, I already use a textCursor when importing text from a file, but what I really need is formatting while adding text. How do I do that best? Should I add the word first, then select it and change its properties, or can I set a cursor, set some properties, add text and then reset those properties? Or should I work with a TextRange object, as in https://wiki.openoffice.org/wiki/Writer/API/TextRange ? I'll try those methods tomorrow, but if you have a better idea, or some hints as to how it's supposed to be done, I'd be much obliged!
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add text and formatting using Basic

Post by JeJe »

With the viewcursor

Code: Select all

vc = thiscomponent.currentcontroller.viewcursor
with vc
.string = "blah blah"
.charweight = 200
end with

with a text cursor going to the start of the text the viewcursor is in

Code: Select all

vc = thiscomponent.currentcontroller.viewcursor
tc = vc.text.createtextcursorbyrange(vc.text.getstart)
with tc
.string = "blah blah"
.charweight = 200
end with
You may find MRI helpful to look at the properties of the view and text cursor objects

viewtopic.php?t=49294

The API for character properties

https://www.openoffice.org/api/docs/com ... rties.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Add text and formatting using Basic

Post by D.Bugger »

After having lost my bookmark several times, and quite a few improvements, I now got the following code that basically does what I want:

Code: Select all

sub addSomeTextAtBM1
	dim document   as object
	dim bookmark as object
	dim cursor as object
	
	document   = ThisComponent
	bookmark= document.getBookmarks().getByName("BM1")
	cursor= document.getText().createTextCursorByRange(bookmark.getAnchor())
	cursor.setString("normal ")
	cursor.gotoEnd(true)
	cursor.charweight= 200
	cursor.setString("bold")
	cursor.gotoEnd(true)
	cursor.setString(" normal")
	cursor.charweight= 100
end sub
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add text and formatting using Basic

Post by JeJe »

These are the font weight constants. Note BOLD is 150% font weight.

https://www.openoffice.org/api/docs/com ... eight.html

You use the constants like this

Code: Select all

.charweight = com.sun.star.awt.FontWeight.BOLD ' or .SEMIBOLD or whatever
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Add text and formatting using Basic

Post by D.Bugger »

I'm not exactly using BASIC, I use the COM interface to produce documents using LotusScript, it might be OLE, anyway there are no predefined constants.
Unfortunately, the code above doesn't quite behave: when there is text after the bookmark, the calls to gotoEnd remove that text, or they position the cursor at the end of the text, instead of at the end of the words that were just inserted. How can I have a cursor that moves with the text?
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add text and formatting using Basic

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Add text and formatting using Basic

Post by D.Bugger »

From that last page:

collapseToEnd sets the start of the position to the end.

Neither the name of the method nor its description couldn't help me understand that the final position of the cursor would be at the end of the text that was just inserted. Thanks for clearing that up!!

Anyway, the current code seems to work:

Code: Select all

sub addSomeTextAtBM1
	dim document   as object
	dim bookmark as object
	dim cursor as object
	
	document   = ThisComponent
	bookmark= document.getBookmarks().getByName("BM1")
	cursor= document.getText().createTextCursorByRange(bookmark.getAnchor())
	cursor.setString("normal ")
	cursor.collapseToEnd(false)
	cursor.setString("bold")
	cursor.charweight= 200
	cursor.collapseToEnd(false)
	cursor.setString(" normal")
	cursor.charweight= 100
' 	MsgBox Cursor.DBG_methods
end sub
Solved! Until the next question... ;-)
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
Post Reply