How to wrap text around image in C#

2015-04-29 08:41:34 Written by  support iceblue
Rate this item
(0 votes)

When we add image into word document, of course we want to move it exactly where we want to make our page tidy and beautiful. With the help of Spire.Doc, we can set the wrapping style to adjust the position of the image. Usually there are seven kinds of wrapping styles: In Line with Text, Square, Tight, Through, Top and Bottom, Behind the Text, In Front of Text and Spire.Doc supports all of them. This article will show you how to wrap text around image in C#. Here comes to the steps:

Step 1: Create a new word document and load the document from the file.

Document document = new Document();
document.LoadFromFile("Sample.docx");

Step 2: Add a paragraph for the first section.

Paragraph paragraph = document.Sections[0].AddParagraph();

Step 3: Add a picture in the paragraph.

DocPicture picture = paragraph.AppendPicture(Image.FromFile("image.jpg"));

Step 4: Set text wrapping style to Square.

picture.TextWrappingStyle = TextWrappingStyle.Square;

Step 5: Set text wrapping type to both.

picture.TextWrappingType = TextWrappingType.Both;

Step 6: Save the document to file and process it.

document.SaveToFile(output,FileFormat.Docx);
System.Diagnostics.Process.Start(output);

Effective screenshot of warp text around image:

How to wrap text around image in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace wrap_text_around_image
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("Sample.docx");
            Paragraph paragraph = document.Sections[0].AddParagraph();
             
            DocPicture picture = paragraph.AppendPicture(Image.FromFile("image.jpg"));
            picture.TextWrappingStyle = TextWrappingStyle.Square;
            picture.TextWrappingType = TextWrappingType.Both;
            string output = "output.docx";
            document.SaveToFile(output,FileFormat.Docx);
            System.Diagnostics.Process.Start(output);
    
        }

    }
}

Additional Info

  • tutorial_title: Wrap text around image
Last modified on Friday, 03 September 2021 03:47