Java: Convert Word to XPS, XML, RTF, TXT and SVG

In daily work, converting a Word document to other formats can be extremely frequent. For example, sometimes you may need to convert a Word document to XML to store and organize data; on some occasions, you may also need to convert Word to SVG for sharing graphics contents on the Internet. In this article, you will learn how to convert Word to XPS, XML, RTF, TXT and SVG 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.4.6</version>
    </dependency>
</dependencies>
    

Convert Word to XPS, XML, RTF, TXT and SVG

The following are the main steps to convert Word to XPS, XML, RTF, TXT and SVG.

  • Create a Document object.
  • Load the document using Document.loadFromFile() method.
  • Use Document.saveToFile() method to save the document as SVG, RTF, XPS, XML and TXT respectively.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.ImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertWordToOtherFormats {

    public static void main(String[] args) throws IOException {

        //Create a Document object.
        Document doc = new Document();

        //Load the Word document.
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //Save Word as SVG.
        doc.saveToFile("output/ToSVG.svg",FileFormat.SVG);

        //Save Word as RTF.
        doc.saveToFile("output/ToRTF.rtf",FileFormat.Rtf);

        //Save Word as XPS.
        doc.saveToFile("output/ToXPS.xps",FileFormat.XPS);

        //Save Word as XML.
        doc.saveToFile("output/ToXML.xml",FileFormat.Xml);

        //Save Word as TXT.
        doc.saveToFile("output/ToTXT.txt",FileFormat.Txt);
    }
}

The original Word file:

Java: Convert Word to XPS, XML, RTF, TXT and SVG

The generated XPS file:

Java: Convert Word to XPS, XML, RTF, TXT and SVG

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.