System Information

Impossible Is Nothing...

导航

asp.net+Word

I've seen many, many requests on the Microsoft newsgroups asking how you can insert a picture into a Word document without saving it to the file system first. This example application described in this blog illustrates both methods for inserting a picture firstly using the Word object model (InlineShapes.AddPicture) and secondly using Word's XML support (InsertXML).

The Word object model code is very straight forward:

private void buttonInsertNormal_Click(object  sender, System.EventArgs e)
{
  object oMissing = Type.Missing;
  object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
  _wordApp.ActiveDocument.Content.Collapse(ref oCollapseEnd);

  string filename = @"c:\temp\WordMLImage.bmp";

  try
  {
    pictureBox1.Image.Save(filename);
    _wordApp.ActiveWindow.Selection.Range.InlineShapes.AddPicture(filename,
      ref oMissing, ref oMissing, ref oMissing);
  }
  finally
  {
    File.Delete(filename);
  }
}

The only problem with this code is the requirement to write the image to the file system so Word can reload it. There are many circumstances where you would prefer to avoid touching the file system or maybe your application does not have write permission.

Fortunately there is an alternative approach using Word's XML support. This approach requires us to constructing a WordML document fragment representing the image to be inserted and then calling Word's InsertXML function to place the image in the document.

The code to do this is as follows:

private  void  buttonInsertWordML_Click(object  sender, System.EventArgs e)
{
  object oMissing = Type.Missing;
  object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
  _wordApp.ActiveDocument.Content.Collapse(ref oCollapseEnd);

  try
  {
    PictWriter pw = new PictWriter(pictureBox1.Image, "Example Image", "Example Image");
    _wordApp.ActiveWindow.Selection.Range.InsertXML(pw.ToString(), ref oMissing); 
  }
  catch (Exception ex)
  {
    // do something sensible here.
  }
}

You will notice the use of a custom class called Sentient.WordML.PictWriter which handles the task of converting the image into the WordML document fragment. The complete class source is included in the zip download at the top of the article however the core elements are the following two methods.

WriteDoc handles writing the WordML document wrapper. InsertXML expects a complete WordML document including full namespace references and style definitions if you are using styles (we are not). WritePict handles writing the actual image data into the XML stream as Base64.


///<summary>
/// Write the whole WordML document
///</summary>
///<param name="wtr">The XmlTextWriter to write to</param>
protected void WriteDoc(XmlTextWriter wtr)
{
  // start <xml> tag
  wtr.WriteStartDocument();

  // add processing instructions
  wtr.WriteProcessingInstruction("mso-application", "progid=\"Word.Document\"");

  // start <wordDocument> tag
  wtr.WriteStartElement("w", "wordDocument", WordMLNS);

  // write namespaces
  foreach(string prefix in _namespaces.AllKeys)
  {
    wtr.WriteAttributeString("xmlns", prefix, null, _namespaces[prefix]);
  }

  // start <body> tag
  wtr.WriteStartElement("body", WordMLNS);

  // call WritePict to add our image to the Xml stream.
  WritePict(wtr);

  // end <body> tag
  wtr.WriteEndElement();

  // end <wordDocument> tag
  wtr.WriteEndElement();

  // end <xml> tag
  wtr.WriteEndDocument();
}

///<summary>
/// Write the Pict WordML element
///</summary>
///<param name="wtr">The XmlTextWriter to write to</param>
protected void WritePict(XmlTextWriter wtr)
{
  if(_data==null)
  {
    return;
  }

  // start <pict> tag
  wtr.WriteStartElement("pict", WordMLNS);

  // start <binData> tag
  wtr.WriteStartElement("binData", WordMLNS);
  wtr.WriteAttributeString("name", WordMLNS, string.Format("wordml://{0}{1}", _name, _extension));

  // write the image as Base64
  wtr.WriteBase64(_data, 0, _data.Length);

  // end <binData> tag
  wtr.WriteEndElement();

  // start <shape> tag which describes the shape containing the image
  wtr.WriteStartElement("shape", VMLNS);
  wtr.WriteAttributeString("id", "_x0000_" + _name);
  wtr.WriteAttributeString("style", string.Format("width:{0}px;height:{1}px", _width, _height));

  // start <imagedata> tag which links to the <binData> above.
  wtr.WriteStartElement("imagedata", VMLNS);
  wtr.WriteAttributeString("src", string.Format("wordml://{0}{1}", _name, _extension));
  wtr.WriteAttributeString("title", OfficeNS, _title);

  // end <imagedata> tag
  wtr.WriteEndElement();

  // end <shape> tag
  wtr.WriteEndElement();

  // end <pict> tag
  wtr.WriteEndElement();
}

As you can see the InsertXML approach requires a little more code however does achieve our objective of not touching the file system.

The only drawback I've found with the InsertXML approach is you dont get the image autosizing functionality which means if you want the image to be reduced or enlarged in size you'll have to do this yourself in code either by resizing the image before inserting it or using the Word object model to manipulate the InlineShape properties after it has been inserted.

A major bonus is that using InsertXML is considerably faster than writing the image to the file system and then calling InlineShape.Add so you get a considerable performance improvement especially if you are working with large images.

http://www.developerfusion.co.uk/show/4677/

How to: Insert programmatically a bitmap to Microsoft Word documents

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

  1. Add to your .NET project a reference to System.Drawing.
  2. Add to your .NET project a reference to System.Windows.Forms.
  3. Add a reference to the previous namespaces in your code.

    [C#]

    using System.Drawing;
    using 
    System.Windows.Forms;

  4. 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);

  5. 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.

  6. Copy the image to the clipboard.

    Clipboard.SetImage(img);

  7. Paste the image to the range defined in step 4.

    rng.Paste();

http://www.dotnettreats.com/tipstricks/howtoword1.aspx

posted on 2005-11-09 15:07  SysInfo  阅读(691)  评论(1编辑  收藏  举报