Page 1 of 1

[Solved] Getting Total Page Count for text document

Posted: Thu Sep 27, 2012 2:16 am
by dvcroft
I need to know the page count of a document. I see there is a PageCount property in TextDocumentView, but on the getPropertyValue() in the code below I get com.sun.star.beans.UnknownPropertyException.

Code: Select all

    XViewSettingsSupplier xViewSettings = (XViewSettingsSupplier) UnoRuntime.queryInterface(XViewSettingsSupplier.class,xcontroller);
    XPropertySet viewSettings=xViewSettings.getViewSettings();
    Object o=viewSettings.getPropertyValue("PageCount");
Note the following code and output.

Code: Select all

      XServiceInfo factory = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xcontroller);
      String[] availableServiceNames = factory.getSupportedServiceNames();
      for (String string : availableServiceNames) {
          System.out.println(string);
      }

output:
com.sun.star.text.TextDocumentView
com.sun.star.view.OfficeDocumentView

Re: Getting Total Page Count for text document

Posted: Thu Sep 27, 2012 3:36 am
by hanya
Query XPropertySet interface from xcontroller and get PageCount property from it.

Re: Getting Total Page Count for text document

Posted: Thu Sep 27, 2012 3:39 am
by FJCC
The PageCount is part of the CurrentController, not part of the ViewSettings. I used MRI to record the following Java code to get the PageCount.

Code: Select all

import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;

static public void snippet(Object oInitialTarget)
{
	try
	{
		XModel xModel = UnoRuntime.queryInterface(
			XModel.class, oInitialTarget);
		XController xController = xModel.getCurrentController();
		
		XPropertySet xPropertySet = UnoRuntime.queryInterface(
			XPropertySet.class, xController);
		int nPageCount = AnyConverter.toInt(xPropertySet.getPropertyValue("PageCount"));
		
	}
	catch (WrappedTargetException e1)
	{
		// getPropertyValue
		e1.printStackTrace();
	}
	catch (IllegalArgumentException e2)
	{
		// 
		e2.printStackTrace();
	}
	catch (UnknownPropertyException e3)
	{
		// getPropertyValue
		e3.printStackTrace();
	}
}

Re: [Solved] Getting Total Page Count for text document

Posted: Thu Sep 27, 2012 5:22 pm
by dvcroft
Thanks to Hanya and FJCC for the quick help! Worked just as desired.