Java: Convert RTF to Word Doc/Docx and Vice Versa

RTF, short for Rich Text Format, is created by Microsoft in 1987 for the purpose of cross-platform document interchange. It is supported by many word processing applications, and the most popular one among them is Microsoft Word. In certain circumstances, you may need to convert an RTF document to Word Doc/Docx format or convert a Word Doc/Docx document to RTF. In this article, you will learn how to accomplish this task using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert RTF to Word Doc/Docx

The following are the steps to convert an RTF document to Word Doc/Docx using Spire.Doc for Java:

  • Create a Document instance.
  • Load an RTF document using Document.loadFromFile(String, FileFormat) method.
  • Save the RTF to Doc/Docx format using Document.saveToFile(String, FileFormat) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RtfToDocDocx {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a RTF document
        document.loadFromFile("Input.rtf", FileFormat.Rtf);

        //Save the document to Doc
        document.saveToFile("toDoc.doc", FileFormat.Doc);

        //Save the document to Docx
        document.saveToFile("toDocx.docx", FileFormat.Docx_2013);
    }
}

Java: Convert RTF to Word Doc/Docx and Vice Versa

Convert Word Doc/Docx to RTF

The steps to convert a Word Doc/Docx document to RTF is very similar with that of the above example:

  • Create a Document instance.
  • Load a Doc or Docx document using Document.loadFromFile(String) method.
  • Save the Doc/Docx document to RTF using Document.saveToFile(String, FileFormat) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DocDocxToRtf {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a Docx document
        document.loadFromFile("input.docx");

        //Load a Doc document
        //document.loadFromFile("sample.doc");

        //Save the document to RTF
        document.saveToFile("toRTF.rtf", FileFormat.Rtf);
    }
}

Java: Convert RTF to Word Doc/Docx and Vice Versa

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.