![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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 );
}
}
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 );
}
}