[Solved] How to get the formatted text from foot/end notes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
floris v
Volunteer
Posts: 4431
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

[Solved] How to get the formatted text from foot/end notes

Post by floris v »

I'm trying to write a macro that will convert footnotes and endnotes to plain text, but so far I'm unable to find how to get the formatted text from the uno footnotes interface; the text attribute of a list item just yields the unformatted text. So far I have this (not attempting to make things look clean)

Code: Select all

Sub footnotestotext
dim allNotes as object, aNote as object
dim x as long
dim args3(0) as new com.sun.star.beans.PropertyValue
dim document   as object
dim dispatcher as object

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
allNotes= thisComponent.FootNotes

for x = 0 to allNotes.Count -1
  aNote = allNotes.getByIndex(x)
  args3(0).Name = "Text"
  args3(0).Value = str(x + 1) + ". " + aNote.String // this is the troublesome part; the string attribute (and lucky to find that attribute name  :twisted: ) only gets the unformatted text.
  dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
  dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())
 next
End Sub
Last edited by floris v on Mon Apr 28, 2008 6:03 pm, edited 1 time in total.
OpenOffice 4.1.11 on Ubuntu; LibreOffice 6.4 on Linux Mint, LibreOffice 7.6.2.1 on Ubuntu
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to get the formatted text from foot/end notes

Post by Villeroy »

I already solved a similar problem here: Formatted cell-text Calc2Writer.
The following code copies the footnotes and endnotes to the end of the text body. Footnotes first, then endnotes, separated by paragraph marks. For now it supports formatted plain text only, fields and such are copied as text.

Code: Select all

REM  *****  BASIC  *****
Option Explicit
Sub Main
Dim oCollection, oCursor
	oCollection = thisComponent.FootNotes
	oCursor = thisComponent.Text.createTextCursor()
	processNotes(oCollection, oCursor)
	oCollection = thisComponent.EndNotes
	processNotes(oCollection, oCursor)
End Sub

Sub processNotes(oCollection, oCursor)
Dim i%, oNote
	for i = 0 to oCollection.getCount() -1
		oCursor.gotoEnd(False)
		oNote = oCollection.getByIndex(i)
		processNextNote(oNote, oCursor)
		oCursor.Text.insertControlCharacter(oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
	next
End Sub

Sub processNextNote(oNote, oCursor)
Dim XText, eParas, para, eRanges, rg
	XText = oCursor.getEnd(True)
	REM enumerate paragraphs in note
	eParas = oNote.createEnumeration()
	while eParas.hasMoreElements()
		para = eParas.nextElement()
		REM enumerate formatted portions in paragraph
		eRanges = para.createEnumeration
		while eRanges.hasMoreElements()
			rg = eRanges.nextElement()
			XText.setString(rg.getString)
			cloneProperties rg, XText
			oCursor.collapseToEnd()
			XText = oCursor.getEnd(true)
		wend
		if eParas.hasMoreElements then
			oCursor.Text.insertControlCharacter(oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
			oCursor.collapseToEnd()
			XText = oCursor.getEnd(true)
		endif
	wend
End Sub

REM (C) Stephan Wunderlich
REM Archived-At: <http://permalink.gmane.org/gmane.comp.openoffice.devel.api/14674>
function cloneProperties(original, clone)
Dim properties, i, aName$, aValue
On Error resume next
properties = original.getPropertySetInfo.getProperties()
for i=0 to UBound(properties)
    aName = properties(i).Name
    aValue = original.getPropertyValue(aName)
    if (NOT isNull(aValue)) AND (NOT isEmpty(aValue)) then
        clone.setPropertyValue(aName,aValue)
    endif
next
end function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
floris v
Volunteer
Posts: 4431
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: How to get the formatted text from foot/end notes

Post by floris v »

Yes, that works! Great.
Now I'll just add the code to insert a note number as well. Just to insert

Code: Select all

mid (str(i + 1), 2) + ". "
I seem to have to write a novel to achieve that. Is code like
args3(0).Name = "Text"
args3(0).Value = mid (str(i + 1), 2) + ". "
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())
really necessary or am I getting my leg pulled? (Note that I didn't quote the declarations.)
OpenOffice 4.1.11 on Ubuntu; LibreOffice 6.4 on Linux Mint, LibreOffice 7.6.2.1 on Ubuntu
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to get the formatted text from foot/end notes

Post by Villeroy »

Well, there is no easy way to do anything in this API and recorded dispatcher methods use to fall short. I could find some more efficient interfaces to append paragraphs and transfer formatted stuff.
There is some internal numbering oNote.getReferenceId() which does not correspond with the displayed numbering. There is oNote.getLabel() without number. There are two services
FootnoteSettings and EndnoteSettings. They comprise document specific settings for the notes. The services don't help to read the actual numbers. If footnotes re-start their numbering on every page they may be a moving target while running a macro that inserts lines. Anyway, I implemented a simple consecutive, numbering from 1 to notes.count. The numbers may or may not be in sync with the displayed numbers of the foot/endnotes.

Code: Select all

REM  *****  BASIC  *****
Option Explicit
Sub Main
Dim oView, XText, oCollection
	oView = thisComponent.getCurrentController()
	XText = thisComponent.getText()
	oCollection = thisComponent.FootNotes
	processNotes(oView, oCollection, XText)
	oCollection = thisComponent.EndNotes
	processNotes(oView, oCollection, XText)
End Sub

Sub processNotes(oView, oCollection, XText)
Dim i%, oNote, XTrans
	for i = 0 to oCollection.getCount() -1
REM copy the note content (does not include the prefix number)
		oNote = oCollection.getByIndex(i)
		oView.select(oNote.getText())
		XTrans = oView.getTransferable()

REM append a new paragraph to the passed text and set it's style
		XText = XText.Text.appendParagraph(Array())
		XText.ParaStyleName = oNote.Start.ParaStyleName
		
REM add the number & ". "
		XText.setString(cStr(i +1)&". ")
		
REM paste the transferable content
		oView.select(XText.getEnd(False))
		oView.insertTransferable(XTrans)
	next
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
floris v
Volunteer
Posts: 4431
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: How to get the formatted text from foot/end notes

Post by floris v »

Thank you very much. :D The code is almost completely incomprehensible to me, but it works just great. :?
Well, there is no easy way to do anything in this API
:lol: :Relieved: So it wasn't entirely my fault that I felt I was working my way through plastic text, trying to make sense of the documentation in the SDK.

edit: Added [Solved] to the subject line.
OpenOffice 4.1.11 on Ubuntu; LibreOffice 6.4 on Linux Mint, LibreOffice 7.6.2.1 on Ubuntu
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
Post Reply