[Java solution] Using XInputStream and XOutputStream

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

[Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

Introduction

OpenOffice.org offers the possibility to load and store files using streams. For using this, all you need are two classes implementing XInputStream and XOutputStream. Additionally it seems to be important, that the class that implements XInputStream also implements XSeekable.

To get this example working, I've used the BootstrapSocketConnector from [Java solution] "no office executable found!".

I've developed this example on Windows XP using OOo 2.3.1, NetBeans IDE 6.0 and Java 1.6_04.


How-to

The first class implements XInputStream and XSeekable:

Code: Select all

package ooo.streams;

import java.io.ByteArrayInputStream;

import com.sun.star.io.BufferSizeExceededException;
import com.sun.star.io.NotConnectedException;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XSeekable;

/**
 * <a href="http://www.oooforum.org/forum/viewtopic.phtml?t=13205">OOInputStream from the thread <b>OOo-Java: Using XInputStream...</b></a>
 */
public class OOoInputStream extends ByteArrayInputStream implements XInputStream, XSeekable {

    public OOoInputStream(byte[] buf) {
        super(buf);
    }


    //
    // Implement XInputStream
    //

    public int readBytes(byte[][] buffer, int bufferSize) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException {
        int numberOfReadBytes;
        try {
            byte[] bytes = new byte[bufferSize];
            numberOfReadBytes = super.read(bytes);
            if(numberOfReadBytes > 0) {
                if(numberOfReadBytes < bufferSize) {
                    byte[] smallerBuffer = new byte[numberOfReadBytes];
                    System.arraycopy(bytes, 0, smallerBuffer, 0, numberOfReadBytes);
                    bytes = smallerBuffer;
                }
            }
            else {
                bytes = new byte[0];
                numberOfReadBytes = 0;
            }

            buffer[0]=bytes;
            return numberOfReadBytes;
        }
        catch (java.io.IOException e) {
            throw new com.sun.star.io.IOException(e.getMessage(),this);
        }
    }

    public int readSomeBytes(byte[][] buffer, int bufferSize) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException {
        return readBytes(buffer, bufferSize);
    }

    public void skipBytes(int skipLength) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException {
        skip(skipLength);
    }

    public void closeInput() throws NotConnectedException, com.sun.star.io.IOException {
        try {
            close();
        }
        catch (java.io.IOException e) {
            throw new com.sun.star.io.IOException(e.getMessage(), this);
        }
    }


    //
    // Implement XSeekable
    //

    public long getLength() throws com.sun.star.io.IOException {
        return count;
    }

    public long getPosition() throws com.sun.star.io.IOException {
        return pos;
    }

    public void seek(long position) throws IllegalArgumentException, com.sun.star.io.IOException {
        pos = (int) position;
    }
}
The second class implements XOutputStream:

Code: Select all

package ooo.streams;

import java.io.ByteArrayOutputStream;

import com.sun.star.io.BufferSizeExceededException;
import com.sun.star.io.NotConnectedException;
import com.sun.star.io.XOutputStream;

/**
 * <a href="http://www.oooforum.org/forum/viewtopic.phtml?t=13205">OOInputStream from the thread <b>OOo-Java: Using XInputStream...</b></a>
 */
public class OOoOutputStream extends ByteArrayOutputStream implements XOutputStream {

    public OOoOutputStream() {
        super(32768);
    }


    //
    // Implement XOutputStream
    //

    public void writeBytes(byte[] values) throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException {
        try {
            this.write(values);
        }
        catch (java.io.IOException e) {
            throw(new com.sun.star.io.IOException(e.getMessage()));
        }
    }

    public void closeOutput() throws NotConnectedException, BufferSizeExceededException, com.sun.star.io.IOException {
        try {
            super.flush();
            super.close();
        }
        catch (java.io.IOException e) {
            throw(new com.sun.star.io.IOException(e.getMessage()));
        }
    }

    @Override
    public void flush() {
        try {
            super.flush();
        }
        catch (java.io.IOException e) {
        }
    }
}
The last class is a little example that uses OOoInputStream and OOoOutputStream to convert an OOo Writer document to PDF:

Code: Select all

package ooo.streams;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;

import ooo.connector.BootstrapSocketConnector;

public class OOoStreamConverter {
    private XComponentContext xComponentContext;

    public OOoStreamConverter(XComponentContext xComponentContext) {
        this.xComponentContext = xComponentContext;
    }

    public void convert(OOoInputStream input, OOoOutputStream output, String filterName) throws Exception {
        XMultiComponentFactory xMultiComponentFactory = xComponentContext.getServiceManager();
        Object desktopService = xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xComponentContext);
        XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopService);
        
        PropertyValue[] conversionProperties = new PropertyValue[2];
        conversionProperties[0] = new PropertyValue();
        conversionProperties[1] = new PropertyValue();

        conversionProperties[0].Name = "InputStream";
        conversionProperties[0].Value = input;
        conversionProperties[1].Name = "Hidden";
        conversionProperties[1].Value = new Boolean(true);

        XComponent document = xComponentLoader.loadComponentFromURL("private:stream", "_blank", 0, conversionProperties);

        conversionProperties[0].Name = "OutputStream";
        conversionProperties[0].Value = output;
        conversionProperties[1].Name = "FilterName";
        conversionProperties[1].Value = filterName;

        XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,document);
        xstorable.storeToURL("private:stream", conversionProperties);

        XCloseable xclosable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class,document);
        xclosable.close(true);
    }

    public static void main(String[] args) {
        String oooExecutableFolder = "c:/program files/openoffice.org 2.3/program/";
        String inputFilename = "c:/temp/text.odt";
        String outputFilename = "c:/temp/text.pdf";

        try {
            // Connect to OOo server
            XComponentContext xComponentContext = BootstrapSocketConnector.bootstrap(oooExecutableFolder);
            OOoStreamConverter converter = new OOoStreamConverter(xComponentContext);

            // Create OOoInputStream
            InputStream inputFile = new BufferedInputStream(new FileInputStream(inputFilename)); 
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            byte[] byteBuffer = new byte[4096];
            int byteBufferLength = 0;
            while ((byteBufferLength = inputFile.read(byteBuffer)) > 0) {
                    bytes.write(byteBuffer,0,byteBufferLength);
            }
            inputFile.close();
            OOoInputStream inputStream = new OOoInputStream(bytes.toByteArray());

            // Create OOoOutputStream
            OOoOutputStream outputStream = new OOoOutputStream();

            // Convert document to PDF
            converter.convert(inputStream, outputStream, "writer_pdf_Export");

            // Save OOoOutputStream
            FileOutputStream outputFile = new FileOutputStream(outputFilename);
            outputFile.write(outputStream.toByteArray());
            outputFile.close();
        } catch (BootstrapException e) {
            System.err.println("Connection not available");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("File error");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }
}
The method OOoStreamConverter.convert() contains the crucial part of this example. It shows how to tell OOo to use an InputStream for loading (conversionProperties[0].Name = "InputStream"; conversionProperties[0].Value = input; and loadComponentFromURL("private:stream", ...)) and an OutputStream for storing (conversionProperties[0].Name = "OutputStream"; conversionProperties[0].Value = output; and storeToURL("private:stream", ...).

Credits

The idea to develop this little how-to comes from the thread OOo-Java: Using XInputStream... and from various posts in the (Unofficial) OpenOffice.org Forum asking how to handle XInputStream and XOutputStream.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Change log

Post by hol.sten »

24. Mar 2008: Creation of Java solution
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
GandalfMCSD
Posts: 1
Joined: Fri May 16, 2008 7:38 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by GandalfMCSD »

I am a new convert to the OO side of things, and have been tasked with writing a .NET (C#) module that does this same thing. Has anyone done this? Can you give me a link/somewhere to look to get me started?

I have the code using the OO API, and I seem to get the document loaded, but when I do the save I get a blank document.

Suggestions? Somewhere to look?

THANKS!

Rich
(aka Gandalf the White)
gato
Posts: 2
Joined: Tue Dec 16, 2008 12:49 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by gato »

loadComponentFromURL("private:stream") opens Writer because it's default component.
How can i put the stream into component loading Spreadsheet (ods) ?
private:factory/scalc - loading empty component
private:stream/scalc - didn't work
OOo 3.0.X on Ms Windows XP
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

gato wrote:loadComponentFromURL("private:stream") opens Writer because it's default component.
This is not correct! loadComponentFromURL("private:stream") loads a stream. OOo decides from the stream content, which OOo application should be opened. In the example above it opens OOo Writer, because the stream is filled from inputFilename = "c:/temp/text.odt";.
gato wrote:How can i put the stream into component loading Spreadsheet (ods) ?
Fill the stream from an ods document. That's all!
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
gato
Posts: 2
Joined: Tue Dec 16, 2008 12:49 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by gato »

Yes, you right. The problem that FODS (flat ods) do not works correctly with stream.Why is it?
Success example:
loadComponentFromURL("c:/test.fods", "_blank", 0, propertyValues);
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = "calc8";
xstorable.storeToURL("c:/test.ods", propertyValues);

Error:
InputStream inputFile = new BufferedInputStream(new FileInputStream("c:/test.fods"));
...
OOoInputStream inputStream = new OOoInputStream(bytes.toByteArray());
loadComponentFromURL("private:stream", "_blank", 0, propertyValues);
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = "calc8";
xstorable.storeToURL("private:stream", propertyValues);
FileOutputStream outputFile = new FileOutputStream(outputFilename);
outputFile.write(outputStream.toByteArray());
outputFile.close();
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

gato wrote:The problem that FODS (flat ods) do not works correctly with stream.Why is it?
What is a flat ods? I've never worked with a flat ods, whatever that might be.
gato wrote:Error:

Code: Select all

InputStream inputFile = new BufferedInputStream(new FileInputStream("c:/test.fods"));
...
OOoInputStream inputStream = new OOoInputStream(bytes.toByteArray());
loadComponentFromURL("private:stream", "_blank", 0, propertyValues);
It's hard to guess what went wrong, because you omitted in your code example the setting of propertyValues.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
tanalyw
Posts: 5
Joined: Wed Feb 25, 2009 5:13 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by tanalyw »

Hi,

I used the same code to do a server client conversion.
However, when I convert the odt document into html document, the images in the odt document is not extracted.
Any idea what is wrong?
OOo 2.4.X on Ms Windows W2k
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

tanalyw wrote:Any idea what is wrong?
There is always a lot that can cause problems. But without any helpful information from you (OOo version and were from, operating system, Java version, your source code, your ODT file, the export filter your use, ...) I have no idea where to start helping you.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
tanalyw
Posts: 5
Joined: Wed Feb 25, 2009 5:13 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by tanalyw »

Hi,

OO version - 2.4
OS Client - Win 2k
OS Server - Red hat
Java version - 1.4.2

Codes.

OpenOffice.java

private static XComponentContext componentContext = null;
private XComponentLoader componentLoader = null;
private XComponent component = null;

private String connectionString = "uno:socket,host=192.168.0.10,port=8100;urp;StarOffice.ServiceManager";

//Method to convert OpenOffice document to html and vice versa
public boolean convert(OOInputStream inputStream, OOOutputStream outputStream, String conversionType) {
boolean converted = false;
setNeccessaryComponents(inputStream, outputStream);

if(componentContext != null && componentLoader != null && component != null) {
storeConvertedDocToPath(conversionType, outputStream);
converted = true;
System.out.println("Converted");
}

return converted;
}

//Set the ComponentContext, ComponentLoader and Component
public void setNeccessaryComponents(OOInputStream inputFile, OOOutputStream output) {
XComponentContext cc = getComponentContext();
if(cc != null)
componentContext = cc;
XComponentLoader cl = getComponentLoader();
if(cl != null)
componentLoader = cl;
XComponent c = getXComponent(inputFile, output);
if(c != null)
component = c;
}

//Retrieve the xComponentContext object
public XComponentContext getComponentContext() {
XComponentContext cc = null;
try {
cc = Bootstrap.createInitialComponentContext(null);
}
catch(Exception e) {}
return cc;
}

//Retrieve the xComponentLoader object
public XComponentLoader getComponentLoader() {
XComponentLoader cl = null;
try {
XMultiComponentFactory multiComponentFactory = getXMulitComponentFactory();
if(multiComponentFactory != null) {
XPropertySet propertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, multiComponentFactory);
Object defaultContext = propertySet.getPropertyValue("DefaultContext");

componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, defaultContext);
cl = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
multiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", componentContext));
}
}
catch(Exception e) {}
return cl;
}

