Add a picture to a Word document in C#


Add a picture to a Word document

This example shows how a C# program can add a picture to a Word document. It also shows how to use a bookmark to find the beginning of the Word document, make text flow around the picture, and align the picture on the page’s upper right.

See the example Create a Word document in C# for basic instructions on using the Word application to create a Word document in C#.

After creating the Word document and adding text to it as in the previous example, the program uses the following code to add and format the picture.

// Find the beginning of the document.
// For other pre-defined bookmarks, see:
//      http://support.microsoft.com/kb/212555
object start_of_doc = "\\startofdoc";

// Get a Range at the start of the document.
Word.Range start_range =
    word_doc.Bookmarks.get_Item(ref start_of_doc).Range;

// Add the picture to the Range's InlineShapes.
string picture_file = txtPicture.Text;
Word.InlineShape inline_shape =
    start_range.InlineShapes.AddPicture(
        picture_file, ref missing, ref missing, ref missing);

// Format the picture.
Word.Shape shape = inline_shape.ConvertToShape();

// Scale uniformly by 50%.
shape.LockAspectRatio = Core.MsoTriState.msoTrue;
shape.ScaleHeight(0.5f, Core.MsoTriState.msoTrue,
    Core.MsoScaleFrom.msoScaleFromTopLeft);

// Wrap text around the picture's square.
shape.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;

// Align the picture on the upper right.
shape.Left = (float)Word.WdShapePosition.wdShapeRight;
shape.Top = (float)Word.WdShapePosition.wdShapeTop;

This code first defines a string containing the bookmark name “\startofdocument.” This is a predefined bookmark that represents the start of the document. The code uses it to get a Range object representing that start. It then adds the picture to the Range object’s InlineShapes collection.

The code then gets a Shape object representing the InlineShape. It sets the object’s LockAspectRatio property to true so the picture will only resize uniformly. It then scales the picture to 50% of its original size.

The code makes the Shape wrap text around itself and finally sets the Shape object’s Left and Top values to make the picture appear in the upper right corner of the page.

Much of the work in this kind of Office automation is figuring out what objects in the Office object model do the things you want. For example, figuring out how to use Word’s InlineShape and Shape objects to create and format the picture. If you want to do a lot of this, my book Microsoft Office Programming: A Guide for Experienced Developers may help. The code is in Visual Basic and it’s a few years old but it should help you digure out how to manipulate the Word, Excel, PowerPoint, Access, and Outlook object models and those models haven’t changed too much since the book was written.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in interoperability, Office, Word and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Add a picture to a Word document in C#

  1. huyendieu says:

    Thank’s for greate article, but i want to generate many pages and insert break lines. please help me,thank’s a lot

  2. Chris says:

    I’ve spent at least an hour trying to figure out how to set left and right alignment on a Shape. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *