[Solved] Applying a new style to a page in Writer using Java code

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
svetlanaSG
Posts: 3
Joined: Tue Mar 19, 2024 7:07 am

[Solved] Applying a new style to a page in Writer using Java code

Post by svetlanaSG »

I am trying to set a new style to the second page in order to have different footers with barcode on each page.
In the documentation I found:
The PageStyle is set at the current text cursor position by setting the property PageDescName to an existing page style name.This will insert a new page that uses the new page style. If no new page should be inserted, the cursor has to be at the beginning of the first paragraph.
But the next code doesn't work:

Code: Select all

XTextDocument xTextDocument = (com.sun.star.text.XTextDocument)
    UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xComponent);
XController xController = xTextDocument.getCurrentController();
XTextViewCursorSupplier xViewCursorSupplier = UnoRuntime.queryInterface(
      XTextViewCursorSupplier.class, xController);
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
XPageCursor xPageCursor = UnoRuntime.queryInterface(XPageCursor.class, xViewCursor);
xPageCursor.jumpToFirstPage();
xPageCursor.jumpToNextPage();
xPageCursor.jumpToStartOfPage();
xPageCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xPageCursor);
xPageCursorProps.setPropertyValue("PageDescName", "Style2");
Maybe I am using incorrect objects..
I

Also I found example on Basic, but I don't know how to translate it to Java:

Code: Select all

sub Page_styles_convert1

dim document   as object
dim dispatcher as object

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

dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Template"
args1(0).Value = "Beige_Default"
args1(1).Name = "Family"
args1(1).Value = 8

For i = 0 To 276
	dispatcher.executeDispatch(document, ".uno:StyleApply", "", 0, args1())
	dispatcher.executeDispatch(document, ".uno:GoToNextPage", "", 0, Array())
Next

end sub
Any help would be appreciated. Thanks.
Last edited by svetlanaSG on Tue Apr 02, 2024 11:26 am, edited 1 time in total.
LibreOffice 7.5.6.2 on AstraLinux
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Applying a new style to a page in Writer using Java code

Post by JeJe »

This creates a new page style and then sets the viewcursor pagestyle to it in Basic

Code: Select all


with  thiscomponent.stylefamilies.getbyname("PageStyles")
ps = thiscomponent.CreateInstance("com.sun.star.style.PageStyle")
.insertbyname "MyNewStyleName",ps
end with

thiscomponent.currentcontroller.viewcursor.pageDescname = "MyNewStyleName"
To help translate to Java, you can use MRI, The following is what I get from MRI on thiscomponent and clicking through currentcontroller and viewcursor to PageDescName

Code: Select all


import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XController;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XController xController = oInitialTarget.getCurrentController();
		
		XTextViewCursorSupplier xTextViewCursorSupplier = UnoRuntime.queryInterface(
			XTextViewCursorSupplier.class, xController);
		XTextViewCursor xTextViewCursor = xTextViewCursorSupplier.getViewCursor();
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xTextViewCursor);
		String sPageDescName = AnyConverter.toString(xPropSet.getPropertyValue("PageDescName"));
		
	}
	catch (WrappedTargetException e1)
	{
		// getPropertyValue
		e1.printStackTrace();
	}
	catch (IllegalArgumentException e2)
	{
		// 
		e2.printStackTrace();
	}
	catch (UnknownPropertyException e3)
	{
		// getPropertyValue
		e3.printStackTrace();
	}
}
viewtopic.php?t=49294
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Applying a new style to a page in Writer using Java code

Post by Lupp »

Are you both aware of the fact that page styles aren't applied to single pages, but to the maximum sequence of pages containing the current ViewCursor position (interactive mode) and being delimited at the beginning by either start of docoument or by a page starting with a first paragraph (or respective TextTable) having set the property .PageDescName and ending before the next page of this kind or the end of the document.
If not in interactive mode you need to get the paragraph or TextTable you want to apply a .PageDescName setting to by the running code, of course.
In my mind I call such sequences "streets". Internal hard page breaks not explicitly defining a page style for the next page do not break the street.

