print preview Tom Schindl Stephan Wunderlich How to get a print preview

I've been looking at printing via the OpenOffice.org API. Is there a way to get a print preview? There doesn't seem to be anything about this in the Devlopers' Guide.

supposing you have the XModel (xModel) of the document and the ServiceManager (xMSF) then the following should do what you want.

import com.sun.star.frame.XController; import com.sun.star.frame.XDispatch; import com.sun.star.frame.XDispatchProvider; import com.sun.star.frame.XModel; import com.sun.star.util.XURLTransformer; import com.sun.star.util.URL; import com.sun.star.lang.XMultiServiceFactory; XController xController = xModel.getCurrentController(); //switch to 'Print Preview' mode try { XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, xController); XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, xMSF.createInstance("com.sun.star.util.URLTransformer")); URL[] aParseURL = new URL[1]; aParseURL[0] = new URL(); aParseURL[0].Complete = ".uno:PrintPreview"; xParser.parseStrict(aParseURL); URL aURL = aParseURL[0]; XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0); if(xDispatcher != null) { xDispatcher.dispatch( aURL, new PropertyValue[]{} ); } } catch (com.sun.star.uno.Exception e) { }
Initial version, summed up from mailling list Added missing imports