Page 1 of 1

[Solved] Add text and formatting using Basic

Posted: Sat Jan 06, 2024 7:38 pm
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!

Re: Add text and formatting using Basic

Posted: Sat Jan 06, 2024 7:43 pm
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.

Re: Add text and formatting using Basic

Posted: Sat Jan 06, 2024 7:45 pm
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.

Re: Add text and formatting using Basic

Posted: Sat Jan 06, 2024 7:49 pm
by JeJe

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 1:34 am
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!

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 1:44 am
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

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 4:04 pm
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

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 4:20 pm
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

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 4:43 pm
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?

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 4:47 pm
by JeJe

Re: Add text and formatting using Basic

Posted: Sun Jan 07, 2024 5:08 pm
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... ;-)