Tell me if I'm wrong with the help of an example demonstrating it.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Applying a new style to a page in Writer using Java code

Post by JeJe »

Thanks Lupp. Yeah, page styles are a paragraph property - not very intuitive. My code assumes that the applied page style's next page style will be the desired one - otherwise you'll also need to set the following page's first paragraph's page style to override the style's next page style setting.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
svetlanaSG
Posts: 3
Joined: Tue Mar 19, 2024 7:07 am

Re: Applying a new style to a page in Writer using Java code

Post by svetlanaSG »

Thank you for reply.

In interactive mode I can make different page styles by the Format Footer -> Organizer -> NextStyle. And it is no need any page break, text table or first paragraph.
I attached the file with the example. Do you think is it possible to realize not in interactive mode?
Attachments
Example.odt
(53.57 KiB) Downloaded 13 times
LibreOffice 7.5.6.2 on AstraLinux
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Applying a new style to a page in Writer using Java code

Post by Lupp »

@ svetlanaSG
If your "you" is me: Sorry. I obviously expressed myself not precisely enough.
I didn't want to tell anybody how to "make different page styles".
I thought the ordinary way to create an additional page style is to use the Styles pane (formerly "Stylist"), to select the mode/icon Page Styles, to choose one of the options ('New' e.g.), and to fill info into the tabbed pages of the respective dialog. That there should be a basically different way I neither knew nor can I find it under my V 7.5.4.
The main intention of my post was to describe in what (rather complicated) way the sequence of pages (my "street") to which a changed ("different") page style will be applied, is defined. The process finding this sequence is performed "on the fly" internally if you select the changed page style from the pane (by a DoubleClick supposedly). And, of course, the sequence may consist of just one page in a specific case.
Anyway a textdocument can only be rendered using more than one page style, if each "next" one is separated from the previous one by a hard bage break including the choice of the "next" style (or by a special automatism organizing this). I don't know a way to do so using the UI except going the menu path
>Insert>More Breaks>Manual break ...>>Page break
and then to select the next style (and probably make an additional setting).
What you described as
... by the Format Footer -> Organizer -> NextStyle ...
only reminds me (disregarding the "Format Footer") of the tab "Organizer" used for the definition of a specific page style.
To use this setting actually offers a way to create cycles of equal length of page styles. Entering or exiting such a cycle will again require a special hard page break defining the next page style explicitly. I never worked with such cycles, and therefore I don't think of them mostly.
Anyway, the feature isn't well described as "Applying a new style to a page...". It's "Defining page styles with automatically used successor styles". I don't know how this variant is implemented. (Experimental research needed?) However, though "pages" aren't accessible as objects, PageStyle objects exist, and you can access and change them by user code.

The document has a property StyleFamilies listing the types of styles available for the document type. From there you can get "ByName" the family "PageStyles" which is a container for actual styles providing the needed methods to create/remove/edit them.

Actually I think page styles and the ways they are used in rendering/wrapping text documents is one of the most convoluted and hard to precisely_understand/describe topics concerning Writer.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
floris v
Volunteer
Posts: 4430
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: Applying a new style to a page in Writer using Java code

Post by floris v »

There seems to be a misunderstanding about "making a new page style".
svetlanaSG wrote:In interactive mode I can make different page styles by the Format Footer -> Organizer -> NextStyle. And it is no need any page break, text table or first paragraph.
That is not making a new page style but switching from one page style to another. However, the switch to another page style will always take place on the first page following the first page with a certain page style. See Tutorial on Page styles and headers/footers for the details. Btw, I don't know anything about doing this kind of thing in a macro.
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
svetlanaSG
Posts: 3
Joined: Tue Mar 19, 2024 7:07 am

Re: Applying a new style to a page in Writer using Java code

Post by svetlanaSG »

Thanks for replies. It helps me to do something. I don't understand how, but it seems to be working.
LibreOffice 7.5.6.2 on AstraLinux
Post Reply