| View previous topic :: View next topic |
| Author |
Message |
peterv6i General User


Joined: 13 Jun 2005 Posts: 36
|
Posted: Mon Jun 13, 2005 12:20 am Post subject: Convert SXW to DOC with JAVA |
|
|
Is there any API to convert SXW to DOC format?
I would like to do it with Java because I have SXW files stored in BLOB field and when the user choice is MS Word then the file must be converted with Java api do DOC format.
best regards
Peter |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
|
| Back to top |
|
 |
peterv6i General User


Joined: 13 Jun 2005 Posts: 36
|
Posted: Mon Jun 13, 2005 12:59 am Post subject: |
|
|
SXW to DOC..
I wolud like to convert SXW to DOC. only this... (and not DOC to SXW or PDF)... |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
Posted: Mon Jun 13, 2005 1:13 am Post subject: |
|
|
| SXW to DOC works exactly like DOC to SXW ... exporting to PDF is somewaht defferent |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3533 Location: Hamburg, Germany
|
|
| Back to top |
|
 |
9point9 Moderator

Joined: 31 Aug 2004 Posts: 3875 Location: UK
|
Posted: Mon Jun 13, 2005 2:43 pm Post subject: |
|
|
| peterv6i wrote: | | I wolud like to convert SXW to DOC. only this |
There is a way as explained but why would you want to automate conversion of an open file format to a closed one? In saving to DOC you may loose some information so I'd be careful what you use it on. _________________ Arch Linux
OOo 3.2.0
OOoSVN, change control for OOo documents:
http://sourceforge.net/projects/ooosvn/ |
|
| Back to top |
|
 |
peterv6i General User


Joined: 13 Jun 2005 Posts: 36
|
Posted: Mon Jun 13, 2005 10:52 pm Post subject: |
|
|
Why SXW to DOC..
In my company we used Open Office as alternative as MS Office.. The biggest assicuration in our country has installed open office on all 500 clients.. Our software based on Oracle (IAS) use Open Office SXW file format as Report Engine (because user can change contet ) and then save the report in BLOB file.. Now the project is finished and all works fine..
We have now a second customer which use the same application but with Microsoft Office programs.. So I must implement a conversion between SXW and DOC.. (because all reports are made in Open Office) |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Tue Jun 14, 2005 6:41 am Post subject: |
|
|
Once you have the document model....
- queryInterface for the XStorable interface.
- call storeToUrl() to save it
- pass an array of PropertyValue to storeToUrl().
- Set the property Name to "Filter". Set the property Value to "MS Word 97".
Something roughly like this....
| Code: |
PropertyValue filterProperties = new PropertyValue[1];
filterProperties[0].Name = "Filter";
filterProperties[0].Value = "MS Word 97";
XStorable oDoc_xStorable = queryInterface( oDoc, XStorable );
oDoc_xStorable.storeToUrl( sUrl, filterProperties );
|
Yes, I know I oversimplified that queryInterface just a bit.
What you are doing here is using an Export filter.
Similarly, when you call loadComponentFromURL(), you can pass an array of PropertyValue with the name of an Import filter.
By using Import filters, you can get OOo to load documents of other formats, such as Excel. By using Export filters, you can save documents into other formats, such as PDF.
See Also....
Current list of past Conversion examples.
http://www.oooforum.org/forum/viewtopic.php?t=12922
Filter list
http://www.oooforum.org/forum/viewtopic.php?t=3549 _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
peterv6i General User


