Re: API to retrieve document metadata

Ariel Constenla-Haile <[email protected]>
Newsgroups gmane.comp.openoffice.questions
Message-ID <20111025224039.GA16154@localhost>
Hi Alona,

On Mon, Oct 24, 2011 at 04:41:50PM -0400, Alona Rossen wrote:
> metadata listed under File->Properties->Statistics in OpenOffice (number
> of pages, paragraph count, word count, etc).

I see I didn't include those in the example.
See the version I attach now.

Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
client_app.cxx (text/plain, 7 KB)
#define C2U( constAsciiStr ) \
        ( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) ) )

#define U2C( ouString ) \
        ( ::rtl::OUStringToOString( ouString, RTL_TEXTENCODING_UTF8 ).getStr() )

// -------------------------------------------------------------------------------------

#include <vector>
#include <iostream>
#include <cstdio>
#include <osl/file.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/uno/XComponentContext.hpp>

using namespace ::com::sun::star;


int SAL_CALL main ( const int argc, const char* argv[] )
{
    int ret = 0;

    if ( argc != 2 )
    {
        std::cerr << "Usage: " << std::endl <<
                     "       " << argv[0] << " DOC_TO_LOAD" << std::endl <<
                     "where  " << std::endl <<
                     "       DOC_TO_LOAD is the system path of the document to load" << std::endl;
        return 1;
    }

    rtl::OUString path = rtl::OUString::createFromAscii( argv[1] );
    rtl::OUString path_url;
    if ( osl_File_E_None != osl_getFileURLFromSystemPath( path.pData, &path_url.pData ) )
    {
        std::cerr << "Can not convert " << argv[1] << " to URL." << std::endl;
        return 1;
    }

    try
    {
        uno::Reference< uno::XComponentContext > rxContext ( ::cppu::bootstrap() );

        uno::Reference< frame::XComponentLoader > rxComponentLoader(
            rxContext->getServiceManager()->createInstanceWithContext(
                C2U( "com.sun.star.frame.Desktop" ),
                rxContext ),
            uno::UNO_QUERY );

        uno::Reference< lang::XComponent > xComponent =
            rxComponentLoader->loadComponentFromURL(
                path_url, 
                C2U( "_default" ), 
                frame::FrameSearchFlag::ALL,
                uno::Sequence< beans::PropertyValue >() );

        uno::Reference< document::XDocumentPropertiesSupplier> xDocPropsSup( xComponent, uno::UNO_QUERY );
        if ( !xDocPropsSup.is() )
        {
            std::cerr << "No Document Props. Supplier!" << std::endl;
            return 1;
        }

        uno::Reference< document::XDocumentProperties > xDocProps = xDocPropsSup->getDocumentProperties();
        if ( !xDocProps.is() )
        {
            std::cerr << "No Document Properties!" << std::endl;
            return 1;
        }

        // Attributes
        rtl::OUString Title        = xDocProps->getTitle();
        rtl::OUString Subject      = xDocProps->getSubject();
        rtl::OUString Description  = xDocProps->getDescription();

        rtl::OUString Generator    = xDocProps->getGenerator();
        rtl::OUString Author       = xDocProps->getAuthor();

        util::DateTime CreationDate     = xDocProps->getCreationDate();
        rtl::OUString ModifiedBy        = xDocProps->getModifiedBy();
        util::DateTime ModificationDate = xDocProps->getModificationDate();

        uno::Sequence< ::rtl::OUString > Keywords = xDocProps->getKeywords();
        lang::Locale Language = xDocProps->getLanguage();

        rtl::OUString PrintedBy  = xDocProps->getPrintedBy();
        util::DateTime PrintDate = xDocProps->getPrintDate();

        rtl::OUString TemplateName  = xDocProps->getTemplateName();
        rtl::OUString TemplateURL   = xDocProps->getTemplateURL();
        util::DateTime TemplateDate = xDocProps->getTemplateDate();

        rtl::OUString AutoloadURL   = xDocProps->getAutoloadURL();
        sal_Int32 AutoloadSecs      = xDocProps->getAutoloadSecs();
        rtl::OUString DefaultTarget = xDocProps->getDefaultTarget();

        uno::Sequence< beans::NamedValue > DocumentStatistics = xDocProps->getDocumentStatistics();
        sal_Int32   nPageCount,
                    nTableCount,
                    nImageCount,
                    nObjectCount,
                    nParagraphCount,
                    nWordCount,
                    nCharacterCount;
         nPageCount = nTableCount = nImageCount = nObjectCount =
            nParagraphCount = nWordCount = nCharacterCount = 0;

        for ( sal_Int32 n = 0; n < DocumentStatistics.getLength(); n++ )
        {
            rtl::OUString sName = DocumentStatistics[n].Name;
            uno::Any aValue     = DocumentStatistics[n].Value;

            if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "PageCount" ) ) )
                 aValue >>= nPageCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "TableCount" ) ) )
                 aValue >>= nTableCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ImageCount" ) ) )
                 aValue >>= nImageCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ObjectCount" ) ) )
                 aValue >>= nObjectCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ParagraphCount" ) ) )
                 aValue >>= nParagraphCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "WordCount" ) ) )
                 aValue >>= nWordCount;
            else if ( sName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CharacterCount" ) ) )
                 aValue >>= nCharacterCount;
        }

        std::cout << "PageCount       " << nPageCount       << std::endl;
        std::cout << "TableCount      " << nTableCount      << std::endl;
        std::cout << "ImageCount      " << nImageCount      << std::endl;
        std::cout << "ObjectCount     " << nObjectCount     << std::endl;
        std::cout << "ParagraphCount  " << nParagraphCount  << std::endl;
        std::cout << "WordCount       " << nWordCount       << std::endl;
        std::cout << "CharacterCount  " << nCharacterCount  << std::endl;

        sal_Int16 EditingCycles   = xDocProps->getEditingCycles();
        sal_Int32 EditingDuration = xDocProps->getEditingDuration();

        std::cout << "Title :        " << U2C( Title )        <<  std::endl;
        std::cout << "Subject :      " << U2C( Subject )      <<  std::endl;
        std::cout << "Description :  " << U2C( Description )  <<  std::endl;
        std::cout << "Generator :    " << U2C( Generator )    <<  std::endl;
        std::cout << "Author :       " << U2C( Author )       <<  std::endl;
        std::cout << "ModifiedBy :   " << U2C( ModifiedBy )   <<  std::endl;
        std::cout << "PrintedBy :    " << U2C( PrintedBy )    <<  std::endl;
        std::cout << "TemplateName : " << U2C( TemplateName ) <<  std::endl;
        std::cout << "TemplateURL :  " << U2C( TemplateURL )  <<  std::endl;
        std::cout << "AutoloadURL :  " << U2C( AutoloadURL )  <<  std::endl;

    }
    catch ( uno::RuntimeException& e )
    {
        ret = 1;
        OSL_ASSERT ( false );
    }
    catch ( uno::Exception& e )
    {
        ret = 1;
        OSL_ASSERT ( false );
    }
    return ret;
}
// kate: indent-mode cstyle; space-indent on; indent-width 4;
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBCAAGBQJOpzrnAAoJEMjP1bm45QNW+/IQALRSk4QxMnF30uohW7n4rR4S
sOWPf1z/T9V0vGjNQhFU6IjTIdsGArXRaM8IgcwgBbbs23GxQEPm8NzUWl7U5e/Q
02i7ElgJIyd2kXjE1JwYpiV9bMI2wLFKYqSddDlcT0Fghuxjxv/e2Oe2hVPiTw+K
fJ88vIm94TwZVeJDS6gl3OeYNBDe44/T1BC4cMslZpcyMZXF9Vzk7lyp757jBQ94
+3uwFUGf0DCyEcy77x4hQ4HoTjzFZcAcBkl9smpoKwvOJiJzi/4KOunWHvv/RCUK
6MhKoKrgZYgVE0yx3X50oYsIO5acBH9YwU+QtTiisjkTtnIDLaAkH+RYCBLwaCN7
DWc4CNH0IatBdeJ6cvmKdop9scCIbhOLjOB3qYRc2aHMCneHCof8YaQE6qfhGJAL
bP6GfyOrA4GPh9iZnYI4zX3+lRJJw+HO3oThrmzPUpRKs1haadazHr5KEOpqF287
ZdINbvj3BZnqslYXCAAPym6pHNCoYgbJBnHwrw4CurhIv4RzWtFn+xvRXR7c96+D
j9WIaoLOnpTqm8F2mF2YBvUVGIrDqv3duyZErDhHbXu5ejXW+7yOE43gtGZC3fIm
fSRR1i1kXIBREsSKyDhGMnYWrRQ2LeJaen1dOjPMROwKh+oywXxgAGdcb71EbqnM
a7EcKcSyJYUHiO0al2fO
=x+Xl
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.