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
- Add Aspose.Words library using Maven repository to convert Word to JPG
- Add references to Document, ImageSaveOptions, IPageSavingCallback, and other classes
- Load the sample Word document
- Instantiate the ImageSaveOptions object to set save format as JPG
- Set the range of pages to render in ImageSaveOptions
- Set the call back function while saving each page as an image
- Insert page number in each image file name within the call back function
- 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
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.