Joined: 13 Jun 2005 Posts: 36
|
Posted: Wed Jun 15, 2005 12:39 am Post subject: |
|
|
Ok. here is SXW to DOC
"delete some import which are not used..."
| Code: | package openoffice;
import com.sun.star.awt.FontWeight;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import com.sun.star.awt.Point;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyState;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertyState;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.servicemanager.ServiceManager;
import com.sun.star.connection.XConnection;
import com.sun.star.connection.XConnector;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XIndexReplace;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.container.XNamed;
import com.sun.star.drawing.XDrawPageSupplier;
import com.sun.star.drawing.XShape;
import com.sun.star.drawing.XShapeGrouper;
import com.sun.star.drawing.XShapes;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.style.NumberingType;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.table.XCell;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.ReferenceFieldPart;
import com.sun.star.text.ReferenceFieldSource;
import com.sun.star.text.TextColumn;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.XAutoTextContainer;
import com.sun.star.text.XAutoTextEntry;
import com.sun.star.text.XAutoTextGroup;
import com.sun.star.text.XBookmarksSupplier;
import com.sun.star.text.XDependentTextField;
import com.sun.star.text.XDocumentIndex;
import com.sun.star.text.XFootnote;
import com.sun.star.text.XFootnotesSupplier;
import com.sun.star.text.XPageCursor;
import com.sun.star.text.XParagraphCursor;
import com.sun.star.text.XReferenceMarksSupplier;
import com.sun.star.text.XRelativeTextContentInsert;
import com.sun.star.text.XSentenceCursor;
import com.sun.star.text.XSimpleText;
import com.sun.star.text.XText;
import com.sun.star.text.XTextColumns;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextField;
import com.sun.star.text.XTextFieldsSupplier;
import com.sun.star.text.XTextFrame;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XTextSection;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextTableCursor;
import com.sun.star.text.XTextTablesSupplier;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.text.XWordCursor;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.XInterface;
import com.sun.star.uno.XNamingService;
import com.sun.star.util.XRefreshable;
import com.sun.star.view.XPrintable;
import java.lang.Math;
import java.util.Hashtable;
import java.util.Random;
public class SXWtoDOC
{
private static String sOutputDir;
private XComponentContext mxRemoteContext = null;
private XMultiComponentFactory mxRemoteServiceManager = null;
private XTextDocument mxDoc = null;
private XMultiServiceFactory mxDocFactory = null;
private XMultiServiceFactory mxFactory = null;
private XPropertySet mxDocProps = null;
private XText mxDocText = null;
private XTextCursor mxDocCursor = null;
private XTextContent mxFishSection = null;
private Random maRandom = null;
//private String unoUrl = "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
private String unoUrl = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
public SXWtoDOC()
{
try
{
System.out.println("Pretvorba.. sxw v doc");
System.out.println("uno url: " + unoUrl);
mxRemoteServiceManager = this.getRemoteServiceManager(unoUrl);
// retrieve the Desktop object, we need its XComponentLoader
Object desktop = mxRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
mxRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
desktop);
PropertyValue[] loadProps = new PropertyValue[0];
java.io.File sourceFile = new java.io.File("c:/peter.sxw");
StringBuffer sLoadFileUrl = new StringBuffer("file:///");
sLoadFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
XComponent xDoc = xComponentLoader.loadComponentFromURL(sLoadFileUrl.toString(),
"_blank", 0, loadProps);
if (xDoc != null)
{
String sOutputDir ="c:/";
sourceFile = new java.io.File(sOutputDir);
StringBuffer sStoreFileUrl = new StringBuffer();
sStoreFileUrl.append(sourceFile.toURL().toString());
sStoreFileUrl.append("somepopularfileformat.doc");
storeDocComponent(xDoc, sStoreFileUrl.toString());
//printDocComponent(xDoc);
}
}
catch (Exception exc)
{
System.out.println("Napaka: " + exc.toString());
}
}
private XMultiComponentFactory getRemoteServiceManager(String unoUrl)
throws java.lang.Exception
{
if (mxRemoteContext == null)
{
XComponentContext xLocalContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
xLocalContext);
// query XUnoUrlResolver interface from urlResolver object
XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class,
urlResolver);
// Second step: use xUrlResolver interface to import the remote StarOffice.ServiceManager,
// retrieve its property DefaultContext and get the remote servicemanager
Object initialObject = xUnoUrlResolver.resolve(unoUrl);
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
initialObject);
Object context = xPropertySet.getPropertyValue("DefaultContext");
mxRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class,
context);
}
return mxRemoteContext.getServiceManager();
}
protected void storeDocComponent(XComponent xDoc, String storeUrl)
throws java.lang.Exception
{
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,
xDoc);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "MS Word 97";
xStorable.storeAsURL(storeUrl, storeProps);
}
public static void main(String[] args)
{
SXWtoDOC SXWtoDOC1 = new SXWtoDOC();
}
}
|
|
|
| Back to top |
|
 |
Ivan_ Newbie

