OO-Snippets: Add Page number

Commons

Keywordspage number
LanguageJava
ApplicationWriter
AuthorsStefan Wunderlich
Tom Schindl
Supported Versions
Supported OS
Question How do I add a page number to the header I need to put page number in the footer of each page of a writer document by using java UNO api. From now, I'm able to set text into the headerText, but I don't know how to get current page number and I need to put page number in the footer of each page of a writer document by using java UNO api. From now, I'm able to set text into the headerText, but I don't know how to get current page number and total pages number.total pages number.
Answer

This should do the trick

Code-Snippet-Listing (snippet-source)

XPropertySet xPropertySet = null;
try {
    // Turn on the header
    XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface( XStyleFamiliesSupplier.class, xTextDoc);

    XNameAccess xNameAccess = xSupplier.getStyleFamilies();
    XNameContainer xStyleCollection = (XNameContainer) UnoRuntime.queryInterface( XNameContainer.class, xNameAccess.getByName("PageStyles") );
    xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStyleCollection.getByName("Standard") );
    xPropertySet.setPropertyValue("HeaderIsOn", new Boolean(true) );

    // Fill in the text
    XText headerText = (XText) UnoRuntime.queryInterface( XText.class, xPropertySet.getPropertyValue("HeaderText") );
    XTextCursor headerCursor = headerText.createTextCursor();
    
    XMultiServiceFactory xDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, xTextDoc );
    
    Object pageCount = xDocMSF.createInstance( "com.sun.star.text.TextField.PageCount" );
    XTextContent pageCountTC = (XTextContent) UnoRuntime.queryInterface( XTextContent.class, pageCount );
    XPropertySet pageCountPS = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, pageCount );
    pageCountPS.setPropertyValue( "NumberingType", new Short(com.sun.star.style.NumberingType.ARABIC) );
    headerText.insertTextContent( headerCursor, pageCountTC, true );
} catch (Exception e) {
    // error handling
}

Changelog

DateUserModification
2004-09-06tomsontomInitial version (summed up from mailling list)

and