Friday, 22 October 2021

Convert Word to HTML/SVG/RTF/XPS/XML/TXT in Java

When we manipulate Word documents in daily life, we may need to convert Word to other document formats for some purposes. This tutorial will demonstrate how to realize the function of converting Word to HTML/SVG/RTF/XPS/XML/TXT in Java using Free Spire.Doc for Java.

About Free Spire.Doc for Java

Free Spire.Doc for Java is a free and professional Java Word API that enables Java applications to create, manipulate, convert and print Word documents without installing Microsoft Office.

Install Free Spire.Doc for Java

First of all, we need 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Convert Word to HTML/SVG/RTF/XPS/XML/TXT

Free Spire.Doc for Java provides Document.saveToFile() method to convert Word to other document formats. Some steps to do it are listed below.

l  Create a Document instance.

l  Load a Word sample document using Document.loadFromFile() method.

l  Save the document as HTML/SVG/RTF/XPS/XML/TXT using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class Conversion {
public static void main(String[] args) {
//Create a Document object.
Document doc = new Document();

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

//Save Word as HTML
doc.saveToFile("output/toHtml.html", FileFormat.Html);

//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 following is a screenshot of conversion results.





No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...