`
lexinquan
  • 浏览: 47188 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Dotnet水印之3:Word文档加水印图片

阅读更多
一两年前的代码了,三个版本都在实际中运用过。


///  WORD文件加水印
		/// </summary>
		/// <param name="srcDocName">源WORD文件名</param>
		/// <param name="SaveDocName">加水印后文件名</param>
		/// <param name="ModelPicName">水印的图片文件</param>
		/// <param name="top">离顶部距离</param>
		/// <param name="left">离左边距离</param>
		/// <returns>返回失败的错误信息1.返回空错误信息表示成功,2.返回非空,即为相应错误信息</returns>
		public static string WordWaterMark(string srcDocName,string SaveDocName,string ModelPicName,float top,float left)
		{
		
			object Nothing = System.Reflection.Missing.Value;
			object readOnly = false;
			object isVisible = true;	
			object filename =srcDocName;
			object docname = SaveDocName;

		
			//新建一个word应用
			Word.Application WordApp = new Word.ApplicationClass();
			//打开word对象
			Word.Document 	WordDoc=null;	
			try
			{
				WordDoc = WordApp.Documents.Open(ref filename,ref Nothing, ref readOnly,ref Nothing,
					ref Nothing, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
		    
				WordDoc.PageSetup.DifferentFirstPageHeaderFooter=-1;//设置首页不同

				WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
				WordDoc.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
				WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter("");
						
				WordDoc.Application.ActiveWindow.Selection.Range.Select(); 
				WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekFirstPageHeader;//第一页开始插入.wdSeekCurrentPageHeader;
				WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(ModelPicName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
				WordDoc.Application.Selection.ShapeRange.LockAspectRatio = Office.MsoTriState.msoTrue;
				WordDoc.Application.Selection.ShapeRange.Height = 40f;
				WordDoc.Application.Selection.ShapeRange.Width = 180f;
				//WordDoc.Application.Selection.ShapeRange.Left = Convert.ToSingle(WdShapePosition.wdShapeRight);//居中
				//WordDoc.Application.Selection.ShapeRange.Top = Convert.ToSingle(WdShapePosition.wdShapeTop);//居中
				WordDoc.Application.Selection.ShapeRange.Left = left;
				WordDoc.Application.Selection.ShapeRange.Top = top;
				WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
				WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
				WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone;                   // 
				WordDoc.Application.Selection.ShapeRange.ZOrder(Office.MsoZOrderCmd.msoSendBehindText);//文本底下
				WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;//相对于页面
				WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
				//WordDoc.Application.Selection.ClearFormatting();
				WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
		
				//文件保存
				WordDoc.SaveAs(ref docname, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
					ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
				ErrorString="";
				
			}
			catch(Exception ex)
			{
				ErrorString=ex.Message.Trim();
			}
			finally
			{
				if (WordDoc!= null)
				{
					WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
				}
				WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);  
				GC.Collect();
			}
			//}
			return ErrorString;          
		}
		 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics