How to Use Free Spire.Doc to Wrap Text Around Image Easily

Introduction

It is easy for us to insert an image into a Word document, whereas it is very difficult to position it exactly where we want to be to make the page tidy and beautiful. That's because the image is inline with the text by default. 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 change the text wrapping setting in picture format in the Word document. It is quite difficult for developers to wrap text around an image. In the process we tried several approaches, including Microsoft.word.Interop and the Microsoft Open XML SDK. After in-depth research, they were abandoned for the following reasons:

  • Code was huge
  • Corresponding product installation was needed

Finally we chose the Free Spire.Doc library on .NET platform from Nuget.

Procedure to Implement

There are seven kinds of wrapping styles we can select in Word as in the following:

  • Inline 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.

In Spire.Doc there are also same the styles as Word, so we can wrap text around an image directly and easily. The following code snippet will show you how to set the text wrapping style to a Square in C#. The procedure is:

  1. Create a new project and reference the Spire.Doc.dll
  2. Initialize a Document object as in the following:
    Document document = new Document();
  3. Load an existing Word document with the LoadFromFile method as in the following:
    document.LoadFromFile("Sample.docx");
  4. Add a paragraph for the first section (the Paragraph class is in the Spire.Doc.Documents namespace) as in the following:
    Paragraph paragraph = document.Sections[0].AddParagraph();
  5. Add a picture to the paragraph as in the following:
    DocPicture picture = paragraph.AppendPicture(Image.FromFile("logo.jpg"));
  6. Set the text wrapping style to Square as in the following:
    picture.TextWrappingStyle = TextWrappingStyle.Square;
  7. Set the text wrapping type to both as in the following:
    picture.TextWrappingType = TextWrappingType.Both;
  8. Save the document to a file as in the following:
    string output = "output.docx";
    document.SaveToFile(output, FileFormat.Docx);

The following is a relevant screenshot of the text wrapping style at Square:


Figure 1: Screenshot of the text wrapping

The following is the full code of how to wrap text around an image:

  1. using Spire.Doc;  
  2. using Spire.Doc.Documents;  
  3. using System.Drawing;  
  4. using Spire.Doc.Fields;  
  5. namespace wrap_text_around_image  
  6. {  
  7.     class Program   
  8.     {  
  9.         static void Main(string[] args)   
  10.         {  
  11.             Document document = new Document();  
  12.             document.LoadFromFile("Sample.docx");  
  13.             Paragraph paragraph = document.Sections[0].AddParagraph();  
  14.             DocPicture picture = paragraph.AppendPicture(Image.FromFile("logo.jpg"));  
  15.             picture.TextWrappingStyle = TextWrappingStyle.Square;  
  16.             picture.TextWrappingType = TextWrappingType.Both;  
  17.             string output = "output.docx";  
  18.             document.SaveToFile(output, FileFormat.Docx);  
  19.         }  
  20.     }  
  21. }
Conclusion: Free Spire.Doc is a powerful and free C# Word API without Word automation and any other third-party add-ins. It is easy to use and totally independent.

Up Next
    Ebook Download
    View all
    Learn
    View all