Create a paragraph using the java sdk

Hello,

I’m struggling to perform a simple task: creating a paragraph using libreoffice java sdk.
I have a big document with a special tag in it and I need to put a complex structure in place of this tag.
I already managed to replace the tag with some text using insertString method.

Now I need to put format this text with different style.
I managed to create a style and apply it to the whole textRange which is the tag.

Now I’m clueless as how I can create several paragraph of text with a different styling for each paragraph.

Regards,

This is part of sdk\examples\java\Text\HardFormatting.java:

        String sMyText = "A very short paragraph for illustration only";
        
        // you can travel with the cursor throught the text document.
        // you travel only at the model, not at the view. The cursor that you can
        // see on the document doesn't change the position
        com.sun.star.text.XTextCursor xTextCursor = null;
        xTextCursor = (com.sun.star.text.XTextCursor)
            xTextDocument.getText().createTextCursor();
        
        xText.insertString( xTextCursor, "Headline", false );
        xText.insertControlCharacter(xTextCursor,
                  com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
        
        xText.insertString(xTextCursor, sMyText, false);

So
.insertControlCharacter(xTextCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);

Thanks for the quick reply.
That’s the method I used for adding the text. But there’s no styling involved here.
I need to add the text and apply a specific style for this text only.

Oh! It’s in another file - StyleCreation.java :

xParagraphPropertySet.setPropertyValue(“ParaStyleName”, new String( “myheading” ) );

thanks, this part I have done already. I can apply one style to the whole text I add. I need to apply different styles to the newly inserted texts

maybe I can rephrase my question how can I have a textRange for the text I just added, so I can apply the style ?

Added text AND PARAGRAPH_BREAK behind it? In this case cursor .gotoPreviousParagraph(True) (select previous paragraph) and .paraStyleName = styleName (applay style to selection)

it looks like what I was missing. it’s my day off, I’ll try your solution next week.

Thanks to @JohnSun I managed to come up with a solution.
Assuming you know how to have your text and cursor, here’s the method I developed to add a paragraph with some style.

	private void addParagraph(com.sun.star.text.XSimpleText xText, com.sun.star.text.XParagraphCursor cursor, String paragraph, String style)
		throws com.sun.star.lang.IllegalArgumentException, com.sun.star.uno.Exception{
	xText.insertString(cursor, paragraph, false);
	xText.insertControlCharacter(cursor, ControlCharacter.PARAGRAPH_BREAK, false);

	// Select the paragraph who was juste created
	cursor.gotoPreviousParagraph(true);
	// get the XPropertySet of the paragraph to set its style
	XPropertySet xParagraphPropertySet = UnoRuntime.queryInterface(XPropertySet.class, cursor);
	xParagraphPropertySet.setPropertyValue("ParaStyleName", style);

	cursor.gotoNextParagraph(false);
}

The trick to manipulating the LibreOffice API is to use a cursor as if you were typing some text yourself in the GUI.
I also used this online book which have many more examples : http://fivedots.coe.psu.ac.th/~ad/jlop/index.html

very nice. thank you for sharing the book. It is amazing.