C# ASP.Net VS2010 word 2010生成水印
2011-11-25 14:50
453 查看
网上的代码很多,但拿过来肯定不能直接运行。因为需要配置一下。
1工程添加引用COM:Microsoft Word 14.0 Object Library
2修改WEB.CONFIG,在<system.web>加入:<identity impersonate="true"/>
3开始-》运行:dcomcnfg,会显示出“组件服务”管理器。打开“组件服务-》计算机-》我的电脑-》DCOM 配置”,找到“Microsoft Word 97 - 2003 文档”,单击右键,选择“属性”。在“属性”对话框中单击“安全”选项卡,在“启动和激活权限”处选择“自定义”,再单击右边的“编辑”,在弹出的对话框中添加“ASPNET”(在IIS6中是NETWORD SERVICE)用户,给予“本地启动”和“本地激活”的权限,单击“确定”,关闭“组件服务”管理器。
4 using Microsoft.Office.Interop.Word;
5编码:
protected void Page_Load(object sender, EventArgs e)
{
WaterMarkToWord("d:\\a.jpg", "d:\\d.docx", "d:\\c.docx");// d:\\d.docx可以换成"d:\\b.doc",
}
public void WaterMarkToWord(string imgName, object srcFileName, object dstFileName)
{
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordAppObj = null;
Microsoft.Office.Interop.Word.Document wordDoc = null;
try
{
object obj = true;
wordAppObj = new Microsoft.Office.Interop.Word.Application();
wordDoc = wordAppObj.Documents.Open(ref srcFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Shape oShape;
wordDoc.ActiveWindow.View.Type = WdViewType.wdOutlineView;
wordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
//WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter(DateTime.Now.ToString("yyyyMMddhhssmm"));
object left = wordDoc.ActiveWindow.Width / 4;
object top = wordDoc.ActiveWindow.Height / 2;
oShape = wordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(imgName, ref Nothing,
ref Nothing, ref left, ref top, ref Nothing, ref Nothing, ref Nothing);
oShape.WrapFormat.Type = WdWrapType.wdWrapInline;
oShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);
wordDoc.SaveAs(ref dstFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception ex)
{
}
finally
{
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordAppObj.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
注意蓝色的地方。
好了,可以用了,弄个a.jpg图片,弄个b.doc或b.docx都可以的,效果就出来了,不过具体的图片的位置需要调。
1工程添加引用COM:Microsoft Word 14.0 Object Library
2修改WEB.CONFIG,在<system.web>加入:<identity impersonate="true"/>
3开始-》运行:dcomcnfg,会显示出“组件服务”管理器。打开“组件服务-》计算机-》我的电脑-》DCOM 配置”,找到“Microsoft Word 97 - 2003 文档”,单击右键,选择“属性”。在“属性”对话框中单击“安全”选项卡,在“启动和激活权限”处选择“自定义”,再单击右边的“编辑”,在弹出的对话框中添加“ASPNET”(在IIS6中是NETWORD SERVICE)用户,给予“本地启动”和“本地激活”的权限,单击“确定”,关闭“组件服务”管理器。
4 using Microsoft.Office.Interop.Word;
5编码:
protected void Page_Load(object sender, EventArgs e)
{
WaterMarkToWord("d:\\a.jpg", "d:\\d.docx", "d:\\c.docx");// d:\\d.docx可以换成"d:\\b.doc",
}
public void WaterMarkToWord(string imgName, object srcFileName, object dstFileName)
{
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordAppObj = null;
Microsoft.Office.Interop.Word.Document wordDoc = null;
try
{
object obj = true;
wordAppObj = new Microsoft.Office.Interop.Word.Application();
wordDoc = wordAppObj.Documents.Open(ref srcFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Shape oShape;
wordDoc.ActiveWindow.View.Type = WdViewType.wdOutlineView;
wordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
//WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter(DateTime.Now.ToString("yyyyMMddhhssmm"));
object left = wordDoc.ActiveWindow.Width / 4;
object top = wordDoc.ActiveWindow.Height / 2;
oShape = wordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(imgName, ref Nothing,
ref Nothing, ref left, ref top, ref Nothing, ref Nothing, ref Nothing);
oShape.WrapFormat.Type = WdWrapType.wdWrapInline;
oShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);
wordDoc.SaveAs(ref dstFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception ex)
{
}
finally
{
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordAppObj.Quit(ref Nothing, ref Nothing, ref Nothing);
}
}
注意蓝色的地方。
好了,可以用了,弄个a.jpg图片,弄个b.doc或b.docx都可以的,效果就出来了,不过具体的图片的位置需要调。
相关文章推荐
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- Asp.net或C#使用word模板生成替换后的Word和pdf文档-总结
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- jetway_loves_programming之c#初学——如何利用asp.net生成word
- ASP.NET(C#)生成Word文档
- [网络收集]使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法(转)
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- 使用ASP.net(C#)批量上传图片并自动生成缩略图,文字水印图,图片水印图
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- ASP.net(c#)生成条形码
- ASP.NET(c#)生成条形码
- asp.net、C#动态添加图片水印
- Asp.net(C#)给图片加上水印效果
- ASP.NET生成WORD文档服务器部署注意事项
- ASP.NET(C#)将数据导出到Word或Excel