How can the no. of pages in an OpenOffice.org spreadsheet be obtained programmatically?
-
13-09-2019 - |
Question
I want to count the number 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
Solution
// 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow