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

How to write Chinese into a document?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
asouxuning
Power User
Power User


Joined: 20 Jan 2008
Posts: 79

PostPosted: Thu Mar 27, 2008 6:06 pm    Post subject: How to write Chinese into a document? Reply with quote

now i can only write English into a .pdf or .doc file. but if i try to write Chinese into a document, it appears a train of characters no one knows what's the meaning.
below is the code(mainly copied from Serge Moutou's code) for the application:

// C++
#include <stdio.h>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>

using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace rtl;
using namespace cppu;
using namespace com::sun::star::text;
using namespace com::sun::star::beans;


// a procedure for what the so called boostrap
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=2083;urp;StarOffice.ServiceManager" ) );
*/
rInstance = rResolver->resolve( OUString::createFromAscii(
"uno:pipe,name=xuning;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;
}

// /opt/openoffice.org2.3/program/soffice -headless -nofirststartwizard -accept="socket,host=localhost,port=2083;urp;StarOffice.ServiceManagr"
// /opt/openoffice.org2.3/program/soffice -headless -nofirststartwizard -accept="pipe,name=xuning;urp;StarOffice.ServiceManagr"

int
main( )
{
//retrieve an instance of the remote service manager
Reference< XMultiServiceFactory > rOfficeServiceManager //将其看作一个openoffice服务句柄.
= 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 spreadsheet
Reference< XComponent > xWriterComponent = rComponentLoader->loadComponentFromURL(
OUString::createFromAscii("private:factory/swriter"),
OUString::createFromAscii("_blank"),
0,Sequence < ::com::sun::star::beans::PropertyValue >());

// add code here
Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);
Reference< XText > xText = xTextDocument->getText();

Sequence < PropertyValue > filterProperties(2);
filterProperties[0].Name = OUString::createFromAscii ("FilterName");
// filterProperties[0].Value <<= OUString::createFromAscii ("writer_pdf_Export");
filterProperties[0].Value <<= OUString::createFromAscii ("MS Word 97");
filterProperties[1].Name = OUString::createFromAscii ("CharacterSet");
filterProperties[1].Value <<= OUString::createFromAscii ("GB2312");
Reference< XStorable > rcomponentStore (xWriterComponent, UNO_QUERY);
if( !rcomponentStore.is() )
printf( "XStorable Not successfully instanciated\n" );
else
{
xText->setString(OUString::createFromAscii("你好,世界!"));
rcomponentStore->storeToURL(OUString::createFromAscii("file:///opt/openoffice.org2.3_sdk/examples/DevelopersGuide/ProfUNO/CppBinding/t.doc"),
filterProperties );
//rcomponentStore->storeToURL(OUString::createFromAscii("file:///opt/openoffice.org2.3_sdk/examples/DevelopersGuide/ProfUNO/CppBinding/t.pdf"),
// filterProperties );
}
return 0;
}

even i set PropertyValue, it still can't work.
Back to top
View user's profile Send private message
asouxuning
Power User
Power User


Joined: 20 Jan 2008
Posts: 79

PostPosted: Thu Mar 27, 2008 6:47 pm    Post subject: Reply with quote

as code wrote above, i set the CharacterSet property.
filterProperties[1].Name = OUString::createFromAscii ("CharacterSet");
filterProperties[1].Value <<= OUString::createFromAscii ("GB2312");

but this can't work.
Back to top
View user's profile Send private message
asouxuning
Power User
Power User


Joined: 20 Jan 2008
Posts: 79

PostPosted: Fri Mar 28, 2008 1:15 am    Post subject: Reply with quote

ok, i have solved it.
i used the invalid constructor of class OUString.
if i use OUString ("你好!", strlen ("你好!"), RTL_TEXTENCODING_GB_2312), everything will be ok.
this means i must encode the string to unicode while try to write string.
Back to top
View user's profile Send private message
SergeM
Super User
Super User


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

PostPosted: Mon Mar 31, 2008 11:36 am    Post subject: Reply with quote

Quote:

xText->setString(OUString::createFromAscii("你好,世界!"));

createFromAscii is not a good start for OUString when you don't use ASCII chars.
_________________
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 Macros and API 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