Hi,
I’m using a macro in order to format text in LibreOffice Writer. Earlier I formatted each paragraph in a number of ways using direct formats, but a while ago I’d thought I’d try using paragraph formats instead since that should be a more flexible/quicker way of doing it. As far as I can see what I’ve got should work - but it doesn’t, so it seems as if I’m missing something (or I’m doing something incorrectly). It’s also possible that the problems I’m having (formatting and saving/opening) aren’t related since they occur at different stages.
There are two parts to the formatting macro. First, I set up all of my paragraph formats using the following code:
document = ThisComponent
styleFamilies = document.StyleFamilies
rem PARAGRAPH STYLES
rem get the paragraph styles
paragraphStyles = styleFamilies.getByName("ParagraphStyles")
rem create the body paragraph styles
rem set properties for the "BODY" style
paragraphStyle = document.createInstance("com.sun.star.style.ParagraphStyle")
paragraphStyle.CharFontName = BODY_FONTNAME
paragraphStyle.CharHeight = BODY_CHARHEIGHT
paragraphStyle.ParaFirstLineIndent = BODY_FIRSTLINEINDENT
LineSpace = paragraphStyle.ParaLineSpacing
LineSpace.height = BODY_LINESPACING15
paragraphStyle.ParaLineSpacing = LineSpace
paragraphStyle.ParaAdjust = com.sun.star.style.ParagraphAdjust.BLOCK
paragraphStyle.ParaOrphans = BODY_ORPHANS
paragraphStyle.ParaWidows = BODY_WIDOWS
paragraphStyle.ParaIsHyphenation = True
paragraphStyle.ParaHyphenationMaxLeadingChars = BODY_HYPHENATIONMAXLEADINGCHARS
paragraphStyle.ParaHyphenationMaxTrailingChars = BODY_HYPHENATIONMAXTRAILINGCHARS
Endif
paraLocale = paragraphStyle.CharLocale
paraLocale.Language = "sv"
paraLocale.Country = "SE"
paragraphStyle.CharLocale = paraLocale
paragraphStyles.insertByName("BODY",paragraphStyle)
etc.
And similarly for all of the other body and header paragraph formats. The values in ALL_CAPS are constants with different values. This works fine, and all of the character formats are inserted correctly into the document.
Once I’ve set up all the formats, I go through the document paragraph by paragraph, selecting the different paragraph formats for each paragraph depending on tags, location etc., formatting using the “BODY” paragraph format above if no other options exists. The code looks something like the following, with all of the options for the other types of paragraphs format removed:
rem step A1: find all *non-empty* lines
SearchDesc = Document.createSearchDescriptor
SearchDesc.SearchRegularExpression = True
rem find all lines
SearchDesc.SearchString = "^.*$"
rem step A3: fix all paragraphs
Found = Document.findFirst(SearchDesc)
rem set up variables
styleFamilies = document.StyleFamilies
paragraphStyles = styleFamilies.getByName("ParagraphStyles")
Do Until IsNull(Found)
rem create a cursor
Cursor = Document.Text.createTextCursorByRange(Found)
Cursor.ParaStyleName = "BODY"
Loop
Problem: None of the settings beginning with “Char…” in the paragraph formats are applied, possibly because of direct formatting in my source document. I can’t do anything about that, though, since the source document is exported “as is” from my writing program.
As a workaround to fix that problem, I changed the Do loop as follows to apply the formats which weren’t applied when applying the paragraph formats:
Do Until IsNull(Found)
rem create a cursor
Cursor = Document.Text.createTextCursorByRange(Found)
Cursor.ParaStyleName = "BODY"
paragraphStyle = paragraphStyles.getByName("BODY")
Cursor.CharFontName = paragraphStyle.CharFontName
Cursor.CharHeight = paragraphStyle.CharHeight
Cursor.CharLocale = paragraphStyle.CharLocale
Loop
After having done that, the formatting actually looks fine. It might be that I have to to this, given that the settings I add “manually” at this point are (possibly) formatted using direct formatting in the exported source file.
Problem: when I save and re-open the document, there are issues both with the formatting and with the saved paragraph formats:
- there is some missing formatting, like indents. If I re-apply the paragraph format for paragraphs, this is fixed
- even though the indentation settings for paragraphs are saved, some other paragraph formats settings are not saved, like widows/orphans, hyphenation settings etc.
Any help would be much appreciated.
Regards,
Anders