Hi,

On Wed, Sep 18, 2013 at 12:29:54PM +0200, Peter Eberlein wrote:
> Hi,
> 
> the service css.text.TextFrame exports the XTextFrame Interface with
> the getText() method.
> The service css.text.TextSection exports the XTextSection Interface,
> but I'm missing a convenient method to get the XText of the section.
> The XTextContent's anchor of the section gives me the XTextRange of
> the parent of the section.

a section does not have an XText of its own, so the anchor of the
section gives you access to its text.

If you want to insert text inside the section right after inserting the 
section, you may have to move the text cursor to the previous paragraph, 
because inserting a section inserts a new empty paragraph and positions the 
cursor in the next paragraph:

REM  *****  BASIC  *****

Option Explicit

Sub Main
Dim oDoc as Object
oDoc = 
StarDesktop.loadComponentFromURL("private:factory/swriter","_default",0,Array())

Dim oText as Object
Dim oCursor as Object
Dim oSection as Object
Dim sSection as String
Dim oSectionText as Object

sSection = "Test section"
oText = oDoc.getText()
oCursor = oText.createTextCursorByRange(oText.getStart())
oSection = oDoc.createInstance("com.sun.star.text.TextSection")
oSection.setName(sSection)

oText.insertString(oCursor, "A new section:", False)
oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,
 False)
oText.insertTextContent(oCursor, oSection, False)

'after inserting the section, the cursor is on the next paragraph
oCursor.gotoPreviousParagraph(False)
oText.insertString(oCursor, "Text inside section", False)

 
> Any ideas? (I don't want to iterate over the paragraphs to obtain
> the section)?

If you know the name of the section, get the section by name; if you
have a reference to the already inserted section, simply create a text
cursor by range, using the section's anchor as parameter:

Dim oSections as Object
oSections = oDoc.getTextSections()
oSection = oSections.getByName(sSection)

'create a text cursor at the section anchor
oText = oSection.getAnchor().getText()
oCursor = oText.createTextCursorByRange(oSection.getAnchor())
oText.insertString(oCursor, "More text inside the section", False)
oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,
 False)


All together:

REM  *****  BASIC  *****

Option Explicit

Sub Main
Dim oDoc as Object
oDoc = 
StarDesktop.loadComponentFromURL("private:factory/swriter","_default",0,Array())

Dim oText as Object
Dim oCursor as Object
Dim oSection as Object
Dim sSection as String
Dim oSectionText as Object

sSection = "Test section"
oText = oDoc.getText()
oCursor = oText.createTextCursorByRange(oText.getStart())
oSection = oDoc.createInstance("com.sun.star.text.TextSection")
oSection.setName(sSection)

oText.insertString(oCursor, "A new section:", False)
oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,
 False)
oText.insertTextContent(oCursor, oSection, False)

'after insterting the section, the cursor is on the next paragraph
oCursor.gotoPreviousParagraph(False)
oText.insertString(oCursor, "Text inside section", False)
    
'Access the section (not needed, as we already have a reference)
'Dim oSections as Object
'oSections = oDoc.getTextSections()
'oSection = oSections.getByName(sSection)

'create a text cursor at the section anchor
oText = oSection.getAnchor().getText()
oCursor = oText.createTextCursorByRange(oSection.getAnchor())
oText.insertString(oCursor, "More text inside the section", False)
oText.insertControlCharacter(oCursor, 
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)


End Sub


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina

Attachment: pgpptS4iSDphR.pgp
Description: PGP signature

Reply via email to