Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All, I am new to this forum. In my application I came across a new requirement where I have to convert RTF files to Word and PDF formatted files. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2

    How to convert RTF files to MS Word files using java?


    Hi All,

    I am new to this forum. In my application I came across a new requirement where I have to convert RTF files to Word and PDF formatted files.

    I am searching for an API using which my java application can able to convert the above specified formats.

    Please guide me in right direction? Any clue is also highly appreciated.

    Its urgent.

    Thanks in advance.

    MSP

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,425
    Hi, I am not a dev and the following is by no means light weight.
    But maybe you can utilize OpenOffice and its UNO api to convert between any format OpenOffice can import and export.
    api: Overview (Java UNO Runtime Reference)
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2
    Hi,

    I tried with below code :

    Code:
    		
    import com.sun.star.beans.PropertyValue;
    import com.sun.star.beans.XPropertySet;
    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.text.XParagraphCursor;
    import com.sun.star.text.XText;
    import com.sun.star.text.XTextDocument;
    import com.sun.star.uno.Exception;
    import com.sun.star.uno.UnoRuntime;
    import com.sun.star.util.XCloseable;
    
    public class Converter2 { 
    
    	public static void main(String[] args)
    	{
    		try{
    		// connect to open office
            	com.sun.star.uno.XComponentContext xContext;
    			xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            	com.sun.star.lang.XMultiComponentFactory xMCF = xContext.getServiceManager();
            	Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
            	XComponentLoader xCLoader = (com.sun.star.frame.XComponentLoader) UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop);
            	
            	// create document
            	XComponent document = xCLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0,new PropertyValue[0]);
            	XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class,document);
            	XText xText = xTextDocument.getText();
            	
            	// create a paragraph cursor
            	XParagraphCursor xParagraphCursor = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, xText.createTextCursor());
            	
            	// add some text
            	xText.insertString(xText.getEnd(), "My First OpenOffice Document",false);
            	
            	// style the paragraph
            	XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xParagraphCursor);
            	xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
            	
    
            	// add some text
            	xText.insertString(xText.getEnd(),"rThis is the first line in my paragraph. " + "Some more text, just to add some text to this document. ",
            	false);
            	xPropertySet.setPropertyValue("ParaStyleName", "Text body");
    
            	// Add another heading paragraph
            	xText.insertString(xText.getEnd(), "rSecond heading", false);
            	xPropertySet.setPropertyValue("ParaStyleName", "Heading 2");
    
            	// And another text body paragraph
            	xText.insertString(xText.getEnd(),"rThis is the second normal paragraph.", false);
            	xPropertySet.setPropertyValue("ParaStyleName", "Text body");
            	
            	// save document
            	XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
            	PropertyValue[] storeProps = new PropertyValue[0];
            	xStorable.storeAsURL("file:///tmp/first_document.odt", storeProps);
            	
            	// export document to pdf
            	storeProps = new PropertyValue[1];
            	storeProps[0] = new PropertyValue();
            	storeProps[0].Name = "FilterName";
            	storeProps[0].Value = "writer_pdf_Export";
            	xStorable.storeToURL("file:///tmp/first_document.pdf", storeProps);
            	
            	// close document
            	XCloseable xcloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, document);
            	xcloseable.close(false);
    		} catch (BootstrapException e) {
    			e.printStackTrace();
    		}
    		catch( Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    and I am getting below Exception:

    com.sun.star.comp.helper.BootstrapException: no office executable found!
    at com.sun.star.comp.helper.Bootstrap.bootstrap(Boots trap.java:253)
    at Converter2.main(Converter2.java:22)


    System Environment:
    OS : Windows XP
    IDE : Eclipse 3.5
    JDK: 1.6
    Open Office Version: 3.2 (latest)

    Below configurations made manually:

    set the PATH environment variable to : C:\Program Files\OpenOffice.org 3\program to search for soffice.exe

    &

    set CLASSPATH environment variable to :
    D:\test\Jars\unoil-2.2.0.jar;
    D:\test\Jars\juh-2.3.1.jar;
    D:\test\Jars\jurt-2.3.1.jar;
    D:\test\Jars\ridl-2.3.1.jar;
    C:\Program Files\OpenOffice.org 3\Basis\program\classes

    I installed C:\OOo\OOffice3\OpenOffice.org3 at location.

    Please help me to clear this error and able to create a sample word document.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,425
    As I said, I am not a developer, even less a windows developer.
    But as the output complains about binary not found, then either
    • the path is wrong
    • the path needs to be escaped, as there are white spaces in it (also I am not sure about / versus \ directory delimiter
    • com.sun.star.comp.helper.BootstrapException *maybe* doesnt use path
    You must always face the curtain with a bow.

  6. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Try, with and without the quotes,
    Code:
    SET UNO_PATH="C:\Program Files\OpenOffice.org 3\program"
    , i.e, to the dir where soffice.exe is found.
    Last edited by nmset; 09-21-2010 at 01:09 PM.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •