ASP.NET中对Word添加水印效果!!

用到了ImageWaterMark.dll 虽然不添加这个也能实现水印,但是本人到目前为止还没有解决自定义水印文字的方法,所以引用了ImageWaterMark.dll

 部分参考代码如下:

1.生成水印图片 

    public void CreateWaterMarkImg(string waterMarkStr, int picWidth, int picHeigh, int minwidth, int minheight)

    {



       ImageWaterMark.WaterMark wm = new ImageWaterMark.WaterMark();


        wm.SourceImagePath = new string[] { "~/ImgWaterMark/WaterMark.jpg" };

        #region 基本属性配置

        // WaterMark.WaterMarkType wmtType = WaterMark.WaterMarkType.Text;//水印类型

        wm.WaterMarkAlign =ImageWaterMark.WaterMark.WaterMarkAlignMode.Center;//水印在图象的位置

        wm.WaterMarkTransparence = 50;//设置水印不透明度,最大为100

        wm.WaterMarkAngle = 315;//水印旋转角度

        wm.WaterMarkPlaceX = 0;//水印位置X坐标

        wm.WaterMarkPlaceY = 0;//水印位置Y坐标

        #endregion

        #region 文字水印基本配置

        wm.WaterMarkText = waterMarkStr;//设置水印文字

        wm.TextFont = "宋体";//设置水印字体

        wm.TextColor = "#ffffff";//水印颜色

        wm.TextSize = 52;//水印文字大小

        wm.WaterMarkTextStyle =ImageWaterMark.WaterMark.TextStyle.Italic;//水印文字样式

        wm.Vertical = false;//水印是否垂直排列

        wm.TextShadow = false;//是否有阴影

        //wm.TextShadowColor = "#000";//阴影颜色

        //wm.TxTShadowTransparence = 0;//阴影不透明度

        //wm.TextShadowDepthX = 0;//阴影

        //wm.TextShadowDepthY = 0;//阴影

        #endregion

        #region 文字水印高级配置

        wm.WaterMarkTextEffect = ImageWaterMark.WaterMark.TextType.Ordinary;//水印类型

        wm.EmBossOffsetMode =ImageWaterMark.WaterMark.EmBossOffset.EmBossOffsetLeftTop;//设置浮雕偏移方向

        wm.SolidOffsetMode =ImageWaterMark.WaterMark.SolidOffset.SolidOffsetLeftTop;//设置立体侧面偏移方向

        wm.EmBossStyle =ImageWaterMark.WaterMark.TextEmBossStyle.Hollow;//设置浮雕样式

        wm.EmBossReinfor = false;

        wm.SolidFretwork = false;

        wm.SolidDepth = 5;

        wm.SolidDensity = 4;

        wm.TextBorderWidth = 0;

        wm.TextBorderColor = "#000";

        #endregion

        #region 图象编辑

        //wm.SetImgEffect = WaterMark.ImageEffect.None;//图象处理效果

        //wm.ImageRotate = WaterMark.RotateFlip.None;//设置原图像旋转或翻转

        //wm.CutX = 0;

        //wm.CutY = 0;

        //wm.CutWidth = 0;

        //wm.CutHeight = 0;

        //wm.MosGranule = 0;//马赛克

        //wm.MosX = 0;

        //wm.MosY = 0;

        //wm.MosWidth = 0;

        //wm.MosHeight = 0;

        #endregion

        #region 综合设置

        wm.DelSourceImg = false;//是否删除原图

        wm.ImageQuality = 90;//图象质量

        // wm.MiniatureImagePath = "~/images";//原图路径

        //wm.SaveWaterMarkImagePath = "~/images";//保存路径

        wm.WaterMarkTile = true;//水印是否平铺

        wm.WaterMarkTileMode =ImageWaterMark.WaterMark.TiledMode.AlignTiled;//平铺方式

        wm.VerticalSpace = 10;//平铺列距离

        wm.LineSpace = 120;//平铺行距离

        wm.ImageBorder = false;

        //wm.WaterMarkImagePath = "";//水印Logo路径

        wm.WaterMarkImageGroundColor = "#000";//过滤水印底色

        wm.ImgType =ImageWaterMark.WaterMark.ImageType.Bmp;//设置生成图像类型

        #endregion

        //生成水印图象

        wm.GetToWaterMarkImage(ImageWaterMark.Parameter.WaterMarkType.Text, picWidth, picHeigh, false, minwidth, minheight, false, false);

    } 

 2.给Word添加水印效果

public void show()

    {

        object Nothing = System.Reflection.Missing.Value;

        Word.ApplicationClass wordAppObj = null;

        Word.Document WordDoc = null;

        try

        {

        

            // object srcFileName = Server.MapPath("Documnets") + "/ProjectTmp.doc";

            // object dstFileName = Server.MapPath("Documnets") + "/Project.doc";

            string FileName = Server.MapPath("ImgWaterMark") + "/WaterMark_W_W.bmp";

            object srcFileName = "C:\\aa.doc";

            object dstFileName = "C:\\bb.doc";

            object obj = true;

             wordAppObj = new Word.ApplicationClass();

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

           

            this.CreateWaterMarkImg("与电子文本一致", WordDoc.ActiveWindow.Height, WordDoc.ActiveWindow.Width, WordDoc.ActiveWindow.Width, WordDoc.ActiveWindow.Height);

            this.CreateWaterMarkImgT("", WordDoc.ActiveWindow.Height, WordDoc.ActiveWindow.Width, WordDoc.ActiveWindow.Width, WordDoc.ActiveWindow.Height);

            Word.Shape oShape;

            WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;

            WordDoc.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;

         //   WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter(DateTime.Now.ToString("yyyyMMddhhssmm"));

            object top = 36;

            oShape = WordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(FileName, ref Nothing,

                ref Nothing, ref Nothing, ref top, ref Nothing, ref Nothing, ref Nothing);

            oShape.WrapFormat.Type = Word.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);

        }

    } 

原文地址:https://www.cnblogs.com/coderwar/p/1434687.html