Ask Your Question

Detecting when the page changes

asked 2017-05-15 00:39:08 +0200

ksten gravatar image

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?

edit retag flag offensive close merge delete



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 @floris_v

librebel gravatar imagelibrebel ( 2017-05-15 01:36:37 +0200 )edit

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.

Mike Kaganski gravatar imageMike Kaganski ( 2017-05-15 10:02:30 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-05-22 05:34:22 +0200

ksten gravatar image

updated 2017-05-22 05:35:22 +0200

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.

edit flag offensive delete link more


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

librebel gravatar imagelibrebel ( 2017-05-22 07:18:41 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-05-15 00:39:08 +0200

Seen: 73 times

Last updated: May 22 '17