In this brief tutorial, we will learn how to insert picture in Word using Java. You can save the file in DOC or DOCX format after adding the picture in it. The following steps can be used to add picture to Word document using Java in your applications.
Steps to Insert Picture in Word using Java
- Install Aspose.Words from the Maven repository
- Load an input Word document using the Document class object
- Instantiate DocumentBuilder class object
- Insert a picture in Word document header
- Add picture in Word document paragraph
- Save output Word document after inserting the pictures
These steps initialize the Document class to load an input Word document and then insert a picture in header and a paragraph. The output file is saved back in DOCX format however, you may select any other format as per the requirements.
Code to Add Picture to Word Document using Java
This Java code sample loads a Word document without needing MS Word application or any other Word processing application. Then it moves control to header and last paragraph to insert a picture respectively. Finally, you can save the file by simply mentioning the proper file extension or using the SaveFormat enumerator as the second argument in the save function.
In this tutorial, we have learned how to insert a picture in MS Word using Java. If you want to learn how to create a Word document from scratch, you may refer to the article how to create Word document using Java.