Get page count with API

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
aehrensberger
Posts: 10
Joined: Tue Apr 07, 2009 8:45 pm

Get page count with API

Post by aehrensberger »

Using the API, is there an easy way to get the number of pages from the Office document regardless of what type of doc it is - ie, .doc, .xls, .ppt? I want to display the page count to the user in our interface and haven't found a quick and easy way to do it and it seems like there should be one?
OOo 3.0.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get page count with API

Post by Villeroy »

You want the page count from menu:File>Properties...tab:Statistics :?:
Install http://extensions.services.openoffice.org/project/MRI and restart the office with some multipage documents.
menu:Tools>Add-Ons>MRI [inspect current document]
Double-click DocumentProperties
Double-click DocumentStatistics
Double-click the "Value" of named value "PageCount"

It's almost as easy as calling the GUI dialogue. The only difficult matter is that DocumentStatistics is implemented as an array of named values: http://api.openoffice.org/docs/common/r ... Statistics
Loop through the array until you get a struct named "PageCount" and get the value.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
aehrensberger
Posts: 10
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get page count with API

Post by aehrensberger »

Either I misunderstand your post or you misunderstood mine I fear....

I want to get the page count programmatically through java code using the API. I don't have any dialogs available or any GUI features so when I read double-click on things, you lose me. At least, I think you lose me.

Are you talking about getting those page counts through programmatic API calls?
OOo 3.0.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get page count with API

Post by Villeroy »

Did you install MRI? No?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
aehrensberger
Posts: 10
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get page count with API

Post by aehrensberger »

No....so installing this will give me a new API to use? I didn't see that so I didn't go any further with it... And I can use it in a headless mode, correct? I'll give it a shot...
OOo 3.0.X on Ms Windows XP
aehrensberger
Posts: 10
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get page count with API

Post by aehrensberger »

I installed the extension and can definitely see where to pull that data from the interface....but I still can't find an API that I can use programmatically to get the number of pages? Where would I find that? I personally find the OO api to be confusing to navigate.
OOo 3.0.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get page count with API

Post by Villeroy »

Code: Select all

REM BASIC
Function getPageCount(oDoc)
oProps = oDoc.getDocumentProperties()
REM or pseudo-property: oProps = oDoc.DocumentProperties
aStats = oProps.DocumentStatistics ' true property
REM oDoc.getDocumentStatistics() is not a valid method call

REM Loop through the array of c.s.s.beans.NamedValue
for i = uBound(aStats) to 0 step -1
  x = aStats(i)
  if x.Name = "PageCount" then exit for
next
getPageCount = 0
if i > -1 then getPageCount = x.Value
End Function
Last edited by Villeroy on Fri Apr 10, 2009 12:17 pm, edited 1 time in total.
Reason: REM comments on pseudo-property
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
aehrensberger
Posts: 10
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get page count with API

Post by aehrensberger »

I appreciate that snippet, but I'm using Java, not OOBasic....and in translating it, I'm missing something...

My code is ...

Code: Select all

XComponent document = xComponentLoader.loadComponentFromURL(docURL, "_blank", 0, loaderValues);
                      
// get properties
XDocumentProperties xProps = (XDocumentProperties) UnoRuntime.queryInterface(XDocumentProperties.class, document);
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();                  
    }
}
But that line that pulls the properties back....that line is returning a NULL for the properties. I don't understand that.... if I could get the properties, I think I'd be all set.
OOo 3.0.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get page count with API

Post by Villeroy »

OK, Java. I don't have a JDK, but you know how to call methods and get properties of UNO objects.
When using scripting languages, there is some syntactical sugar coating for method calls var=obj.getSomething() and obj.setSomething(var). You can treat this pair of get/set-methods like a pseudo-property and write var=obj.Something and obj.Something=var.
If there is no set-method behind a pseudo-property it's read-only, without get-method it's write-only.

MRI tags these properties as "Pseud" and you can't use them in Java, as far as I know.
getDocumentProperties() is a method, returning an object with a true property "DocumentStatistics". MRI does not tag it as "Pseud". There is not corresponding method getDocumentStatistics().
As far as I know, you must call method getPropertyValue() of interface c.s.s.beans.XPropertySet.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dba03
Posts: 5
Joined: Wed Apr 15, 2009 4:38 am

Re: Get page count with API

Post by dba03 »

Villeroy wrote:

Code: Select all

REM BASIC
Function getPageCount(oDoc)
oProps = oDoc.getDocumentProperties()
REM or pseudo-property: oProps = oDoc.DocumentProperties
aStats = oProps.DocumentStatistics ' true property
REM oDoc.getDocumentStatistics() is not a valid method call

REM Loop through the array of c.s.s.beans.NamedValue
for i = uBound(aStats) to 0 step -1
  x = aStats(i)
  if x.Name = "PageCount" then exit for
next
getPageCount = 0
if i > -1 then getPageCount = x.Value
End Function
The macro has some errors.
It seems that the result is correct only if I open "File > Properties" at first.
If I open the document and run the macro directly, the result is wrong.

May be it need to update statistics firstly。
OOo 3.0.X on Ms Windows XP
dba03
Posts: 5
Joined: Wed Apr 15, 2009 4:38 am

Re: Get page count with API

Post by dba03 »

Code: Select all

ThisComponent.CurrentController.PageCount
This is simple and correct.
OOo 3.0.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Get page count with API

Post by Villeroy »

dba03 wrote:

Code: Select all

ThisComponent.CurrentController.PageCount
This is simple and correct.
:lol: Indeed, I was too focussed on what I knew from the user interface. Thank you.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Manuel.DS
Posts: 1
Joined: Tue Feb 24, 2015 4:22 pm

Re: Get page count with API

Post by Manuel.DS »

Code: Select all

    Private Function GetPageCount() As Integer
        Dim ReturnValue As Integer

        Dim DocumentPropertiesSupplier As unoidl.com.sun.star.document.XDocumentPropertiesSupplier
        Dim DocumentProperties As unoidl.com.sun.star.document.XDocumentProperties
        Dim DocumentStatistics() As unoidl.com.sun.star.beans.NamedValue

        DocumentPropertiesSupplier = DirectCast(oDoc, unoidl.com.sun.star.document.XDocumentPropertiesSupplier)
        DocumentProperties = DocumentPropertiesSupplier.getDocumentProperties
        DocumentStatistics = DocumentProperties.DocumentStatistics

        For j = 0 To DocumentStatistics.Length - 1
            If DocumentStatistics(j).Name = "PageCount" Then
                ReturnValue = DocumentStatistics(j).Value.Value
            End If
        Next

        DocumentPropertiesSupplier = Nothing
        DocumentProperties = Nothing
        DocumentStatistics = Nothing

        Return ReturnValue
    End Function
hi,
i wrote this vb.net function to retrieve the page count of an opened document (by another function). If i call the function immediately after open it's work fine but if i add text the function result is wrong (return the initial value, not updated).
how do I do to update the data?
Thanks
Manuel
OpenOffice 4.1.1 on Windows 8
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Get page count with API

Post by Mr.Dandy »

Manuel.DS wrote:but if i add text the function result is wrong (return the initial value, not updated).
Sounds like Issue 116909 :)
OpenOffice 4.1.12 - Windows 10
Post Reply