Image and Shape (7)
Stamps can guarantee the authenticity and validity of a document and also make the document look more professional. Since Microsoft Word doesn't provide a built-in stamp feature, you can add an image to your Word documents to mimic the stamp effect. This is useful when the document will be printed to paper or PDF. In this article, you will learn how to add a "stamp" to a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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>13.11.2</version>
</dependency>
</dependencies>
Add an Image Stamp to Word Document
Spire.Doc for Java allow developers to use the core classes and method listed in the below table to add and format an image to make it look like a stamp in the Word document.
| Name | Description |
| DocPicture Class | Represents a picture in a Word document. |
| Paragraph.appendPicture() Method | Appends an image to end of paragraph. |
| DocPicture.setHorizontalPosition() Method | Sets absolute horizontal position of the picture. |
| DocPicture.setVerticalPosition() Method | Sets absolute vertical position of the picture. |
| DocPicture.setWidth() Method | Sets picture width. |
| DocPicture.setHeight Method | Sets picture height. |
| DocPicture.setTextWrappingStyle() Method | Sets text wrapping type of the picture. |
The detailed steps are as follows:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Get the specific paragraph using ParagraphCollection.get() method.
- Add an image to the Word document using Paragraph.appendPicture() method.
- Set position, size and wrapping style of the image using the methods offered by DocPicture class.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
public class AddStamp {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("test.docx");
//Get the specific paragraph
Section section = doc.getSections().get(0);
Paragraph paragraph = section.getParagraphs().get(4);
//Add an image
DocPicture picture = paragraph.appendPicture("cert.png");
//Set the position of the image
picture.setHorizontalPosition(240f);
picture.setVerticalPosition(120f);
//Set width and height of the image
picture.setWidth(150);
picture.setHeight(150);
//Set wrapping style of the image to In_Front_Of_Text, so that it looks like a stamp
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
//Save the document to file
doc.saveToFile("AddStamp.docx", FileFormat.Docx);
doc.dispose();
}
}

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.
Word documents often contain images, which are useful elements to enhance the aesthetics of the document. If the overall design of the document changes, the existing images might no longer match the new style. Replacing them with updated images can help maintain a consistent look. In this article, you will learn how to replace images in Word in Java using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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>13.11.2</version>
</dependency>
</dependencies>
Replace Images with New Images in Word in Java
To replace an image in a Word document with another image, you need to loop through the elements of the document, find the images and add them to a list, then get the image that you want to replace from the list and call the DocPicture.loadImage() method to replace it with the loaded image. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Create an ArrayList instance.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Get a specific image from the list and replace it with another image using DocPicture.loadImage() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.ArrayList;
public class ReplaceImages {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("E:\\PythonDoc\\WordImage.docx");
//Create an ArrayList instance
ArrayList<DocumentObject> pictures = new ArrayList();
//Iterate through all sections in the document
for (Section sec : (Iterable<Section>) doc.getSections()
) {
//Iterate through all paragraphs in each section
for (Paragraph para : (Iterable<Paragraph>) sec.getParagraphs()
) {
//Iterate through all child objects in each paragraph
for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects()
) {
//Find the images and add them to the list
if (obj.getDocumentObjectType() == DocumentObjectType.Picture) {
pictures.add(obj);
}
}
}
}
//Replace the first picture in the list with a new image
DocPicture picture = (DocPicture)pictures.get(0) ;
picture.loadImage("pic.png");
//Save the result document
doc.saveToFile("ReplaceWithNewImage.docx", FileFormat.Docx);
}
}

Replace Image with Text in Word in Java
Spire.Doc for Java doesn't provide a direct method to replace image with text, but you can achieve this task by inserting the text at the image location and then removing the image from the document.
The following steps demonstrate how to replace all images in a Word document with text:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Create an ArrayList instance.
- Iterate through all sections in the document.
- Iterate through all paragraphs in each section.
- Iterate through all child objects in each paragraph.
- Find the images and add them to the list.
- Iterate through the images in the list.
- Get the index of the image in the paragraph using Paragraph.getChildObjects().indexOf() method.
- Create a TextRange instance and set text for the text range through TextRange.setText() method.
- Insert the text range at the image location using Paragraph.getChildObjects().insert() method.
- Remove the image from the paragraph using Paragraph.getChildObjects().remove() method.
- Save the result document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.ArrayList;
public class replaceImageWithText {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("WordImage.docx");
//Create an ArrayList instance
ArrayList<DocumentObject> pictures = new ArrayList();
int j = 1;
//Iterate through all sections in the document
for (Section sec : (Iterable<Section>) doc.getSections()
) {
//Iterate through all paragraphs in each section
for (Paragraph para : (Iterable<Paragraph>) sec.getParagraphs()
) {
//Iterate through all child objects in each paragraph
for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects()
) {
//Find the images and add them to the list
if (obj.getDocumentObjectType() == DocumentObjectType.Picture) {
pictures.add(obj);
}
}
//Iterate through all images in the list and replace them with text "Here was image {image index}"
for (DocumentObject pic : pictures)
{
int index = para.getChildObjects().indexOf(pic);
TextRange range = new TextRange(doc);
range.setText(String.format("Here was image-%d", j));
para.getChildObjects().insert(index, range);
para.getChildObjects().remove(pic);
j++;
}
}
}
//Save the result document
doc.saveToFile("ReplaceImageWithText.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.
This article demonstrates how to rotate shapes on a Word document using Spire.Doc for Java.
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
public class RotateShape {
public static void main(String[] args) throws Exception {
//Load the Sample Word document.
Document doc = new Document();
doc.loadFromFile("InsertShapes.docx");
//Get the first section
Section sec = doc.getSections().get(0);
//Traverse every paragraphs to get the shapes and rotate them
for ( Paragraph para: (Iterable) sec.getParagraphs()) {
for (DocumentObject obj : (Iterable) para.getChildObjects()) {
if (obj instanceof ShapeObject) {
((ShapeObject) obj).setRotation(20);
}
}
}
//Save to file
doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);
}
}
Effective screenshot after rotating the shapes on word:

This article will show you how to set the wrapping style to adjust the position of the image in Java applications with the help of Spire.Doc for Java.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.TextWrappingType;
import com.spire.doc.fields.DocPicture;
public class ImageWrappingStyle {
public static void main(String[] args) throws Exception {
Document doc = new Document();
doc.loadFromFile("Sample.docx");
Section sec = doc.getSections().get(0);
Paragraph para = sec.getParagraphs().get(0);
DocPicture picture = para.appendPicture("logo.png");
//Set image width and height
picture.setWidth(150f);
picture.setHeight(125f);
//Set text wrapping style to Behind
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
picture.setTextWrappingType(TextWrappingType.Both);
//Save the document to file
doc.saveToFile("Output/WrapStyle.docx");
doc.close();
}
}
Effective screenshot after setting the wrapping style for image:

