How to Convert Word to JPG in Java

This short tutorial will assist you on how to convert Word to JPG in Java. You can convert Word to JPG in Java such that each page of the DOCX Word document is saved as a separate JPG image. If you want to convert a selected range of pages to JPG images, this can also be achieved using this tutorial.

Steps to Convert Word to JPG in Java

  1. Add Aspose.Words library using Maven repository to convert Word to JPG
  2. Add references to Document, ImageSaveOptions, IPageSavingCallback, and other classes
  3. Load the sample Word document
  4. Instantiate the ImageSaveOptions object to set save format as JPG
  5. Set the range of pages to render in ImageSaveOptions
  6. Set the call back function while saving each page as an image
  7. Insert page number in each image file name within the call back function
  8. Save each page as JPG

During these steps, we load the input Word file and instantiate the ImageSaveOptions object to set the save format for saving Word to JPG using Java. The range of pages is also set that is to be rendered as JPG images. A call back function is declared which will be called before saving each page to set a different image file name by using the page index in it.

Code to Convert Word File to JPG in Java

import com.aspose.words.License;
import com.aspose.words.PageRange;
import com.aspose.words.PageSavingArgs;
import com.aspose.words.PageSet;
import com.aspose.words.SaveFormat;
import java.text.MessageFormat;
import com.aspose.words.Document;
import com.aspose.words.IPageSavingCallback;
import com.aspose.words.ImageSaveOptions;
public class HowToConvertWordToJPGInJava {
public static void main(String[] args) throws Exception { //main function for How To Convert Word To JPG In Java
// Initialize a license to avoid trial version watermark in the output JPG file
License license = new License();
license.setLicense("Aspose.Words.lic");
// Load the input document that is to be converted to JPG
Document doc = new Document("input.docx");
// Instantiate the ImageSaveOptions for saving Word file to JPG
ImageSaveOptions wordpagestoimage = new ImageSaveOptions(SaveFormat.JPEG);
// Set the range of pages for conversion to images
PageRange pagerange = new PageRange(0, doc.getPageCount() - 1);
wordpagestoimage.setPageSet(new PageSet(pagerange));
// Set callback function while saving each page
wordpagestoimage.setPageSavingCallback(new FileNamePageSavingCallback());
// Save document's pages to JPG
doc.save("output.jpg", wordpagestoimage);
}
private static class FileNamePageSavingCallback implements IPageSavingCallback {
@Override
public void pageSaving(PageSavingArgs args) throws Exception {
String outFileName = MessageFormat.format("InputDocx.Page_{0}.jpg", args.getPageIndex());
// Set a filename for the output image against each page
args.setPageFileName(outFileName);
}
}
}

This code converts each page of the document to a separate JPG image. However, if you want to convert only a specified range of pages, set it in the PageRange object where the first argument takes the initial page number and the second argument takes the total number of pages to be converted to JPG.

Here in this tutorial, we have learned to convert Word document to JPEG in Java. If you want to learn the process to convert a Word file to a PDF, refer to the article on how to convert Word to PDF in Java.

 English