| <!-- |
| $RCS index.html,v $ |
| |
| last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2004/09/06 10:17:19 $ |
| |
| (c)2003 by the copyright holders listed with the author-tags. |
| If no explicit copyright holder is mentioned with a certain author, |
| the author him-/herself is the copyright holder. All rights reserved. |
| |
| Public Documentation License Notice: |
| |
| The contents of this Documentation are subject to the |
| Public Documentation License Version 1.0 (the "License"); |
| you may only use this Documentation if you comply with |
| the terms of this License. A copy of the License is |
| available at http://www.openoffice.org/licenses/PDL.html |
| |
| The Original Documentation can be found in the CVS archives |
| of openoffice.org at the place specified by RCS in this header. |
| |
| The Initial Writer(s) of the Original Documentation are listed |
| with the author-tags below. |
| |
| The Contributor(s) are listed with the author-tags below |
| without the marker for being an initial author. |
| |
| All Rights Reserved. |
| --> |
| |
| <snippet language="Java" application="Writer"> |
| |
| <keywords> |
| <keyword>page number</keyword> |
| </keywords> |
| |
| <authors> |
| <author id="sw" initial="True" email="stephan.wunderlich@sun.com">Stefan Wunderlich</author> |
| <author id="tomsontom" initial="False" email="tom.schindl@bestsolution.at">Tom Schindl</author> |
| </authors> |
| |
| <question heading="Add Page number"> |
| 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. |
| </question> |
| |
| <answer> |
| <p>This should do the trick</p> |
| <listing> |
| 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 |
| } |
| |
| </listing> |
| </answer> |
| |
| <changelog> |
| <change author-id="tomsontom" date="2004-09-06">Initial version (summed up from mailling list)</change> |
| </changelog> |
| |
| </snippet> |