javaopenoffice.orguno

OpenOffice, writing a document to a servlet response


Currently we use OpenOffice to grab bookmarks in a template file document and replace them with content from our DB via Java. The lines of code that actually save the file look like this...

  XStorable storable = UnoRuntime.queryInterface(XStorable.class, document);


        // Save as Word 97 Document
        PropertyValue[] properties = new PropertyValue[1];
        PropertyValue property = new PropertyValue();
        property.Name = "FilterName";
        property.Value = FORMAT_WORD_97;
        properties[0] = property;
        storable.storeAsURL(saveFileURL, properties);

We want to directly write the file to the servlet response outputstream, does anybody know of a way to directly get the document as a byte array or inputstream via OpenOffice's UNO api in Java?


Solution

  • This depends on the implementation of the UNO API. We were able to do this with PDF,

        OutputStream os = response.getOutputStream();
    
        PropertyValue[] properties = new PropertyValue[2];
        PropertyValue property = new PropertyValue();
        property.Name = "FilterName";
        property.Value = FORMAT_WORD_97;
        properties[0] = property;
        PropertyValue streamProp = new PropertyValue();
        streamProp.Name = "OutputStream;
        streamProp.Value = os;
        properties[1] = streamProp;
    
        storable.storeAsURL("private:stream", properties);