public XMultiComponentFactory getXMulitComponentFactory() {
XMultiComponentFactory multiComponentFactory = null;
try {
multiComponentFactory = componentContext.getServiceManager();

Object objectUrlResolver = multiComponentFactory.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", componentContext);
XUnoUrlResolver xurlresolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, objectUrlResolver);

Object objectInitial = xurlresolver.resolve(connectionString);
multiComponentFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial);
}
catch(Exception e) {}
return multiComponentFactory;
}

//Retrieve the xComponent object
public XComponent getXComponent(OOInputStream inputStream, OOOutputStream outputStream) {
XComponent component = null;
try {
PropertyValue[] conversionProperties = new PropertyValue[2];
conversionProperties[0] = new PropertyValue();
conversionProperties[1] = new PropertyValue();

conversionProperties[0].Name = "InputStream";
conversionProperties[0].Value = inputStream;
conversionProperties[1].Name = "Hidden";
conversionProperties[1].Value = new Boolean(true);

component = componentLoader.loadComponentFromURL("private:stream", "_blank", 0, conversionProperties);
}
catch(Exception e) {}
return component;
}

//Store the converted doc using XStorable object
public void storeConvertedDocToPath(String conversionType, OOOutputStream output) {
try {

XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, component);
PropertyValue[] outputProperties = getOutputProperties(output, conversionType);
storable.storeToURL("private:stream", outputProperties);
XCloseable xclosable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, component);
xclosable.close(true);
}
catch(Exception e){}
}

