DEV Community

CodeSharing
CodeSharing

Posted on

Java/ Wrapping Text Around Image in Word Document

When inserting an image to a Word document, choosing a reasonable wrapping style can not only make the display of text and image more coordinated, but also make the page layout more beautiful. This article will introduce how to wrap text around image in Word document by using Free Spire.Doc for Java.

Installation
Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>3.9.0</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Java Code

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 {
        //Load the sample document
        Document doc = new Document();
        doc.loadFromFile("Herman.docx");

        //Add an image
        Section sec = doc.getSections().get(0);
        Paragraph para = sec.getParagraphs().get(0);
        DocPicture picture = para.appendPicture("C:\\Users\\Administrator\\Desktop\\Herman.jpg");

        //Set image width and height
        picture.setWidth(150f);
        picture.setHeight(125f);

        //Set text wrapping style to Square
        picture.setTextWrappingStyle(TextWrappingStyle.Square);
        picture.setTextWrappingType(TextWrappingType.Both);

        //Save the document to file
        doc.saveToFile("WrapStyle.docx");
        doc.close();

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Top comments (0)