Inserting an absolute page number

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
peter.brewer
Posts: 1
Joined: Wed Sep 24, 2008 7:59 pm

Inserting an absolute page number

Post by peter.brewer »

Hi, I'm using openoffice 2.2.0 on windows vista and I'm trying to sort out a problem with page numbering.

I have a document with several different page styles and each page style has it's own numbering in the footer e.g
Page 1 displays 1 of 2
Page 2 displays 2 of 2
Page 3 displays 1 of 3
Page 4 displays 2 of 3
Page 5 displays 3 of 3
and so on...

Thats working very well. However, in the header I want to display the actual Page number ( so 1,2,3,4,5... for the example above). So far I can manually run a (javascript) macro on each page that inserts the absolute page number at the beginning of the page:

Code: Select all

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XTextViewCursorSupplier) ;
importClass(Packages.com.sun.star.text.XTextCursor) ;
importClass(Packages.com.sun.star.text.XPageCursor) ;

importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.frame.XController);
importClass(Packages.com.sun.star.text.XTextViewCursor) ;
importClass(Packages.com.sun.star.style.XStyleFamiliesSupplier) ;

function getPageNumber() {
  var doc = UnoRuntime.queryInterface( XTextDocument, XSCRIPTCONTEXT.getDocument() );
  var controller = UnoRuntime.queryInterface(XTextViewCursorSupplier, doc.getCurrentController()) ;
  var cursor = UnoRuntime.queryInterface(XTextCursor, controller.getViewCursor()) ;
  var pageCursor = UnoRuntime.queryInterface(XPageCursor, cursor) ;
  pageCursor.jumpToStartOfPage() ;
  var absolutePageNo = pageCursor.getPage() ;
  cursor.getStart().setString(absolutePageNo) ;
}
getPageNumber() ;
Q1: How can I insert it into the header of the page?

Q2: How can I automatically run the macro on each page?

Any help or comments on this would be really appreciated as I've spent lots of time on this with little progress.

Thanks in advance, Pete.
User avatar
xergio
Posts: 15
Joined: Thu Jun 26, 2008 11:07 pm
Location: Italy

Re: Inserting an absolute page number

Post by xergio »

Why don't you insert a field with the page number in every style of page that you have?
I ask that because, if you insert a page number or everything else in the header, it will be repeated in every page with the same style, so you have obviously to use a field. But if you do so, you can pre-insert that in page style.
Or do I misunderstand?
OOo 3.2.1 on Ubuntu 10.10 - OOo 3.2.1 on Windows Vista (Sun Virtual Box)
http://www.icstools.it
Post Reply