//Get conversion properties
public PropertyValue[] getOutputProperties(OOOutputStream output, String conversionType)
{
PropertyValue[] outputProperties = new PropertyValue[2];
outputProperties[0] = new PropertyValue();
outputProperties[0].Name = "OutputStream";
outputProperties[0].Value = output;

outputProperties[1] = new PropertyValue();
outputProperties[1].Name = "FilterName";
outputProperties[1].Value = conversionType;

return outputProperties;
}

//Get Array output stream for openoffice
public ByteArrayOutputStream getByteArrayOutputStream(String docPath) {
ByteArrayOutputStream bytes = null;
try {
InputStream inputFile = new BufferedInputStream(new FileInputStream(docPath));
bytes = new ByteArrayOutputStream();
byte[] byteBuffer = new byte[4096];
int byteBufferLength = 0;
while ((byteBufferLength = inputFile.read(byteBuffer)) > 0) {
bytes.write(byteBuffer,0,byteBufferLength);
}
inputFile.close();
}
catch(Exception e) {}
return bytes;
}

//Write out the converted document
public void writeOut(String filePath, OOOutputStream outputStream) {
try {
FileOutputStream outputFile = new FileOutputStream(filePath);
outputFile.write(outputStream.toByteArray());
outputFile.close();
}
catch(IOException ioe) {}
}

