OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Setting the page properties/margins directly from C++

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Sat Sep 25, 2004 8:43 am    Post subject: Setting the page properties/margins directly from C++ Reply with quote

This thread is the translation into C++ of a previous thread :
Setting the page properties/margins directly from java : http://www.oooforum.org/forum/viewtopic.php?t=12311

The translation from Java code into C++ takes me more than 5 hours !!!
As you see I haven't a gift for C++ Embarassed
Here is my code with some comments added :
Code:

// Don't forget to add : using namespace com::sun::star::text;
// Don't forget to add : #include <com/sun/star/text/XTextDocument.hpp>
// Don't forget to add "com.sun.star.text.XTextDocument \" in the makefile

// Don't forget to add : using namespace com::sun::star::beans;
// Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp>
// Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile
// Don't forget to add : using namespace com::sun::star::style;
// Don't forget to add : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
// Don't forget to add "com.sun.star.style.XStyleFamiliesSupplier \" in the makefile
// Don't forget to add : using namespace com::sun::star::container;

// Don't forget to add : #include <com/sun/star/container/XNameContainer.hpp>
// Don't forget to add "com.sun.star.container.XNameContainer \" in the makefile

// Don't forget to add : #include <com/sun/star/style/XStyle.hpp>
// Don't forget to add "com.sun.star.style.XStyle \" in the makefile

// the first line cannot be translated : already done in our main()
// XComponent xWriterComponent = newDocComponent("swriter");
// Serge Moutou
   Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);
 // Access the text document's multi service factory, which we will need for most of the
 // following examples
   Reference< XMultiServiceFactory > mxDocFactory(xTextDocument,UNO_QUERY);
   Reference< XText > xText = xTextDocument->getText();
 // create a text cursor from the cells XText interface
   Reference< XTextCursor > xTextCursor = xText->createTextCursor();
 
 // Get the property set of the cell's TextCursor
   Reference< XPropertySet > xTextCursorProps(xTextCursor,UNO_QUERY);

 // Page Style name
   Any pageStyleName2 = xTextCursorProps->getPropertyValue
                     (OUString::createFromAscii("PageStyleName"));
   OUString pageStyleName;
   pageStyleName2 >>= pageStyleName ;

  // Get the StyleFamiliesSupplier interface of the document
   Reference< XStyleFamiliesSupplier > xSupplier(xTextDocument,UNO_QUERY);

 // Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
 // actual style families
   Reference< XNameAccess > xFamilies(xSupplier->getStyleFamilies(),UNO_QUERY);

 // Access the 'PageStyles' Family
   Reference< XNameContainer > xFamily(xFamilies->getByName
            (OUString::createFromAscii("PageStyles")),UNO_QUERY);
// Insert the newly created style into the PageStyles family
   Reference< XStyle > xStyle(xFamily->getByName(pageStyleName),UNO_QUERY);

 // Get the property set of the TextCursor
   Reference< XPropertySet > xStyleProps(xStyle,UNO_QUERY);
   Any lm, rm, bm;
   lm<<=(short)1200; rm<<=(short)1200; bm<<=(short)1200;
   xStyleProps->setPropertyValue(OUString::createFromAscii("LeftMargin"),lm);
   xStyleProps->setPropertyValue(OUString::createFromAscii("RightMargin"),rm);
   xStyleProps->setPropertyValue(OUString::createFromAscii("BottomMargin"),bm);

Perhaps is there a more straighforward way of using Any type ?

If you give this code to the best C++ compiler it will not generate a super binary executable because there is no main...
I then add little explanation for using this code.

You first put this code in this main() :
Code:

// C++
// adapted for OOoWriter
int main( ) {
//retrieve an instance of the remote service manager
    Reference< XMultiServiceFactory > rOfficeServiceManager;
    rOfficeServiceManager = ooConnect();
    if( rOfficeServiceManager.is() ){
        printf( "Connected sucessfully to the office\n" );
    }

//get the desktop service using createInstance returns an XInterface type
    Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
    OUString::createFromAscii( "com.sun.star.frame.Desktop" ));

//query for the XComponentLoader interface
    Reference< XComponentLoader > rComponentLoader (Desktop, UNO_QUERY);
    if( rComponentLoader.is() ){
           printf( "XComponentloader successfully instanciated\n" );
       }

//get an instance of the OOowriter document
    Reference< XComponent > xWriterComponent = rComponentLoader->loadComponentFromURL(
   OUString::createFromAscii("private:factory/swriter"),
        OUString::createFromAscii("_blank"),
        0,
        Sequence < ::com::sun::star::beans::PropertyValue >());
// add code here
    return 0;
}

and add the ooconnect() subprogram :
Code:

Reference< XMultiServiceFactory > ooConnect(){
   // create the initial component context
   Reference< XComponentContext > rComponentContext = defaultBootstrap_InitialComponentContext();

   // retrieve the servicemanager from the context
   Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager();

   // instantiate a sample service with the servicemanager.
   Reference< XInterface > rInstance =  rServiceManager->createInstanceWithContext(
         OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),rComponentContext );

   // Query for the XUnoUrlResolver interface
   Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY );

   if( ! rResolver.is() ){
      printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" );
      return NULL;
   }
   try {
      // resolve the uno-url
      rInstance = rResolver->resolve( OUString::createFromAscii(
         "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" ) );

      if( ! rInstance.is() ){
         printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" );
         return NULL;
      }

      // query for the simpler XMultiServiceFactory interface, sufficient for scripting
      Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY);

      if( ! rOfficeServiceManager.is() ){
            printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
            return NULL;
        }       
        return rOfficeServiceManager;
   }
   catch( Exception &e ){
      OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      printf( "Error: %s\n", o.pData->buffer );
      return NULL;
   }
   return NULL;
}

All these three piece of code are put in the “<OpenOffice.org1.1_SDK>/examples/DevelopersGuide/ProfUNO/CppBinding” example in the file “office_connect.cxx” keeping all the #include and using namespace but remplacing the code.
You then add the include that you find in the comments and the using namespace also. Then you add also in the makefile what you find in the comments too.
All of this is more explained in my document : http://perso.wanadoo.fr/moutou/UNOCpp_AP01.sxw particularly in chapter 3.
There is a chapter on OOoWriter in the document but it is roughly the same as this thread
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide


Last edited by SergeM on Fri Dec 02, 2005 11:14 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Sat Sep 25, 2004 8:17 pm    Post subject: Reply with quote

Well done.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Mon Sep 27, 2004 8:14 am    Post subject: Reply with quote

Thank you Christian.
I see you are back from your trip. How was the weather ? I hope better than in Floridia.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group