In MS Word, WordArt is used to insert text with special effects such as shadows, outlines, colors, gradients, and 3D effects. It can be helpful when creating stylish headlines or highlighting specific content. In this article, you will learn how to programmatically insert WordArt in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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>13.11.2</version>
</dependency>
</dependencies>
Insert WordArt in Word
The ShapeType enumeration provided by Spire.Doc for Java defines a variety of WordArt shape types whose names begin with "Text". In order to create a WordArt in Word, you need to initialize an instance of ShapeObject and specify the WordArt type and text content. The detailed steps are as follows:
- Create a Document instance.
- Add a section to the document using Document.addSection() method, and then add a paragraph to the section using Section.addParagraph() method.
- Append a shape to the paragraph and specify the shape size and type using Paragraph.appendShape(float width, float height, ShapeType shapeType) method.
- Set the position of the shape using ShapeObject.setVerticalPosition() and ShapeObject.setHorizontalPosition() methods.
- Set the text of WordArt using ShapeObject.getWordArt().setText() method.
- Set the fill color and stroke color of WordArt using ShapeObject.setFillColor() and ShapeObject.setStrokeColor() methods.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class WordArt{
public static void main(String[] args) throws Exception {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Add a paragraph.
Paragraph paragraph = section.addParagraph();
//Add a shape to the paragraph and specify the shape size and type
ShapeObject shape = paragraph.appendShape(400, 150, ShapeType.Text_Deflate_Bottom);
//Set the position of the shape
shape.setVerticalPosition(60);
shape.setHorizontalPosition(60);
//set the text of WordArt
shape.getWordArt().setText("Create WordArt in Word");
// Set the fill color and stroke color of WordArt
shape.setFillColor(Color.CYAN);
shape.setStrokeColor(Color.BLUE);
//save the document to file.
doc.saveToFile("WordArt.docx", FileFormat.Docx_2013);
}
}

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.
This article demonstrates how to add various kinds of shapes and how to group shapes in a Word document using Spire.Doc for Java.
Insert Shapes
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class InsertShapes {
public static void main(String[] args) throws Exception {
//create a Word document.
Document doc = new Document();
//add a section and a paragraph
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//insert a rectangle
ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
rectangle.setFillColor(Color.darkGray);
rectangle.setStrokeColor(Color.darkGray);
rectangle.setVerticalPosition(50);
//insert a triangle
ShapeObject triangle = para.appendShape((float)(160/Math.sqrt(3)),80, ShapeType.Triangle);
triangle.setStrokeColor(Color.red);
triangle.setFillColor(Color.orange);
triangle.setVerticalPosition(50);
triangle.setHorizontalPosition(200);
//insert a ellipse
ShapeObject circle = para.appendShape(80,80, ShapeType.Ellipse);
circle.setFillColor(Color.RED);
circle.setStrokeWeight(15);
circle.setVerticalPosition(50);
circle.setHorizontalPosition((float)(270 + 160/Math.sqrt(3)));
//save to file
doc.saveToFile("output/InsertShapes.docx", FileFormat.Docx);
}
}

Insert Shape Group
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeGroup;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class InsertShapeGroup {
public static void main(String[] args) {
//create a Word document
Document doc = new Document();
//add a section and a paragraph
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//get page width
float pageWidth = sec.getPageSetup().getClientWidth();
//add a shape group, specifying width, height and horizontal position
ShapeGroup shapegroup = para.appendShapeGroup(200, 150);
shapegroup.setHorizontalPosition((pageWidth - 200) / 2);
//calculate the scale ratio
float X = (shapegroup.getWidth() / 1000.0f);
float Y = (shapegroup.getHeight() / 1000.0f);
//create a circle
ShapeObject circle_1 = new ShapeObject(doc, ShapeType.Ellipse);
circle_1.setWidth(80 / X);
circle_1.setHeight(80 / Y);
circle_1.setFillColor(new Color(176, 196, 222));
circle_1.setStrokeColor(new Color(176, 196, 222));
circle_1.setHorizontalPosition(60 / X);//set its horizontal position relative to shape group
//add the circle to shape group
shapegroup.getChildObjects().add(circle_1);
//add two more circles to shape group
ShapeObject circle_2 = new ShapeObject(doc, ShapeType.Ellipse);
circle_2.setWidth(80 / X);
circle_2.setHeight(80 / Y);
circle_2.setFillColor(new Color(0, 128, 128));
circle_2.setStrokeColor(new Color(0, 128, 128));
circle_2.setHorizontalPosition(30 / X);
circle_2.setVerticalPosition(50 / Y);
shapegroup.getChildObjects().add(circle_2);
ShapeObject circle_3 = new ShapeObject(doc, ShapeType.Ellipse);
circle_3.setWidth(80 / X);
circle_3.setHeight(80 / Y);
circle_3.setFillColor(new Color(72, 61, 139));
circle_3.setStrokeColor(new Color(72, 61, 139));
circle_3.setHorizontalPosition(90 / X);
circle_3.setVerticalPosition(50 / Y);
shapegroup.getChildObjects().add(circle_3);
//save the document
doc.saveToFile("output/InsertShapeGroup.docx", FileFormat.Docx_2010);
}
}

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>13.11.2</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.