Main class to test the abv codes
OpenOffice oo = new OpenOffice();

ByteArrayOutputStream baos = oo.getByteArrayOutputStream("/tmp/test.odt");
OOInputStream inputStream = new OOInputStream(baos.toByteArray());
OOOutputStream outputStream = new OOOutputStream();

String conversionType = "HTML";
boolean converted = oo.convert(inputStream, outputStream, conversionType);
String outputPath = "/tmp/test.html";

if(converted == true)
oo.writeOut(outputPath, outputStream);

The html file is generated however the images inside the odt file is not generated. Thanks for the help
OOo 2.4.X on Ms Windows W2k
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

tanalyw wrote:

Code: Select all

...
String conversionType = "HTML";
...
The html file is generated however the images inside the odt file is not generated.
Did you try OOo's other HTML filters? Here is a complete list of OOo's filters: http://wiki.services.openoffice.org/wik ... st_OOo_2_1
So you might try instead of "HTML" either "HTML (StarWriter)" or "XHTML Writer File".

Did you try to export your ODT directly from OOo. If so, did the export with image work there?

With which browser did you open the exported HTML file?

Can you provide you ODT file for further investigations?
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
tanalyw
Posts: 5
Joined: Wed Feb 25, 2009 5:13 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by tanalyw »

I tried using the filters but it gave ErrorIOCodeException

If I the odt document and the oo instance (soffice -accept .... command) is on the same machine, I was able to export it. All images in the odt document will be generated out as .png files.

However, if the oo instance is on another machine (server) and the odt file is at the client machine, the html file will be generated, but not the images in the odt.
The odt document I use is just any document created using OpenOffice.

Thanks for the help.
OOo 2.4.X on Ms Windows W2k
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

I gave it a try on a single machine using Windows XP with OOo 2.4.1 and OOo 3.0.0, Java 1.6.0_10 and a simple ODT file with 4 pages of text including 5 pictures.
tanalyw wrote:I tried using the filters but it gave ErrorIOCodeException
I tried the HTML filters "HTML", "HTML (StarWriter)", and "XHTML Writer File" without getting an ErrorIOCodeException.
I tried additionally the PDF filter "writer_pdf_Export" to create a PDF.

