DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

How to wrap text around image on Word document in Java

We usually add image to the Word document to make the document attractive and beautiful. The image is in line with the text by default after we add it with the word document. If we want to move the image freely or if we'd like to wrap the text around the image in a more natural way, we need to wrap text around image to set the position for the image and text. There are several kinds of wrapping styles we can select in word as below.

  • In Line with Text- Default

  • Square- To wrap text around the border of the image

  • Tight- To wrap text tightly around the image

  • Through- To fill in the negative space around the image

  • Top and Bottom- To place the image on its own line

  • Behind the Text- To display the text over the image

  • In Front of Text- To display the image over the text
  • 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 Free Spire.Doc for Java.

    Full codes of how to wrap text around image:

    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 WrapText {
        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 Square
            picture.setTextWrappingStyle(TextWrappingStyle.Square);
            picture.setTextWrappingType(TextWrappingType.Both);
            //Save the document to file
            doc.saveToFile("Output/WrapStyle.docx");
            doc.close();
    
        }
    }
    

    Effective screenshot of the text wrapping style at Square:
    Wrap Text

    More information:

    Website: https://www.e-iceblue.com/
    Support Forum: Free Spire.Doc for Java

    Discussion (0)