So....this is awesome and thank you for the response. I'm going to attempt to
hook this up right now...
BUT...
One additional question...because this is using the Writer implementation, that
would imply that I cannot use this same code to determine the pages of a
spreadsheet correct? Is there no page count property on a generic document
object?
I found another post that led me towards Document Properties...but in doing
that code, I'm returning a null on the properties. My code for that is as
follows...
XComponent document = xComponentLoader.loadComponentFromURL(docURL, "_blank",
0, loaderValues);
XDocumentPropertiesSupplier xdis =
(XDocumentPropertiesSupplier)UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class,
document);
XDocumentProperties xProps = (XDocumentProperties)
UnoRuntime.queryInterface(XDocumentProperties.class, xdis);
NamedValue[] stats = xProps.getDocumentStatistics();
for (int i=0; i<stats.length; i++) {
NamedValue stat = stats[i];
if ("PageCount".equalsIgnoreCase(stat.Name)) {
System.out.println("stat value is " + stat.Value);
long pageCount = ((Long) stat.Value).longValue();
}
}
I'm going to hook up your suggestions and test it out across document types,
but a generic solution would be the icing on the cake for me. So, any
additional help would be appreciated, but thank you immensely for the help you
have already provided.
Aaron
-----Original Message-----
From: Ariel Constenla-Haile [mailto:[email protected]]
Sent: Thursday, April 09, 2009 3:52 PM
To: [email protected]
Subject: Re: [api-dev] api to get page count
Hello Aaron,
On Thursday 09 April 2009, 15:54, Aaron Ehrensberger wrote:
> Any chance you know the corresponding java?
as OOo API is a language independet specification, you may try to learn how to
find what you want from
http://api.openoffice.org/docs/common/ref/com/sun/star/module-ix.html
This would be more constructive than just giving you the Java code for
copying&pasting.
There you could you go the "Index" for the "P" letter:
http://api.openoffice.org/docs/common/ref/index-files/index-16.html
and you'll find "PageCount"
PageCount - property in service ::com::sun::star::text:: .TextDocumentView
http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextDocumentView.html#PageCount
no idea what the css.text.TextDocumentView is? Well,
http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextDocumentView.html
has a link to the respective documentation in the Dev's Guide:
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Component/Controllers#Document_Specific_Controller_Services
There you'll learn that css.text.TextDocumentView is the css.frame.Controller
implementation in Writer, and a link to the more specific
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Document_Controller
Here you also learn that you get the Controller from the css.frame.XModel.
The whole picture for a Writer document
* if you load the component via css.frame.XComponentLoader, then you have a
css.lang.XComponent. Query css.frame.XModel from it.
* get the controller from the model via
css.frame.XModel.getCurrentController()
* you get a generic css.frame.XController reference, but you know you have its
Writer implementation, a css.text.TextDocumentView. To get its property
"PageCount", just query the css.beans.XPropertySet from this
css.frame.XController, and use
css.beans.XPropertySet.getPropertyValue("PageCount")
in code:
public static void main(String[] args) {
try {
// get the remote office component context
com.sun.star.uno.XComponentContext xContext =
com.sun.star.comp.helper.Bootstrap.bootstrap();
if (xContext == null) {
System.err.println("ERROR: Could not bootstrap default
Office.");
}
com.sun.star.frame.XComponentLoader xComponentLoader =
(com.sun.star.frame.XComponentLoader)
com.sun.star.uno.UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
com.sun.star.lang.XComponent xComponent =
xComponentLoader.loadComponentFromURL(
"private:factory/swriter",
"_default",
com.sun.star.frame.FrameSearchFlag.ALL,
new com.sun.star.beans.PropertyValue[0]);
com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)
com.sun.star.uno.UnoRuntime.queryInterface(
com.sun.star.frame.XModel.class, xComponent);
com.sun.star.frame.XController xController =
xModel.getCurrentController();
com.sun.star.beans.XPropertySet xPropertySet =
(com.sun.star.beans.XPropertySet)
com.sun.star.uno.UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class, xController);
int nPageCount = com.sun.star.uno.AnyConverter.toInt(
xPropertySet.getPropertyValue("PageCount"));
System.out.printf("\"PageCount\" = %d%n", nPageCount);
} catch (java.lang.Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
notice that instead of simply querying the css.frame.XModel, it¡ll be more
useful to query the css.text.XTextDocument (that is, Writer's XModel
implementation):
com.sun.star.text.XTextDocument xWriterModel =
(com.sun.star.text.XTextDocument)
com.sun.star.uno.UnoRuntime.queryInterface(
com.sun.star.text.XTextDocument.class, xComponent);
com.sun.star.frame.XController xController =
xWriterModel.getCurrentController();
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]