Search code examples
javaopenoffice.orgopenoffice-impress

OpenOffice API: Saving Impress (presentation) document as self-contained file


When I save a with the 'MS PowerPoint 97' filter, image files used in GraphicObjectShape objects are just linked, not contained in the file.

Is there a property of this filter or a document-property to make OOo create a self-contained file (image files embedded instead of linked)?

Edit:

The XLinkageSupport object has a breakLink function. Any clues how to obtain these interfaces?


Solution

  • You can embed images into an OOo document thru the com.sun.star.drawing.BitmapTable object. The following function will take the document (its XMultiServiceFactory interface), a File object pointing to the image file of your choice and an internalName which has to be unique, embeds the image and returns the URL pointing to the embedded instance.

    /**
     * Embeds an image into the document and gets back the new URL.
     *
     * @param factory Factory interface of the document.
     * @param file Image file.
     * @param internalName Name of the image used inside the document.
     * @return URL of the embedded image.
     */
    public static String embedLocalImage(XMultiServiceFactory factory, File localFile, String internalName) {
    
        // future return value
        String newURL = null;
    
        // URL of the file (note that BitmapTable expects URLs starting with
        // "file://" rather than just "file:". Also note that getRawPath() will
        // return an encoded URL (with special character in the form of %xx).
        String imageURL = "file://" + localFile.toURI().getRawPath();
    
        try {
    
            // get a BitmapTable object from the document (and get interface)
            Object bitmapTable = factory.createInstance("com.sun.star.drawing.BitmapTable");
            XNameContainer bitmapContainer = (XNameContainer)UnoRuntime.queryInterface(XNameContainer.class, bitmapTable);
    
            // insert image by URL into the table
            bitmapContainer.insertByName(internalName, imageURL);
    
            // get interface
            XNameAccess bitmapAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, bitmapTable);
    
            // get the embedded URL back
            newURL = (String)bitmapAccess.getByName(internalName);
    
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    
        // return the new (embedded) url
        return newURL;
    }