Converting Word to PDF, PNG, SVG, RTF, XPS, XML in Java Application
Tags: java, word, pdf, xml
Published: 2020-08-11 08:18:40

When manipulating Word document, we often need to convert it from doc/docx to other file formats to meet the needs of different work occasions. This article will introduce how to convert Word documents to PDF, PNG, SVG, RTF, XPS, XML, TXT by using Free Spire.Doc for Java.

**Installation**
**Method 1:** Download the [Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html) and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

**Method 2:** You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

```java
<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>2.7.3</version>
   </dependency>
</dependencies>
```


**【Example 1】Converting Word to PDF**

```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ToPdfParameterList;

public class WordtoPDF {
    public static void main(String[] args)  {

        //Load the Word Document
        Document doc = new Document();
        doc.loadFromFile("test1.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",FileFormat.PDF);
    }
}
```

![](https://dev-to-uploads.s3.amazonaws.com/i/szszndx59z6x03702tls.png)



**【Example 2】Converting Word to Other Formats**

```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 {

        //Load the Word Document
        Document doc = new Document();
        doc.loadFromFile("test1.docx");

        //Save the first page to a BufferedImage
        BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);

        //Write the image data to a .png file
        File file= new File("out/ToPNG.png");
        ImageIO.write(image, "PNG", file);

        //Save the Word document as SVG format
        doc.saveToFile("out/ToSVG.svg",FileFormat.SVG);

        //Save the Word document as RTF format
        doc.saveToFile("out/ToRTF.rtf",FileFormat.Rtf);

        //Save the Word document as XPS format
        doc.saveToFile("out/ToXPS.xps",FileFormat.XPS);

        //Save the Word document as XML format
        doc.saveToFile("out/ToXML.xml",FileFormat.Xml);

        //Save the Word document as TXT format
        doc.saveToFile("out/ToTXT.txt",FileFormat.Txt);
    }
}

```

![](https://dev-to-uploads.s3.amazonaws.com/i/o69wc8ebfshkuayig7nz.png)