First I tried to save my ODT file directly from OOo 2.4.1 and OOo 3.0.0 as HTML: I got a HTML file and five PNG files. That's ok.
Exporting my ODT file directly from OOo 2.4.1 and OOo 3.0.0 as PDF has been successful, too. The PDF included all the pictures.

Second I tried to save my ODT file using XInputStream and XOutputStream with OOo 2.4.1 as HTML: I got a HTML file and no PNG files. That's not ok, but similar to your results.
Exporting my ODT file using XInputStream and XOutputStream with OOo 2.4.1 as PDF has been successful, too. The PDF again included all the pictures.

Third I tried to save my ODT file without using XOutputStream with OOo 2.4.1 as HTML. I saved it as a simple file: I got a HTML file and five PNG files. That's ok, but not what you are looking for.
Exporting my ODT file without using XOutputStream with OOo 2.4.1 as PDF has been successful, too. The PDF again included all the pictures.

Conclusion: It seems to me, that you cannot use XOutputStream to create one HTML file and several PNG files. I think that the problem is the output stream itself. It can carry only one file at a time. So if you really need a client/server conversion for creating HTML files with pictures, you need a different approach and some additional coding. You can transfer your ODT file via XInputStream to your server. But for a file conversion to a HTML file with pictures you have to use a simple file on the server side. After saving the HTML file and the PNG files you need some additional coding to transfer those files back to the client.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
tanalyw
Posts: 5
Joined: Wed Feb 25, 2009 5:13 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by tanalyw »

Yeah, thats what I thought of too. Thanks for your help.
OOo 2.4.X on Ms Windows W2k
tanalyw
Posts: 5
Joined: Wed Feb 25, 2009 5:13 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by tanalyw »

Yo, its me again. sorry to trouble u again.

I was trying to convert my odg, odp and into html format.
I am using the filter value specified in here http://wiki.services.openoffice.org/wik ... st_OOo_2_1

odg - draw_html_export
odp - impress_html_export

However, I keep getting com.sun.star.task.ErrorCodeIOException. Any idea why is it this way?
Thanks much
OOo 2.4.X on Ms Windows W2k
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

tanalyw wrote:Any idea why is it this way?
Sorry, but no.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
famagosta..
Posts: 3
Joined: Wed Aug 25, 2010 9:47 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by famagosta.. »

hi all,
i know that this is an old post, but is the more similar i found.
I'm using openoffice to transform PDF documents to PDF/A using Java.
I installed the pdfimport plugin into openoffice and tryed to do it by hands, this work fine.
Second step was to do it using directly UNO openoffice library and i'm able to convert many formats to PDF/A, including PDF without problems.
Third step, i used an opesource library : jodconverter (version 2.2.2, openoffice 3.2.1).
Jodconverter uses a TCP connection, it run soffice with these options : -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager ... and others less important parameters.
In this case (converting documents using a TCP connection) the conversion work without errors, but the content of the target PDF/A is wrong!.
The file produced (the target PDF/A) is a good PDF/A, i can read it with pdf reader, but the text contained is not the text i can find in source.
When i read the target PDF/A (via reader) i found simple text like

%PDF-1.4
1.4%Çì¢
�5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
stream
....

My problem determination is that passing the source PDF to OpenOffice via jodconverter, the document is read as a text file, not a PDF file, so the conversion is wrong.
Main difference i found between two methods (directly via uno and via jodconverter) is the connection type to openoffice.
Jodconverter uses a TCP connection, using uno directly uses a pipe connection (i think).
I've tryed also to set "MediaType" attribute to "application/pdf", but without success.
Which is my mistake?
Someone has idea of how to correct this problem?

many thanks,
OpenOffice 3.2.1, Windows7 64bit, using openoffice to convert documents to PDF/A via Java (Alfresco + jodconverter 2.2.2)
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

famagosta.. wrote:Third step, i used an opesource library : jodconverter (version 2.2.2, openoffice 3.2.1).
The problem you describe here is not the least related to this thread! Ask JODConverter related questions in the JODConverter forum. They might help you how you can use the pdfimport plugin with JODConverter.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
famagosta..
Posts: 3
Joined: Wed Aug 25, 2010 9:47 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by famagosta.. »

