Skip to main content

Convert Word Document to Other File Formats (Pdf, Image, Html, Txt etc.) in Java

Convert Word document to other file formats is undoubtedly one of the most common requirements when we working with Word document. In this article, we’ll learn how to convert Word document to other file formats programmatically in Java by using Free Spire.Doc for Java library.

Free Spire.Doc for Java library let us convert Word document to a series of file formats easily and effectively without having Microsoft Office to be installed on our system. The following conversions are supported by Free Spire.Doc for Java:
  • Microsoft Word file format conversions
  • Convert Word to Pdf
  • Convert Word to Image
  • Convert Word to Html
  • Convert Word to Rtf
  • Convert Word to Svg
  • Convert Word to Txt
  • Convert Word to Xps
  • Convert Word to Pcl
  • Convert Word to Post Script
  • Convert Word to Odt
  • Convert Word to Epub
  • Convert Word to WordML/WordXML

In the following examples, we’ll see how to convert Word document to Pdf, Image, Html and Svg.

Convert Word to Pdf

The following example demonstrates how to convert a Word document to Pdf file format.
public class ConvertWordToPDF {
   
public static void main(String[] args){
        Document doc =
new Document("Input.docx");

        doc.saveToFile(
"Output.pdf", FileFormat.PDF);
   
}
}
Convert Word to PDF

Free Spire.Doc for Java also allows us to convert Word to Pdf with custom settings, such as whether to embed fonts, whether to disable link, as shown in below code.
public class ConvertWordToPDF {
    public static void main(String[] args){
        Document doc = new Document("Input.docx");

        ToPdfParameterList parameterList = new ToPdfParameterList();
        parameterList.isEmbeddedAllFonts(true);
        parameterList.setDisableLink(true);

        doc.saveToFile("Output.pdf", parameterList);
    }
}

Convert Word to Image

Free Spire.Doc for Java supports converting a specific page of a Word document to an image as well as converting the whole document to images. The following example shows how to convert the first page of a Word document to image.
public class ConvertWordToImage {
    public static void main(String[] args){
        Document doc = new Document("Input.docx");

        BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);

        File file= new File("Output.png");
        try {
            ImageIO.write(image, "PNG", file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Convert Word to Image
  
Convert Word to Html

The following example shows how to convert a Word document to Html file format.
 public class ConvertWordToHTML {
    public static void main(String[] args){
        Document doc = new Document("Input.docx");

        doc.saveToFile("Output.html", FileFormat.Html);
    }
}
Convert Word to Html
 
Convert Word to Svg

The following example illustrates how to convert a Word document to Svg file format.
public class ConvertWordToPDF {
    public static void main(String[] args){
        Document doc = new Document("Input.docx");        

        doc.saveToFile("Output.svg", FileFormat.Svg);
    }
}
Convert Word to Svg

Note: The free version is limited to 3 pages of conversion, if you want to convert more pages, you'll need to upgrade to the professional version of Spire.Doc for Java 

More information

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <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>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <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>