用到了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/archive/2009/04/13/1434687.html