Java (481)
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>14.1.3</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<Paragraph>) sec.getParagraphs()) {
for (DocumentObject obj : (Iterable<DocumentObject>) 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 demonstrates how to add data labels to a chart and set the appearance (border style and fill style) for the data labels in PowerPoint using Spire.Presentation for Java. Note some chart types like Surface3D, Surface3DNoColor, Contour and ContourNoColor do not support data labels.
Below screenshot shows the original chart before adding data labels:

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.entity.ChartDataLabel;
import com.spire.presentation.charts.entity.ChartSeriesDataFormat;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class AddDataLabelsToChart {
public static void main(String[] args) throws Exception {
//Load the PowerPoint document
Presentation ppt = new Presentation();
ppt.loadFromFile("Chart.pptx");
//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Get the chart in the slide
IChart chart = (IChart)slide.getShapes().get(0);
//Loop through the series in the chart
for (ChartSeriesDataFormat series:(Iterable<ChartSeriesDataFormat>)chart.getSeries()) {
) {
//Add data labels for the data points in each series
for(int i = 0; i < 4; i++){
ChartDataLabel dataLabel = series.getDataLabels().add();
//Show label value
dataLabel.setLabelValueVisible(true);
//Show series name
dataLabel.setSeriesNameVisible(true);
//Set border line style
dataLabel.getLine().setFillType(FillFormatType.SOLID);
dataLabel.getLine().getSolidFillColor().setColor(Color.RED);
//Set fill style
dataLabel.getFill().setFillType(FillFormatType.SOLID);
dataLabel.getFill().getSolidColor().setColor(Color.YELLOW);
}
}
//Save the resultant document
ppt.saveToFile("DataLabels.pptx", FileFormat.PPTX_2013);
}
}
Output:
