OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Conversion of File Types Problem

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
blesnyder
General User
General User


Joined: 09 Jun 2005
Posts: 24

PostPosted: Wed Jun 15, 2005 1:07 pm    Post subject: Conversion of File Types Problem Reply with quote

I'm working on using OO to generate documents which may be saved in whatever format the client needs, Word, Wordperfect, OO, etc. In my initial testing, I've been loading .odt and .sxw and saving .doc or .rtf formats. When I attempt to open the newly saved files in Word, it can't read them. The newly saved documents can be opened fine in OO, however. Here's my code, can anyone point me in the right direction?

String fileName = "c:/Test.sxw";
java.io.File sourceFile = new java.io.File(fileName);
StringBuffer sLoadFileUrl = new StringBuffer("file:///");
sLoadFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
PropertyValue[] loadProps = new PropertyValue[2];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";
loadProps[0].Value = new Boolean(false);
loadProps[1] = new PropertyValue();
loadProps[1].Name = "DocumentTitle";
loadProps[1].Value = "Testing.odt";
XComponent xDoc = xComponentLoader.loadComponentFromURL(sLoadFileUrl.toString(), "_blank",
0, loadProps );
//..lots of processing goes here...

String sURL = "file:///c:/done.rtf";
com.sun.star.beans.PropertyValue[] lProperties =
new com.sun.star.beans.PropertyValue[2];
lProperties[0] = new com.sun.star.beans.PropertyValue();
lProperties[0].Name = "FilterName ";
lProperties[0].Value = "Rich Text Format";
lProperties[1] = new com.sun.star.beans.PropertyValue();
lProperties[1].Name = "Overwrite";
lProperties[1].Value = new Boolean(true);

com.sun.star.frame.XStorable xStore = (com.sun.star.frame.XStorable)UnoRuntime.queryInterface (
com.sun.star.frame.XStorable .class, xDoc);

xStore.storeAsURL (sURL, lProperties);
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Jun 15, 2005 2:23 pm    Post subject: Reply with quote

DannyB has written a file conversion macro. You can find it here:
http://sourceforge.net/project/showfiles.php?group_id=87718&package_id=95532
To go to the general web site where this is use www.ooomacros.org
Back to top
View user's profile Send private message
blesnyder
General User
General User


Joined: 09 Jun 2005
Posts: 24

PostPosted: Wed Jun 15, 2005 2:36 pm    Post subject: Reply with quote

The macro works great, but what I need to do is do the filtering from my java application. The code I'm using is based upon the documentation, so I'm puzzled as to why it isn't readable by Word. I've manually done a Save As to Word 97 and RTF formats from inside OO, and those are readable by Word as well, which leaves me to conclude that I've done something wrong in my code.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Jun 16, 2005 6:47 am    Post subject: Reply with quote

It looks like your program does exactly as you describe. I see nothing wrong.

The problem is that Word doesn't like the RTF that OOo is generating.
(Have you actually opened the RTF file in a text editor to make sure it really looks like RTF?)

One other minor note, it should not be necessary to specify the property Hidden=False when loading the document. I would expect that you only need the Hidden property if you want to set it to True.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
blesnyder
General User
General User


Joined: 09 Jun 2005
Posts: 24

PostPosted: Thu Jun 16, 2005 7:24 am    Post subject: Reply with quote

You are correct, Word doesn't like the file. I've looked at it, and it looks to me like an RTF, but I'm no expert on what an RTF is supposed to look like. What I find interesting is that if I manually "Save as" to RTF or DOC out of OpenOffice, Word reads that fine. I've been reviewing example code, and have begun to wonder if I'm not doing enough set-up in my code? For example, the code in the example FunctionHelper reads in part:

com.sun.star.lang.XServiceInfo xInfo = (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface(
com.sun.star.lang.XServiceInfo.class,
xDocument);

if(xInfo!=null)
{
// Find out possible filter name.
String sFilter = null;
if(xInfo.supportsService("com.sun.star.text.TextDocument")==true)
sFilter = new String("HTML (StarWriter)");
else
if(xInfo.supportsService("com.sun.star.text.WebDocument")==true)
sFilter = new String("HTML");
else
if(xInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")==true)
sFilter = new String("HTML (StarCalc)");

// Check for existing state of this filter.
if(sFilter!=null)
{
com.sun.star.lang.XMultiServiceFactory xSMGR = OfficeConnect.getSMGR();

com.sun.star.container.XNameAccess xFilterContainer = (com.sun.star.container.XNameAccess)UnoRuntime.queryInterface(
com.sun.star.container.XNameAccess.class,
xSMGR.createInstance("com.sun.star.document.FilterFactory"));

if(xFilterContainer.hasByName(sFilter)==false)
sFilter=null;
}

What I'm wondering is if I need to explicitly make sure that the filter exists? I've tried implementing this code, but the line:

com.sun.star.lang.XMultiServiceFactory xSMGR = OfficeConnect.getSMGR();

won't compile, I don't have the OfficeConnect method. I've searched the example, and haven't found where OfficeConnect is created, and I'm not having any luck finding it.

Could this theory be correct, that I need to ensure that I have the filter before using it? Oh, I have the hidden property in there to remind me to set it to true when I eventually put this code into production.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Jun 16, 2005 7:33 am    Post subject: Reply with quote

blesnyder wrote:
What I find interesting is that if I manually "Save as" to RTF or DOC out of OpenOffice, Word reads that fine.

That is very interesting indeed.

I would suppose that maybe you are using the wrong filter?

Here is a list, and how to produce a list of filters....

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
View user's profile Send private message
blesnyder
General User
General User


Joined: 09 Jun 2005
Posts: 24

PostPosted: Thu Jun 16, 2005 7:55 am    Post subject: Reply with quote

I added the following code:

String sFilter = null;
if(xInfo.supportsService("com.sun.star.text.TextDocument")==true)
sFilter = new String("MS Word 97");

com.sun.star.container.XNameAccess xFilterContainer = (com.sun.star.container.XNameAccess)UnoRuntime.queryInterface(
com.sun.star.container.XNameAccess.class,
xMSF.createInstance("com.sun.star.document.FilterFactory"));

if(xFilterContainer.hasByName(sFilter)==false)
sFilter=null;

sFilter gets populated, but the document still isn't Word readable. Argh!
Back to top
View user's profile Send private message
blesnyder
General User
General User


Joined: 09 Jun 2005
Posts: 24

PostPosted: Thu Jun 16, 2005 8:27 am    Post subject: Reply with quote

I figured it out. The line:

lProperties[0].Value = "MS Word 97";

had been:

lProperties[0].Value = "MS Word 97 "; //Note there's a trailing space

The trailing space didn't raise any errors, but it sure did hose up the output file!

Thanks for all your help.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group