How to Insert Image to Text Box in Word in C#, VB.NET

Spire.Doc supports to insert a picture to text box as a background image or as a part of body content. This article demonstrates how to achieve these purposes through following code snippets.

Part 1. Insert Background Image to Text Box

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertTextBox
{
    class Program
    {

        static void Main(string[] args)
        {

            //Create a Word document
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Append a Textbox to paragraph
            TextBox tb = paragraph.AppendTextBox(120, 200);

            //Set the position of Textbox
            tb.Format.HorizontalOrigin = HorizontalOrigin.Page;
            tb.Format.HorizontalPosition = 50;
            tb.Format.VerticalOrigin = VerticalOrigin.Page;
            tb.Format.VerticalPosition = 50;

            //Set the fill effect of Textbox as picture
            tb.Format.FillEfects.Type = BackgroundType.Picture;

            //Fill the Textbox with a picture
            tb.Format.FillEfects.Picture = Image.FromFile("Background.jpg");

            //Save to file
            doc.SaveToFile("InsertBackgroundImage.docx", FileFormat.Docx2013);

        }
    }
}

Output:

How to Insert Image to Text Box in Word in C#, VB.NET

Part 2. Insert Image to Body of Text Box

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertTextBox
{
    class Program
    {

        static void Main(string[] args)
        {

            //Create a Word document
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Append a Textbox to paragraph
            TextBox tb = paragraph.AppendTextBox(140, 250);

            //Set the position of Textbox
            tb.Format.HorizontalOrigin = HorizontalOrigin.Page;
            tb.Format.HorizontalPosition = 50;
            tb.Format.VerticalOrigin = VerticalOrigin.Page;
            tb.Format.VerticalPosition = 50;

            //Insert an image to body of Textbox
            Paragraph para1 = tb.Body.AddParagraph();
            Image image = Image.FromFile("Shakespeare.jpg");
            DocPicture picture = para1.AppendPicture(image);
            para1.Format.AfterSpacing = 8;
            para1.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //Insert text to body of Textbox
            Paragraph para2 = tb.Body.AddParagraph();
            TextRange textRange = para2.AppendText("(26 Apr.1564–§C23 Apr.1616) English poet, playwright, and actor, widely regarded as the greatest writer in the English language and the world's pre-eminent dramatist.");
            textRange.CharacterFormat.FontName = "Cambria";
            textRange.CharacterFormat.FontSize = 9;
            para2.Format.LineSpacing = 15;
            para2.Format.HorizontalAlignment = HorizontalAlignment.Left;
            para2.Format.SuppressAutoHyphens = true;

            //Save to file
            doc.SaveToFile("InsertToBody.docx", FileFormat.Docx2013);


        }
    }
}

Output:

How to Insert Image to Text Box in Word in C#, VB.NET