Hi Alona,

On Tue, Oct 25, 2011 at 11:12:42AM -0400, Alona Rossen wrote:
> Hi Ariel,
> 
> Could you please provide a C++ example of how to access
> getDocumentProperties() method after the document has been loaded? In
> other words, we cannot understand how to bridge between xComponent,
> which we get after document is loaded and getDocumentProperties().

when you load a document you get a css.lang.XComponent.
Depending on the document you loaded, this document implements different
interfaces.

The interfaces a document supports are documented in the document
service:

* a Writer document is documented in the service css.text.TextDocument
    
http://api.openoffice.org/docs/common/ref/com/sun/star/text/TextDocument.html
        This service includes css.text.GenericTextDocument
        
http://api.openoffice.org/docs/common/ref/com/sun/star/text/GenericTextDocument.html
            This service includes css.document.OfficeDocument   
            
http://api.openoffice.org/docs/common/ref/com/sun/star/document/OfficeDocument.html
            You can see there all the interfaces an OfficeDocument
            implements

To get an interface from an object (your xComponent here), just query for it:

css::uno::Reference< SOME_INTERFACE > xInterface( xComponent, css::uno::QUERY);
if ( xInterface.is() )
{
    // here do your job
}


You can do the same research for all other OOo documents:
* a Draw document
* a Presentation
* etc

See the attached example.


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
#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_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;

Attachment: pgpW3DmMcm3aE.pgp
Description: PGP signature

Reply via email to