给Office文档添加水印效果【测试有效】

  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             string test1 = "C:\\test.docx";//源文件  
  4.             string test2 = "D:\\test.docx";//添加后的文件  
  5.             AddWaterMark("test", test1, test2);//添加水印  
  6.         }  
  7.   
  8.         public void AddWaterMark(string docName, string FilePath, string SavePath)  
  9.         {  
  10.             System.Windows.Forms.Application.DoEvents();  
  11.             object Nothing = System.Reflection.Missing.Value;  
  12.             object filename = FilePath;  
  13.             object docname = SavePath;  
  14.             //图片存放位置  
  15.             String logoPath = "D:\\1.jpg";  
  16.             Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();  
  17.             Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing,  
  18.             ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  19.             ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);  
  20.             try  
  21.             {  
  22.                 WordDoc.Application.ActiveWindow.Selection.Range.Select();  
  23.                 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;  
  24.                 //增加水印图片  
  25.                 WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);  
  26.                 //增加水印文字  
  27.                 WordDoc.Application.Selection.HeaderFooter.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Dog", "Arial", (float)100, MsoTriState.msoTrue, MsoTriState.msoFalse, 0, 0, ref Nothing).Select(ref Nothing);  
  28.                 WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";  
  29.                 WordDoc.Application.Selection.ShapeRange.LockAspectRatio = MsoTriState.msoTrue;  
  30.                 WordDoc.Application.Selection.ShapeRange.Height = 845f;  
  31.                 WordDoc.Application.Selection.ShapeRange.Width = 595f;  
  32.                 WordDoc.Application.Selection.ShapeRange.Left = -999998f; //WdShapePosition.wdShapeCenter;居中  
  33.                 WordDoc.Application.Selection.ShapeRange.Top = -999999f;// WdShapePosition.wdShapeCenter;居中  
  34.                 WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;  
  35.                 WordDoc.Application.Selection.ShapeRange.LayoutInCell = 0;  
  36.                 WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;  
  37.                 WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //    
  38.                 WordDoc.Application.Selection.ShapeRange.ZOrder(MsoZOrderCmd.msoSendBehindText);//文本底下  
  39.                 WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;  
  40.                 WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;  
  41.                 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;  
  42.                 WordDoc.SaveAs(ref docname, 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);  
  43.                 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);  
  44.                 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);  
  45.             }  
  46.             catch (Exception ee)  
  47.             {  
  48.             }  
  49.             finally  
  50.             {  
  51.             }  
  52.         }  
  53.     }  

猜你喜欢

转载自blog.csdn.net/bruce135lee/article/details/81485528
今日推荐