Detecting when the page changes

I’m using the Java SDK interface to build a writer (text) document. As part of the process, I want to selectively add headers to the page, but not to the first page of a section.

My first thought was to just check the page count as my code adds paragraphs, and then use the point where page count increases to mark the change of a page. Unfortunately, the data from XDocumentProperties.getDocumentStatistics() seems to be generated at load time and never updated. I tried adding a few pagefuls of stuff, but the PageCount remained at 1 and WordCount remained at 0 when I queried it.

    XTextDocument document = createDocumentSomehow();
    XDocumentPropertiesSupplier supplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, document);
    XDocumentProperties properties = supplier.getDocumentProperties();
    NamedValue[] values = properties.getDocumentStatistics();
    ... and then fetch "PageCount" from that

Is there a better way to detect a page changing as my code adds to the document?

Hello ksten, perhaps try via Page Styles first… in defining a “Page Style” you can set the Header ON or OFF for that Page Style. ( menu “Format : Page…” , tab “Header” ).
Also see this excellent Tutorial about Page styles and headers/footers from @anon87010807

Just note that LibreOffice allows to have different header for first page in a page style. So, you don’t even need several styles, just one that is setup correctly.

I found it:

    XModel model = UnoRuntime.queryInterface(XModel.class, document);
    XController controller = model.getCurrentController();
    XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, controller);
    return (int)propertySet.getPropertyValue("PageCount");

There are multiple page count values scattered around, but this is the only one that actually updates as you add things to the document.

I can’t mark this as correct because the silly points system won’t allow it, but this is the right answer.

i upvoted your question, dunno how many points are needed to accept your own answer.