openoffice中一个将java代码转换为c++代码的例子

java代码如下:
//Listing 33 A Java Code Example
// Java
// create new writer document and get text, then manipulate text
XComponent xWriterComponent = newDocComponent(“swriter”);
XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class,xWriterComponent);
// Access the text document’s multi service factory, which we will need for most of the
// following examples
mxDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface
(XMultiServiceFactory.class,xTextDocument);
XText xText = xTextDocument.getText();
// create a text cursor from the cells XText interface
XTextCursor xTextCursor = xText.createTextCursor();
// Get the property set of the cell’s TextCursor
XPropertySet xTextCursorProps = (XPropertySet)UnoRuntime.queryInterface
(XPropertySet.class,xTextCursor);
// Page Style name
String pageStyleName= xTextCursorProps.getPropertyValue(“PageStyleName”).toString();
// Get the StyleFamiliesSupplier interface of the document
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface
(XStyleFamiliesSupplier.class,xTextDocument);
// Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
// actual style families
XNameAccess xFamilies = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class,
xSupplier.getStyleFamilies());
// Access the ‘PageStyles’ Family
XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface
(XNameContainer.class,xFamilies.getByName(“PageStyles”));
// Insert the newly created style into the PageStyles family
XStyle xStyle= (XStyle) UnoRuntime.queryInterface(XStyle.class,xFamily.getByName
(pageStyleName));
// Get the property set of the TextCursor
XPropertySet xStyleProps = (XPropertySet)UnoRuntime.queryInterface
(XPropertySet.class,xStyle);
xStyleProps.setPropertyValue(“LeftMargin”,new Short((short)1200));
xStyleProps.setPropertyValue(“RightMargin”,new Short((short)1200));
xStyleProps.setPropertyValue(“BottomMargin”,new Short((short)1200));

c++代码如下:
//Listing 34 The corresponding C++ Translation
// C++
// create new writer document and get text, then manipulate text

// Don’t forget to add : using namespace com::sun::star::text;
// Don’t forget to add : #include
// 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
// 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
// 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
// Don’t forget to add “com.sun.star.container.XNameContainer \” in the makefile

// Don’t forget to add : #include
// 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”);

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
//*** I add a intermediate variable because of Any type returned by getPropertyValue
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);
因为对any类型的理解不深入,我不知道如何直接使用any变量,所以我用了三个临时any变量,见最后三行


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值