Create a Word document containing a picture that makes text flow around it in C#

See the example Create a Word document with some formatting in C# for basic instructions on using the Word application to create a Word document in C#. This example extends that one to add a picture that allows text to flow around it and that is aligned in the page's upper right corner.

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:
// 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 = Microsoft.Office.Core.MsoTriState.msoTrue;
shape.ScaleHeight(0.5f, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.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 representing that start. It then adds the picture to the Range's InlineShapes collection.

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

The code makes the Shape wrap text around itself and finally sets the Shape'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 figure 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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.