In our daily work, we often encounter the need to convert Word documents to other formats. For example, we need to save contracts and invoices as PDF after being made in Word, and we also need to save Word documents as HTML if we want to browse them in a browser. In this article, I am going to show you how to convert Word to PNG, PDF, HTML, XPS, TXT, RTF, etc. using Spire.Doc for Java.
Installing Spire.Doc.jar
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.
<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>4.6.2</version>
</dependency>
</dependencies>
Example 1. Convert Word to PNG
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class WordToPng {
public static void main(String[] args) throws IOException {
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.loadFromFile("input.docx");
//Loop through the pages
for (int i = 0; i < doc.getPageCount(); i++) {
//Save all pages in the Word document to .png images
BufferedImage image = doc.saveToImages(i, ImageType.Bitmap);
File file = new File("out/" + String.format(("Img-%d.png"), i));
ImageIO.write(image, "PNG", file);
}
}
}
Example 2. Convert Word to PDF
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
public class WordToPdf {
public static void main(String[] args) {
//Create Document object
Document doc = new Document();
//Load the file from disk.
doc.loadFromFile("input.docx");
//Create an instance of ToPdfParameterList.
ToPdfParameterList ppl=new ToPdfParameterList();
//Embeds full fonts by default when IsEmbeddedAllFonts is set to true.
ppl.isEmbeddedAllFonts(true);
//Set setDisableLink to true to remove the hyperlink effect for the result PDF page.
//Set setDisableLink to false to preserve the hyperlink effect for the result PDF page.
ppl.setDisableLink(true);
//Set the output image quality as 40% of the original image. 80% is the default setting.
doc.setJPEGQuality(40);
//Save to file.
doc.saveToFile("ToPDF.pdf", ppl);
}
}
Example 3. Convert Word to HTML
import com.spire.doc.*;
public class WordToHtml {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("input.docx");
//Save the document as HTML
document.saveToFile("output/toHtml.html", FileFormat.Html);
}
}
Example 4. Convert Word to XPS and Other Formats
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class WordToXps {
public static void main(String[] args) {
//Create a Document object.
Document doc = new Document();
//Load the Word document.
doc.loadFromFile("input.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);
}
}
Write a comment