views:

138

answers:

1

I want to count the no. of pages that would be produced if I printed out an OpenOffice.org document. I can already do it for ODT files using the following code:

public short getPageCount() {
        XModel model = (XModel) UnoRuntime.queryInterface(XModel.class,
                        getDocument());

        XTextViewCursorSupplier supplier = (XTextViewCursorSupplier) UnoRuntime
                        .queryInterface(XTextViewCursorSupplier.class, model
                                        .getCurrentController());

        XTextViewCursor viewCursor = supplier.getViewCursor();

        XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(
                        XPageCursor.class, viewCursor);

        pageCursor.jumpToLastPage();

        return pageCursor.getPage();
}

public Object getDocument() {
        XComponentContext context = Bootstrap.bootstrap();

        XMultiComponentFactory factory = context.getServiceManager();

        Object desktop = factory.createInstanceWithContext(
                        "com.sun.star.frame.Desktop", context);

        XComponentLoader loader = (XComponentLoader) UnoRuntime.queryInterface(
                        XComponentLoader.class, desktop);

        XComponent component = loader.loadComponentFromURL("file:///path/to/file.odt",
                        "_blank", 0, new PropertyValue[0]);

        return UnoRuntime.queryInterface(XTextDocument.class,
                        component);
}

I want to know if I can do something similar with ODS files. Maybe count the no. of page breaks in the sheets? There is a ShowPageBreaks property in the SpreadsheetViewSettings class but no PageBreakCount or getPageBreaks(). :P

A: 
// get the sheet document using UnoRuntime
XSpreadsheetDocument sheetDoc = ...;
// get an object containing all sheets
XSpreadsheets sheets = sheetDoc.getSheets();
// get the sheets names
String[] sheetnames = sheets.getElementNames();

and there you have something you can count or iterate.

EDIT:
The sheet itself probably has no concept of print pages.
So i guess you either need to use something like XPrintPreview, but i don't see how to determine pages from there, or see Printing Spreadsheet Documents and try to use PrintAreas and some maths.

Stroboskop
Sorry, I just realized I didn't make it clear that I want the no. of pages that would be produced if I printed out the spreadsheet.It appears that the no. of sheets may not always be the same as the no. of pages printed out.
Chry Cheng