thanks for quick reply.
i'm pretty sure that the problem is not about jodconverter. i think the problem is the type of connection.
Can i enable some tracefile on openoffice process to see input parameters?
i've alrady debugged jodconverter and found no problems...
thx,
OpenOffice 3.2.1, Windows7 64bit, using openoffice to convert documents to PDF/A via Java (Alfresco + jodconverter 2.2.2)
famagosta..
Posts: 3
Joined: Wed Aug 25, 2010 9:47 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by famagosta.. »

Finally i found the solution.
Jod Converter did not have problems.
Openoffice did not have problems.
The problem was in the soffice command line.
I removed attribute -env:UserInstallation=... and the conversion is correct!!!
OpenOffice 3.2.1, Windows7 64bit, using openoffice to convert documents to PDF/A via Java (Alfresco + jodconverter 2.2.2)
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

famagosta.. wrote:The problem was in the soffice command line.
I removed attribute -env:UserInstallation=... and the conversion is correct!!!
As I already told you, the problem you described here is not the least related to this thread!
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
icaroqr
Posts: 1
Joined: Mon Mar 14, 2011 5:20 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by icaroqr »

Hi hol.sten !
Im having some problems with big ODT's (> 200 pages) , the converted PDF pages are comming with the string "private:stream" on their footers.
Any idea for this problem?

Im using BROffice 3.0, Win XP, Java 1.6 + Flex 3.5 tecnologies..
OpenOffice 3.1 on Windows XP / Java 1.6 / Flex 3.5
User avatar
bobscat
Banned
Posts: 1
Joined: Wed Jul 13, 2011 1:03 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by bobscat »

icaroqr wrote:the converted PDF pages are comming with the string "private:stream" on their footers.
the same problem here. any solutions?
[url=http://www.reseau-eau.educagri.fr/wakka.php?wiki=GsmRepeater]http://www.reseau-eau.educagri.fr/wakka.php?wiki=GsmRepeater[/url] - Why Gsm Repeaters Must Be Used In Your Own Home?

[url=http://dedalila.org/lepluswikini/wakka.php?wiki=HowToIncreaseGsmSignal]http://dedalila.org/lepluswikini/wakka.php?wiki=HowToIncreaseGsmSignal[/url] - How To Increase Gsm Signal
mayuri thakare
Posts: 2
Joined: Mon Aug 06, 2018 6:11 am

Re: [Java solution] Using XInputStream and XOutputStream

Post by mayuri thakare »

Hi All,

At the time of converting InputStream
xComponentLoader.loadComponentFromURL(
"private:stream", "_blank", 0, conversionProperties); takes time for execution . Please suggest me how can I minimize the execution time of it.
It takes less time if my output pdf is of 2 or 3 pages , If pdf is of more that 7 or 8 pages loadComponentFromURL() takes more time for execution .
Please suggest me , How can i increase the performance .
I am using Libre 6.0
Below is Conversion method :

Code: Select all

public  byte[] convert(OOoInputStream input, OOoOutputStream output) 
			throws Exception { 
		XMultiComponentFactory xMultiComponentFactory = xComponentContext 
				.getServiceManager(); 
		Object desktopService = xMultiComponentFactory 
				.createInstanceWithContext("com.sun.star.frame.Desktop", 
						xComponentContext); 
		XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( 
				XComponentLoader.class, desktopService); 


		PropertyValue[] conversionProperties = new PropertyValue[3]; 
		conversionProperties[0] = new PropertyValue(); 
		conversionProperties[1] = new PropertyValue(); 
		conversionProperties[2] = new PropertyValue(); 


		conversionProperties[0].Name = "InputStream"; 
		conversionProperties[0].Value = input; 
		conversionProperties[1].Name = "Hidden"; 
		conversionProperties[1].Value = Boolean.TRUE; 


		XComponent document = xComponentLoader.loadComponentFromURL( 
				"private:stream", "_blank", 0, conversionProperties); // Method takes more time .Its execution depends on number of pages in pdf or size of input stream. Need to minimize the execution time for better performance


		List<PropertyValue> filterData = new ArrayList<PropertyValue>(); 
		for (Map.Entry<String, Object> entry : filterParameters.entrySet()) { 
			PropertyValue propertyValue = new PropertyValue(); 
			propertyValue.Name = entry.getKey(); 
			propertyValue.Value = entry.getValue(); 
			filterData.add(propertyValue); 
		} 


		conversionProperties[0].Name = "OutputStream"; 
		conversionProperties[0].Value = output; 
		conversionProperties[1].Name = "FilterName"; 
		conversionProperties[1].Value = filterName; 
		conversionProperties[2].Name = "FilterData"; 
		conversionProperties[2].Value = filterData 
				.toArray(new PropertyValue[1]); 
		
		XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, 
				document); 
		xstorable.storeToURL("private:stream", conversionProperties); 


		XCloseable xclosable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, 
				document); 
		xclosable.close(true); 
		
		return output.toByteArray();
	}