Joined: 06 Jul 2005 Posts: 2 Location: Tula, Russia
|
Posted: Wed Jul 06, 2005 6:10 am Post subject: Convert to PDF |
|
|
Hello!
How can I get PDF-file from sxw?
I`m trying to use the code resulted above and change the filter name to "writer_pdf_Export". As result we have an exception. What should i change in source code? Help please...
Exception:
com.sun.star.io.IOException:
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readThrowable(Unmarshal.java:315)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readAny(Unmarshal.java:157)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readObject(Unmarshal.java:404)
at com.sun.star.lib.uno.protocols.urp.urp.readReply(urp.java:192)
at com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:308)
at com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:607)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDispatcher.invoke(java_remote_bridge.java:184)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDispatcher.run(java_remote_bridge.java:175) |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3533 Location: Hamburg, Germany
|
Posted: Wed Jul 06, 2005 6:27 am Post subject: Re: Convert to PDF |
|
|
| Ivan_ wrote: | | What should i change in source code? Help please... |
Replace "xStorable.storeAsURL" with "xStorable.storeToURL".
With kind regards
hol.sten |
|
| Back to top |
|
 |
Ivan_ Newbie

Joined: 06 Jul 2005 Posts: 2 Location: Tula, Russia
|
Posted: Fri Jul 08, 2005 3:27 am Post subject: Re: Convert to PDF |
|
|
[quote="hol.sten"] | Ivan_ wrote: | | What should i change in source code? Help please... |
Replace "xStorable.storeAsURL" with "xStorable.storeToURL".
It works!!! Thanks.
Best regards, Ivan |
|
| Back to top |
|
 |
peterv6i General User


Joined: 13 Jun 2005 Posts: 36
|
Posted: Thu Jul 14, 2005 2:26 am Post subject: |
|
|
This is the code I have used to convert SXW to other formats with JSP page..
this is the bean which i call from jsp:
| Code: |
package in2;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
// UNO API
import com.sun.star.bridge.XUnoUrlResolver;
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.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
/**
* <p>Title: In2 Process</p>
* <p>Description: Izvajanje procesov v JSP-ju</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: In2 d.o.o</p>
* @author Peter Valenčič
* @version 1.0
*/
// JDK API
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
public class ConvertFile
{
private static String sOutputDir;
private String fromFile;
private String toFolder;
private String unoURL = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";
private XComponentContext mxRemoteContext = null;
private XMultiComponentFactory mxRemoteServiceManager = null;
private XPropertySet mxDocProps = null;
private String useFilter = "DOC";
/**
* default constructor
*/
public ConvertFile()
{
}
public static void main(String[] args)
{
ConvertFile convertFile1 = new ConvertFile();
}
public void setUseFilter(String useFilter)
{
this.useFilter = useFilter.toUpperCase();
}
public String getUseFilter()
{
return useFilter.toUpperCase();
}
public void setFromFile(String fromFile)
{
this.fromFile = fromFile;
}
public String getFromFile()
{
return fromFile;
}
public void setToFolder(String toFolder)
{
this.toFolder = toFolder;
}
public String getToFolder()
{
return toFolder;
}
public void setUnoURL(String unoURL)
{
this.unoURL = unoURL;
}
public String getUnoURL()
{
return unoURL;
}
////////////////////////////////////////////////////////////////////////////////
/**
* XMultiComponentFactory
* @param unoUrl URL do servisa OpenOffice
* @return
* @throws java.lang.Exception
*/
private XMultiComponentFactory getRemoteServiceManager(String unoUrl)
throws java.lang.Exception
{
if (mxRemoteContext == null)
{
XComponentContext xLocalContext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
xLocalContext);
XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class,
urlResolver);
Object initialObject = xUnoUrlResolver.resolve(unoUrl);
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
initialObject);
Object context = xPropertySet.getPropertyValue("DefaultContext");
mxRemoteContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class,
context);
}
return mxRemoteContext.getServiceManager();
}
////////////////////////////////////////////////////////////////////////////////
/**
* storeDocComponent metoda shrani iz sxw v msWord
* @param xDoc
* @param storeUrl
* @throws java.lang.Exception
*/
protected void storeDocComponent(XComponent xDoc, String storeUrl)
throws java.lang.Exception
{
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class,
xDoc);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
if (this.getUseFilter().equalsIgnoreCase("DOC"))
{
storeProps[0].Value = "MS Word 97";
}
else if (this.getUseFilter().equalsIgnoreCase("RTF"))
{
storeProps[0].Value = "Rich Text Format";
}
else if (this.getUseFilter().equalsIgnoreCase("PDF"))
{
storeProps[0].Value = "writer_pdf_Export";
}
//zamenjamo metodo če je izbran PDF format
if (this.getUseFilter().equalsIgnoreCase("PDF"))
{
xStorable.storeToURL(storeUrl, storeProps);
}
else
{
xStorable.storeAsURL(storeUrl, storeProps);
}
XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
xStorable);
xcomponent.dispose();
}
////////////////////////////////////////////////////////////////////////////////
/**
* Konvertira datoteko iz sxw v doc
* @throws Exception
*/
public void convert() throws Exception
{
try
{
System.out.println("začetek...");
mxRemoteServiceManager = this.getRemoteServiceManager(this.getUnoURL());
Object desktop = mxRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
mxRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
desktop);
PropertyValue[] loadProps = new PropertyValue[0];
java.io.File sourceFile = new java.io.File(this.getFromFile());
StringBuffer sLoadFileUrl = new StringBuffer("file:///");
sLoadFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
XComponent xDoc = xComponentLoader.loadComponentFromURL(sLoadFileUrl.toString(),
"_blank", 0, loadProps);
if (xDoc != null)
{
sourceFile = new java.io.File(this.toFolder);
java.io.File ff = new java.io.File(this.getFromFile());
String dat = ff.getName();
String tof = dat.substring(0, dat.length() - 4);
StringBuffer sStoreFileUrl = new StringBuffer();
sStoreFileUrl.append(sourceFile.toURL().toString());
//tukaj implementiraj filtre
String koncnica = "";
if (this.getUseFilter().equalsIgnoreCase("DOC"))
{
koncnica = ".DOC";
}
else if (this.getUseFilter().equalsIgnoreCase("RTF"))
{
koncnica = ".RTF";
}
else if (this.getUseFilter().equalsIgnoreCase("PDF"))
{
koncnica = ".PDF";
}
sStoreFileUrl.append(tof + koncnica);
storeDocComponent(xDoc, sStoreFileUrl.toString()); // sStoreFileUrl.toString());
}
else
{
throw new Exception("Exception from ConvertFile: xDoc = null ");
}
}
catch (Exception exc)
{
System.out.println("Napaka: " + exc.toString());
throw new Exception("Exception from ConvertFile: " +
exc.toString());
}
System.out.println("konec..");
//System.exit(0);
}
} |
here is the JSP file...
the structure of my web folders are
/htdocs/peter/ here I store my JSP pages
/htdocs/peter/blob/ here I store my sxw file and then also the converted files are stored here..
the jsp page:
| Code: | <%@ page contentType="text/html; charset=ISO8859-2" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="in2.*" %>
<!--------------------------------------------------------------------------->
<html>
<head>
<title>Proces testiranje</title>
</head>
<%
ConvertFile fc = new ConvertFile();
fc.setUnoURL("uno:socket,host=172.16.201.40,port=8100;urp;StarOffice.ServiceManager");
out.println("<li>uno: "+ fc.getUnoURL());
fc.setFromFile("d:/oracle/isuites/apache/apache/htdocs/peter/blob/22001344.sxw");
out.println("<li>from: "+ fc.getFromFile());
out.println("<li>from: "+ fc.getFromFile().substring(0,fc.getFromFile().length()-4));
fc.setToFolder("d:/oracle/isuites/apache/apache/htdocs/peter/blob/");
fc.setUseFilter("PDF");
out.println("<li>filter: "+ fc.getUseFilter());
out.println("<li>to: "+ fc.getToFolder());
try
{
fc.convert();
}
catch(Exception eee)
{
out.println("<li><b>Exception</b>: "+eee.toString());
}
%>
<body>
<li>ok
</form>
</body>
</html>
|
The metods I call are:
| Code: |
fc.setUnoURL("uno:socket,host=172.16.201.40,port=8100;urp;StarOffice.ServiceManager");
fc.setFromFile("d:/oracle/isuites/apache/apache/htdocs/peter/blob/22001344.sxw");
fc.setToFolder("d:/oracle/isuites/apache/apache/htdocs/peter/blob/");
fc.setUseFilter("PDF");
try
{
fc.convert();
}
catch(Exception eee)
{
out.println("<li><b>Exception</b>: "+eee.toString());
}
|
setFromFile, here we must enter the full path with filename on the server side
setToFolder, is only the path on server side where we want the converted document
setUseFiler, here we specified the filter ("DOC" for MSOffice, "PDF" for PFD, "RTF" for RTF)
fc.Convert, is the method which execute the conversion of document. |
|
| Back to top |
|
 |
kapil.dhakad Power User


Joined: 07 Jul 2005 Posts: 91 Location: INDIA
|
Posted: Fri Oct 06, 2006 12:25 pm Post subject: multiple pages creation, due to open office section in .sxw |
|
|
I tried for conversion .SXW to .DOC but..
if .SXW contain 'section' from open office then it is creating multiple pages in .DOC file |
|
| Back to top |
|
 |
|