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
import com.aspose.words.Document; | |
import com.aspose.words.DocumentBuilder; | |
import com.aspose.words.HeaderFooterType; | |
import com.aspose.words.License; | |
import com.aspose.words.Shape; | |
public class InsertPictureInWordDocumentUsingJava | |
{ | |
public static void main(String[] args) throws Exception { //main function for InsertPictureInWordDocumentUsingJava class | |
// Initialize a license to avoid trial version watermark in the output Word file after adding image | |
License license = new License(); | |
license.setLicense("Aspose.Words.lic"); | |
// Load input Word DOCX document | |
Document AddImagesToWordDOC = new Document("input.docx"); | |
// Initialize DocumentBuilder class object to add image | |
DocumentBuilder imageWriter = new DocumentBuilder(AddImagesToWordDOC); | |
// Move the cursor to the Primary Header | |
imageWriter.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); | |
// Insert a picture in Word document header | |
Shape headerImage = imageWriter.insertImage("SampleImage.jpg"); | |
// Set Image Size in Header section | |
headerImage.setWidth(1 * 72); // equals to one inch | |
headerImage.setHeight(1 * 72); | |
// Move cursor to last Paragraph in Document | |
imageWriter.moveTo(AddImagesToWordDOC.getLastSection().getBody().getLastParagraph()); | |
// Add the picture to Word Document and Link it with the file | |
Shape imageAsLinkToFile = imageWriter.insertImage("SampleImage.jpg"); | |
imageAsLinkToFile.getImageData().setSourceFullName("SampleImage.jpg"); | |
// Save output DOCX file after inserting image | |
AddImagesToWordDOC.save("Word with Embedded and Linked Images.docx"); | |
} | |
} |
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.