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
ASP.NET生成WORD文档服务器部署注意事项 配置详情请下载附件图解 1、Asp.net 2.0在配置Microsoft Excel、Microsoft Word应用程序权限时 error: 80070005 和8000401a 的解决总 2007-11-01 11:30 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框。 单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后 自定义->编辑->添加ASP.NET账户和IUSER_计算机名 * 这些帐户仅在计算机上安装有 IIS 的情况下才存在。 在标识选项卡选择下列用户,输入用户名密码 13. 确保允许每个用户访问,然后单击确定。 14. 单击确定关闭 DCOMCNFG。 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 8000401a 。 运行dcomcnfg打开组件服务, 依次展开"组件服务"->"计算机"->"我的电脑"->"DCOM配置" 找到"Microsoft Excel应用程序"或"Microsoft Word应用程序", 右键打开属性对话框,点击"标识"选项卡, 点"下列用户",把管理员的用户密码正确填写进去... 点击"安全"选项卡, 依次把"启动和激活权限","访问权限","配置权限",都选择为自定义, 然后依次点击它们的编辑,把everyone添加进去,并加入所有的权限... OK,解决此问题! 2、请设置web.config中的帐号和密码,否则会提示检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 例如
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值