Difference between revisions of "API/Samples/Java/Writer/GraphicsInserter"

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
(New page: Description: This example demo how to insert a picture from local file system to a OpenOffice.org TextDocument. It's consist of four parts in the program. 1.General bootstarp loader 2.Con...)
 
Line 2: Line 2:
  
 
1.General bootstarp loader
 
1.General bootstarp loader
 +
 
2.Conversion between local file system Path and URLs
 
2.Conversion between local file system Path and URLs
 +
 
3.File and location validity check
 
3.File and location validity check
 +
 
4.GraphicsObject services and its parameters
 
4.GraphicsObject services and its parameters
  

Revision as of 06:42, 25 March 2008

Description: This example demo how to insert a picture from local file system to a OpenOffice.org 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

convertFromURL

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

convertToURL

    /**
     * Converts a system path into an URL using OOo 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(
                        XFileIdentifierConverter.class,
                        m_xMCF.createInstanceWithContext(
                            "com.sun.star.ucb.FileContentProvider", m_xContext));
            sURL = xFileConverter.getFileURLFromSystemPath(
                    sBase, sSystemPath );
        } catch (com.sun.star.uno.Exception e) {
            e.printStackTrace();
        } 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(
                "com.sun.star.text.TextGraphicObject");
 
        // 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));
Personal tools