Last edited by robleyd on Mon Aug 06, 2018 7:30 am, edited 1 time in total.
Reason: Add Code tags [robleyd, Moderator]
OpenOffice 3.1 on Windows 7
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: [Java solution] Using XInputStream and XOutputStream

Post by hol.sten »

mayuri thakare wrote:It takes less time if my output pdf is of 2 or 3 pages , If pdf is of more that 7 or 8 pages loadComponentFromURL() takes more time for execution.
OOo never has been good on performance. So it's totally natural that a PDF with 3 pages takes less time than a PDF with 7 or more pages. With OOo/LibreOffice it can never be the same time consumption with different page numbers.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
mayuri thakare
Posts: 2
Joined: Mon Aug 06, 2018 6:11 am

Re: [Java solution] Using XInputStream and XOutputStream

Post by mayuri thakare »

Hi hol sten

Are there any other parameters that help to increase the performance , that I can set to
xComponentLoader.loadComponentFromURL("private:stream", "_blank", 0, conversionProperties);
OpenOffice 3.1 on Windows 7
pallavi
Posts: 2
Joined: Fri Nov 29, 2019 3:15 pm

Re: [Java solution] Using XInputStream and XOutputStream

Post by pallavi »

/*
* Copyright (c) 2013 Altrusoft AB.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package se.altrusoft.docserv.controllers.ooconverter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;

/**
* Based on OOoStreamConverter from the thread <a
* href="http://forum.openoffice.org/en/forum/vi ... php?t=3801">[Java
* solution] Using XInputStream and XOutputStream</a>
*
*/
public class OOoStreamConverter {
private XComponentContext xComponentContext;

public OOoStreamConverter(XComponentContext xComponentContext) {
this.xComponentContext = xComponentContext;
}

public void convert(OOoInputStream input, OOoOutputStream output,
String filterName, Map<String, Object> filterParameters)
throws Exception {
XMultiComponentFactory xMultiComponentFactory = xComponentContext
.getServiceManager();
Object desktopService = xMultiComponentFactory
.createInstanceWithContext("com.sun.star.frame.Desktop",
xComponentContext);
XComponentLoader xComponentLoader = UnoRuntime.queryInterface(
XComponentLoader.class, desktopService);

PropertyValue[] conversionProperties = new PropertyValue[3];
conversionProperties[0] = new PropertyValue();
conversionProperties[1] = new PropertyValue();
conversionProperties[2] = new PropertyValue();

conversionProperties[0].Name = "InputStream";
conversionProperties[0].Value = input;
conversionProperties[1].Name = "Hidden";
conversionProperties[1].Value = Boolean.TRUE;

XComponent document = xComponentLoader.loadComponentFromURL(
"private:stream", "_blank", 0, conversionProperties);

List<PropertyValue> filterData = new ArrayList<PropertyValue>();
for (Map.Entry<String, Object> entry : filterParameters.entrySet()) {
PropertyValue propertyValue = new PropertyValue();
propertyValue.Name = entry.getKey();
propertyValue.Value = entry.getValue();
filterData.add(propertyValue);
}

conversionProperties[0].Name = "OutputStream";
conversionProperties[0].Value = output;
conversionProperties[1].Name = "FilterName";
conversionProperties[1].Value = filterName;
conversionProperties[2].Name = "FilterData";
conversionProperties[2].Value = filterData
.toArray(new PropertyValue[1]);

XStorable xstorable = UnoRuntime.queryInterface(XStorable.class,
document);
xstorable.storeToURL("private:stream", conversionProperties);

XCloseable xclosable = UnoRuntime.queryInterface(XCloseable.class,
document);
xclosable.close(true);

Regards:
Last edited by robleyd on Mon Dec 02, 2019 11:55 pm, edited 1 time in total.
Reason: Delete inappropriate link
openoffice 2.4 on ububtu 9.04
Post Reply