Create a paragraph using the java sdk


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.


This is part of sdk\examples\java\Text\

        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 xTextCursor = null;
        xTextCursor = (
        xText.insertString( xTextCursor, "Headline", false );
        , false);
        xText.insertString(xTextCursor, sMyText, false);

.insertControlCharacter(xTextCursor,, 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 - :

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( xText, cursor, String paragraph, String style)
	xText.insertString(cursor, paragraph, false);
	xText.insertControlCharacter(cursor, ControlCharacter.PARAGRAPH_BREAK, false);

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


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 :

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