DEV Community

CodeSharing
CodeSharing

Posted on

1

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay