How to: Insert programmatically a bitmap to Microsoft Word documents
Erika Ehrli Cabral
July 2005
Introduction
Many developers are writing code to generate reports in Microsoft Word to present data and calculations, and most of the time, developers have no time to research on how to add nice-to-have images inside reports that are generated automatically with a tool.
You can find conceptual and procedural documentation on how to work with Microsoft
Word and .NET; however, I have not found any article or how to topic that explains
how to insert bitmaps to Microsoft Word documents from your .NET project.
As a Word user, you can create beautiful reports that take advantage of styles and visual design principles. You can create documents with headers that have a logo or insert other images to a template or document that will make them visually attractive.
One of the biggest advantages of Word programmability is that you are able to do
almost everything that you do as Word application user.
If you are working with a .NET application, follow this easy steps to insert programmatically a bitmap to Microsoft Word documents.
Previous steps:
Create a .NET project that generates Word documents.
For more information about Microsoft Word and .NET, take a look at Understanding the Word Object Model from a .NET Developer's Perspective.
Procedures
To insert programmatically a bitmap to Microsoft Word documents
|
- Add to your .NET project a reference to System.Drawing.
- Add to your .NET project a reference to System.Windows.Forms.
- Add a reference to the previous namespaces in your code.
[C#]
using System.Drawing;
using System.Windows.Forms;
- Define the range where you need to insert the image.
object start = 0;
object end = 0;
Word.Range rng = ThisDocument.Range(ref start, ref end);
- Define the image.
Image img = new Image();
// img = your image goes here.
Note: You should assign the image you want to insert to the img object.
- Copy the image to the clipboard.
Clipboard.SetImage(img);
- Paste the image to the range defined in step 4.
rng.Paste();
|
See Also
Inserting images into Word documents using XML.