From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Revision as of 13:13, 5 March 2021 by DiGro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Description: This example demo how to insert a picture from local file system to an Apache OpenOffice TextDocument. It's consist of four parts in the program.

  1. General bootstarp loader
  2. Conversion between local file system Path and URLs
  3. File and location validity check
  4. GraphicsObject services and its parameters

General bootstrap loader

General process of bootstrap loader was just like other examples, you may look at [Here]. In GraphicsInserter, the process was wrapped into several methods/levels. On top of that, you may quickly implement your methods like createDrawDocument(), createSpreadsheetDocument(), createImpressDocument() etc.


getMultiComponentFactory() method creates the remote office context( XcomponentContext type) from BootStarp.bootstarp(), then get the services manager(XMultiComponentFactory type). It prepared office context and service manager to upper level, the newDoccomponent() method.

newDoccomponent() will use office context and service manager to create Desktop service and query its XComponentLoader interface to load a document according to parameter passed from its higher level, in this case – createTextDocument().

createTextDocument() is for passing a document type as parameter, e.g. “swriter” to NewDocComponent(String docType) to create document and get it's reference of XtextDocument interface for later manipulation. (“swriter” will be appended after “private:factory/”, as a URLs, the 1st parameter of loadComponentFromURL.)

Conversion between local file system Path and URLs


     * Converts an URL into a system path using AOo API
     * @param sURLPath
     * @return
    private String convertFromURL(String sURLPath) {
        String sSystemPath = null;
        try {
            m_xMCF = getMultiComponentFactory();
            XFileIdentifierConverter xFileConverter =
                    (XFileIdentifierConverter) UnoRuntime.queryInterface(
                            "com.sun.star.ucb.FileContentProvider", m_xContext));
            sSystemPath = xFileConverter.getSystemPathFromFileURL(sURLPath);
        } catch (com.sun.star.uno.Exception e) {
        } finally {
            return sSystemPath;


     * Converts a system path into an URL using AOo API
     * @param sBase
     * @param sSystemPath
     * @return
    private String convertToURL(String sBase, String sSystemPath) {
        String sURL = null;
        try {
            m_xMCF = getMultiComponentFactory();
            XFileIdentifierConverter xFileConverter =
                    (XFileIdentifierConverter) UnoRuntime.queryInterface(
                            "com.sun.star.ucb.FileContentProvider", m_xContext));
            sURL = xFileConverter.getFileURLFromSystemPath(
                    sBase, sSystemPath );
        } catch (com.sun.star.uno.Exception e) {
        } finally {
            return sURL;

The interface XfileIdentifierConverter specifies methods to convert between (file) URLs and file paths in system dependent notation.

For more details information about URLs, please visit Be Careful with file URLs, file naming notations and conversion were discussed in detail.

File and location validity check

The Interface of UCB – XSimpleFileAccess provides some basic methods to access a file/directory. Methods used here are “isFolder()” and “exists()”, to check if an URL represents a folder and to check if a file exists. For detail of specification of XsimpleFileAccess, please visit here.

GraphicsObject services and its parameters

Grahpics inserted into text document was a TextContent, which was created as “com.sun.star.text.TextGraphicObject” by Factory Services.

    // Querying for the interface XMultiServiceFactory on the XTextDocument
        XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) 
                UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
        // Creating the service GraphicObject
        Object oGraphic = xMSFDoc.createInstance(
        // Querying for the interface XTextContent on the GraphicObject
        XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(
                XTextContent.class, oGraphic);

Then it could be inserted into the document as a textcontent:

        // Getting the text
        XText xText = xTextDoc.getText();
        // Getting the cursor on the document
        XTextCursor xTextCursor = xText.createTextCursor();
        // Inserting the content
        xText.insertTextContent(xTextCursor, xTextContent, true);

Till now, it is a blank text content, like a empty picture holder in the text document. Thus, the properties of this graphicObject/ picture holder should be assigned, to tell the writer about what or where picture it should display(picture URLs), positions and sizes etc.

        // Querying for the interface XPropertySet on the graphic object
        XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, oGraphic);
        // Setting the anchor type
        xPropSet.setPropertyValue("AnchorType", TextContentAnchorType.AT_PARAGRAPH);
        // Setting the graphic url
        xPropSet.setPropertyValue("GraphicURL", m_sGraphicFileURL);
        // Setting the horizontal position
        xPropSet.setPropertyValue("HoriOrientPosition", new Integer(5500));
        // Setting the vertical position
        xPropSet.setPropertyValue("VertOrientPosition", new Integer(4200));
        // Setting the width
        xPropSet.setPropertyValue("Width", new Integer(4400));
        // Setting the height
        xPropSet.setPropertyValue("Height", new Integer(4000));

Then, work has been done.

Example project download


Personal tools