Microsoft Word enables you create simple Word processing documents like letters and reports. You can add color, clip art, write in a variety of fonts and sizes, add tables, borders & bullet formatting. However, in order to view the Word document(DOC,DOCX), we need specific applications such as MS Word, OpenOffice etc. Sometimes it gets pretty difficult to view these documents over portable devices (mobile phones etc). So one of the viable solution is to convert Word into Image format (JPG, PNG,GIF etc.). So in this article, we are going to explore the details on how to Convert Word to JPG using Java REST API.
Word to JPG Conversion REST API
In order to create, edit and transform Word documents to a variety of supported formats, we have developed a REST based solution named Aspose.Words Cloud. An in order to utilize the same Word document processing capabilities in Java application, we need to try using Aspose.Words Cloud SDK for Java. Now in order to use this SDK, we need to add its reference in our Java project by including the following information in pom.xml (maven build type project).
<repositories> <repository> <id>aspose-cloud</id> <name>artifact.aspose-cloud-releases</name> <url>http://artifact.aspose.cloud/repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words-cloud</artifactId> <version>22.12.0</version> </dependency> </dependencies>
Once the Java Cloud SDK reference has been added, please obtain your client credentials from Cloud Dashboard. Else, you need to first register a free account, using a valid email address.
Convert Word to JPG in Java
In this section, we are going to discuss the steps and their related details for converting Word to JPG using Java.
- Start with the creation of WordsApi object where we pass personalized credentials as arguments
- Secondly, load the input Word document using readAllBytes(…) method and pass the returned value to byte array
- Thirdly, create an object of ConvertDocumentRequest which takes input Word document’s name, JPG format and resultant file’s name as arguments
- Finally, call the method convertDocument(…) to perform Word to JPG conversion. The resultant image is then stored in cloud storage
The input WebP image used in the above example can be download from test_multi_pages.docx.
DOC to JPG using cURL Commands
Another approach of converting DOC to JPG is via cURL commands. So we need to first generate the JWT access token (based on client credentials) using following command and then perform the DOC to JPG or DOCX to JPG conversion.
curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"
Please note that the following commands expects the input Word document to be available in the cloud storage. After the conversion, we can save the resultant JPG image to local drive
curl -v -X GET "https://api.aspose.cloud/v4.0/words/test_multi_pages.docx?format=jpg" \ -H "accept: application/octet-stream" \ -H "Authorization: Bearer <JWT Token>" \ -o output.jpg
We may also consider saving the resultant file in cloud storage and for that purpose, we need to provide the value for outPath parameter.
curl -v -X GET "https://api.aspose.cloud/v4.0/words/test_multi_pages.docx?format=jpg&outPath=newOutput.jpg" \ -H "accept: application/octet-stream" \ -H "Authorization: Bearer <JWT Token>"
In this article, we have explored the feature of converting Word to JPG using Java as well as via cURL commands. You can also test the API through SwaggerUI within a web browser and at the same time, you may consider exploring the Product Documentation. It’s an amazing source of information regarding other exciting features offered by the API.
In case you want to download and modify the source code of Cloud SDK, it is available on GitHub (published under MIT license). Lastly, in the event of any issue or you need some further clarity/information regarding our APIs, please approach us for a quick resolution via free product support forum.
Please visit the following links to learn more about: