Knowledgebase (2300)
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.
The background of a Word document is blank by default. For documents like brochures, invitations, handouts, and marketing materials, blank backgrounds will be too tedious. A good-looking background can be a great attraction to readers. Therefore, you can add a color or insert an image as a background to make your documents more appealing. This article shows how to set background color or image for Word documents by programming using Spire.Doc for Java.
- Add a Background Color to a Word Document
- Add a Gradient Background to a Word Document
- Insert a Background Image to a Word Document
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>
Add a Background Color to a Word Document
Adding a background color to a Word document is very easy. You only need to set the background type as color and then choose a color as your background. The detailed steps are as follows.
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Set the background type as color using Document.getBackground().setType() method.
- Set the background color using Document.getBackground().setColor() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//Create an object of Document class
Document document= new Document();
//Load a Word document
document.loadFromFile("C:/Sample.docx");
//Set the background type as color
document.getBackground().setType(BackgroundType.Color);
//Set the background color as orange
document.getBackground().setColor(Color.orange);
//Save the document
document.saveToFile("AddBackgroundColor.docx", FileFormat.Docx);
}
}

Add a Gradient Background to a Word Document
Adding gradient background requires more steps. You need to set the background type as gradient, choose two colors, and then set shading variant and style. The detailed steps are as follows.
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Set the background type as gradient using Document.getBackground().setType() method.
- Choose two colors using >Background.getGradient().setColor1() and Background.getGradient().setColor2() method.
- Set shading variant using Background.getGradient().setShadingVariant() method.
- Set shading style using Background.getGradient().setShadingStyle() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//load a word document
Document document= new Document("C:/Sample.docx");
//Set the background type as gradient
document.getBackground().setType(BackgroundType.Gradient);
//Choose two colors
Background background = document.getBackground();
background.getGradient().setColor1(Color.white);
background.getGradient().setColor2(Color.orange);
//Choose gradient variant
background.getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);
//Choose gradient style
background.getGradient().setShadingStyle(GradientShadingStyle.Horizontal);
//Save the document
document.saveToFile("AddGradientBackground.docx", FileFormat.Docx_2013);
}
}

Insert Background Image to a Word Document
To insert a background picture to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Change the background type to picture using Document.getBackground().setType() method.
- Insert a picture as the background using Document.getBackground().setPicture() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//Create an object of Document class
Document document= new Document();
//Load a Word document
document.loadFromFile("C:/Sample.docx");
//Set the background type as picture
document.getBackground().setType(BackgroundType.Picture);
//Set the background picture
document.getBackground().setPicture("C:/background.jpg");
//Save the document
document.saveToFile("AddBackgroundPicture.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.
Enabling security options of your Word documents is essential for keeping sensitive information safe. You can encrypt your document with a password so that it cannot be opened by unauthorized users; you can enable the Read-only mode to prevent users from changing the content; you can also partially restrict editing your document. This article demonstrates how to protect or unprotect Word documents in Java using Spire.Doc for Java.
- Protect a Word Document with a Password in Java
- Change Permission of a Word Document in Java
- Lock Specified Sections of a Word Document in Java
- Mark a Word Document as Final in Java
- Remove Password from an Encrypted Word Document in 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>
Protect a Word Document with a Password in Java
Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to password protect a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Encrypt the document with a password using Document.encrypt() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class PasswordProtectWord {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Load a Word file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
//Encrypt the document with a password
document.encrypt("open-psd");
//Save the document to another Word file
document.saveToFile("output/Encryption.docx", FileFormat.Docx);
}
}

Change Permission of a Word Document in Java
Documents encrypted with an open password cannot be opened by those who do not know the password. If you’d like to grant people permission to read your document but restrict the types of modifications that someone can make, you can set the document permission. The following are the steps to change permission of a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Set the document permission and the permission password using Document.protect() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.ProtectionType;
public class ChangePermission {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Load a Word document
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
//Set the document permission and set the permission password
document.protect(ProtectionType.Allow_Only_Form_Fields, "permission-psd");
//Save the document to another Word file
document.saveToFile("output/Permission.docx");
}
}

Lock Specified Sections of a Word Document in Java
You can lock parts of your Word document so that they cannot be changed and leave the unlocked parts available for editing. The following are the steps to protect specified sections of a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Set the editing restriction as Allow_Only_Form_Fields.
- Unprotect a specific section by passing false to Section.protectForm() as a parameter. The rest sections will continue to be protected.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.ProtectionType;
public class LockSpecificSections {
public static void main(String[] args) {
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
//Set editing restriction as "Allow_Only_Form_Fields"
doc.protect(ProtectionType.Allow_Only_Form_Fields, "permissionPsd");
//Unprotect section 2
doc.getSections().get(1).setProtectForm(false);
//Save the document to another Word file
doc.saveToFile("output/ProtectSection.docx");
}
}

Mark a Word Document as Final in Java
By marking a document as Final, you disable typing, editing, and format changes capabilities and a message will appear to any reader that the document has been finalized. The following are the steps to mark a Word document as final using Spire.Doc for Java.
- Create a Document object.
- Load a Word file using Document.loadFromFile() method.
- Get the CustomDocumentProperties object from the document.
- Add a custom property "_MarkAsFinal" to the document.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.CustomDocumentProperties;
import com.spire.doc.Document;
public class MarkAsFinal {
public static void main(String[] args) {
//Create a Document object
Document doc = new Document();
//Load a Word document
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
//Get custom document properties
CustomDocumentProperties customProperties = doc.getCustomDocumentProperties();
//Add "_MarkAsFinal" as a property to the document
customProperties.add("_MarkAsFinal", true);
//Save the document to another Word file
doc.saveToFile("output/MarkAsFinal.docx");
}
}

Remove Password from an Encrypted Word Document in Java
You can remove the password from an encrypted document if the encryption is no longer needed. The following are the detailed steps.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Remove the password using Document.removeEncryption() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class RemovePassword {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Load an encrypted Word document
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Encryption.docx", FileFormat.Docx, "open-psd");
//Remove encryption
document.removeEncryption();
//Save the document to another Word file
document.saveToFile("output/RemoveEncryption.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.