zoukankan      html  css  js  c++  java
  • C# ASP.Net VS2010 word 2010生成水印

    网上的代码很多,但拿过来肯定不能直接运行。因为需要配置一下。

    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都可以的,效果就出来了,不过具体的图片的位置需要调。

  • 相关阅读:
    stenciljs 学习四 组件装饰器
    stenciljs 学习三 组件生命周期
    stenciljs 学习二 pwa 简单应用开发
    stenciljs ionic 团队开发的方便web 组件框架
    stenciljs 学习一 web 组件开发
    使用npm init快速创建web 应用
    adnanh webhook 框架 hook rule
    adnanh webhook 框架 hook 定义
    adnanh webhook 框架request values 说明
    adnanh webhook 框架execute-command 以及参数传递处理
  • 原文地址:https://www.cnblogs.com/myheaven/p/2263249.html
Copyright © 2011-2022 走看看