DEV Community

CodeSharing
CodeSharing

Posted on

Converting Word to PDF, PNG, SVG, RTF, XPS, XML in Java Application

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 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.

<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>
Enter fullscreen mode Exit fullscreen mode

【Example 1】Converting Word to PDF

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

【Example 2】Converting Word to Other Formats

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);
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)