open office (java)

ExpandedBlockStart.gif 代码
import  java.net.MalformedURLException;
import  java.net.URISyntaxException;

import  com.sun.star.beans. * ;
import  com.sun.star.comp.helper. * ;
import  com.sun.star.frame. * ;
import  com.sun.star.lang. * ;
import  com.sun.star.text. * ;
import  com.sun.star.uno. * ;
import  com.sun.star.uno.Exception;
import  com.sun.star.util. * ;
import  ooo.connector.BootstrapSocketConnector;

public   class  Entry {
    
public   static   void  main(String[] args)  throws  Exception, BootstrapException, URISyntaxException,
        MalformedURLException {
    
//  connect to open office
    String oooExeFolder  =   " C:/Program Files/OpenOffice.org 3/program " ;
    XComponentContext xContext 
=  BootstrapSocketConnector.bootstrap(oooExeFolder);
    com.sun.star.lang.XMultiComponentFactory xMCF 
=  xContext.getServiceManager();
    Object oDesktop 
=  xMCF.createInstanceWithContext( " com.sun.star.frame.Desktop " , xContext);
    XComponentLoader xCLoader 
=  (com.sun.star.frame.XComponentLoader) UnoRuntime.queryInterface(
        com.sun.star.frame.XComponentLoader.
class , oDesktop);
    
//  create document
    XComponent document  =  xCLoader.loadComponentFromURL( " private:factory/swriter " " _blank " 0 ,
        
new  PropertyValue[ 0 ]);
    XTextDocument xTextDocument 
=  (XTextDocument) UnoRuntime.queryInterface(XTextDocument. class ,
        document);
    XText xText 
=  xTextDocument.getText();
    
//  create a paragraph cursor
    XParagraphCursor xParagraphCursor  =  (XParagraphCursor) UnoRuntime.queryInterface(
        XParagraphCursor.
class , xText.createTextCursor());
    
//  add some text
    xText.insertString(xText.getEnd(),  " My First OpenOffice Document " false );
    
//  style the paragraph
    XPropertySet xPropertySet  =  (XPropertySet) UnoRuntime.queryInterface(XPropertySet. class ,
        xParagraphCursor);
    xPropertySet.setPropertyValue(
" ParaStyleName " " Heading 1 " );
    
//  add some text
    xText.insertString(xText.getEnd(),  " rThis is the first line in my paragraph.  "
        
+   " Some more text, just to add some text to this document.  " false );
    xPropertySet.setPropertyValue(
" ParaStyleName " " Text body " );
    
//  Add another heading paragraph
    xText.insertString(xText.getEnd(),  " rSecond heading " false );
    xPropertySet.setPropertyValue(
" ParaStyleName " " Heading 2 " );
    
//  And another text body paragraph
    xText.insertString(xText.getEnd(),  " rThis is the second normal paragraph. " false );
    xPropertySet.setPropertyValue(
" ParaStyleName " " Text body " );
    
//  save document
    XStorable xStorable  =  (XStorable) UnoRuntime.queryInterface(XStorable. class , document);
    PropertyValue[] storeProps 
=   new  PropertyValue[ 0 ];
    xStorable.storeAsURL(
" file:///C:/Documents and Settings/Ren/Desktop/wori.odt " , storeProps);
    
//  export document to pdf
    storeProps  =   new  PropertyValue[ 1 ];
    storeProps[
0 =   new  PropertyValue();
    storeProps[
0 ].Name  =   " FilterName " ;
    storeProps[
0 ].Value  =   " writer_pdf_Export " ;
    xStorable.storeToURL(
" file:///C:/Documents and Settings/Ren/Desktop/wori.pdf " , storeProps);
    
//  close document
    XCloseable xcloseable  =  (XCloseable) UnoRuntime.queryInterface(XCloseable. class , document);
    xcloseable.close(
false );
    }
}


转载于:https://www.cnblogs.com/aspxphpjsprb/archive/2009/12/03/1615799.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值