MS Word allows users to insert a lot of elements in Word documents, including text, images, charts, and files. Images are frequently used in Word documents to make them intuitive, to express ideas that are hard to express in words, or just to make them beautiful. This article provides you a convenient and efficient way to insert images to Word documents under the help of Spire.Doc for Java.
- Insert an Image to a Word Document with Specified Text Wrapping Style
- Insert an Image to a Word Document at a Specific Location
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Insert an Image to a Word Document with Specified Text Wrapping Style
The detailed steps of inserting images with specified wrapping style are as follows:
- Create an object of Document class.
- Load a Word document from disk using Document.loadFromFile() method.
- Create an object of DocPicture class.
- Load an image from disk using DocPicture.loadImage() method.
- Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
- Set the text wrapping style of the image as Square using DocPicture.setTextWrappingStyle() method.
- Insert the image at the start of the second paragraph using Paragraph.getChildObjects().insert() method.
- Save the document using Document.saveToFile method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; public class insertImage { public static void main(String[] args) throws Exception { //Create an object of Document class Document doc = new Document(); //Load a Word document from disk doc.loadFromFile("D:/Samples/Sample.docx"); //Create an object of DocPicture class DocPicture picture = new DocPicture(doc); //Load an image from disk picture.loadImage("D:/Samples/System.png"); //Set the size of the image picture.setWidth(75); picture.setHeight(90); //Set the text wrapping style of the image as Square picture.setTextWrappingStyle( TextWrappingStyle.Square); //Insert the image at the start of the second paragraph doc.getSections().get(0).getParagraphs().get(1).getChildObjects().insert(0,picture); //Save the document doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx); } }
Insert an Image to a Word Document at a Specific Location
The detailed steps of inserting images at a specific location are as follows:
- Create an object of Document class.
- Load a Word document from disk using Document.loadFromFile() method.
- Create an object of DocPicture class.
- Load an image from disk using DocPicture.loadImage() method.
- Set the size of the image using DocPicture.setWidth() and DocPicture.setHeight() method.
- Set the text wrapping style of the image as Tight using DocPicture.setTextWrappingStyle() method.
- Insert the image into the third paragraph using Paragraph.getChildObjects().insert() method.
- Set the position of the image using DocPicture.setHorizontalPosition() and DocPicture.setVerticalPositon() method. The original location is at the start of the selected paragraph.
- Save the document using Document.saveToFile method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; public class insertImage { public static void main(String[] args) throws Exception { //Create an object of Document class Document doc = new Document(); //Load a Word document from disk doc.loadFromFile("D:/Samples/Sample.docx"); //Create an object of DocPicture class DocPicture picture = new DocPicture(doc); //Load an image from disk picture.loadImage("D:/Samples/PDF.png"); //Set the size of the image picture.setWidth(75); picture.setHeight(90); //Set the text wrapping style of the image as Tight picture.setTextWrappingStyle( TextWrappingStyle.Tight); //Insert the image into the Third paragraph doc.getSections().get(0).getParagraphs().get(2).getChildObjects().insert(0,picture); //Set the position of the image picture.setHorizontalPosition(370.0F); picture.setVerticalPosition(10.0F); //Save the document doc.saveToFile("D:/javaOutput/insertImage.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.