c#產生word浮水印
來源:互聯網
上載者:User
Code
1 /**//// <summary>
2 /// 加浮水印函數
3 /// </summary>
4 /// <param name="docName">檔案名稱 </param>
5 /// <param name="FilePath">檔案路徑</param>
6 /// <param name="SavePath">儲存路徑</param>
7 public void CreateWordFile(string docName,string FilePath,string SavePath)
8 {
9 System.Windows.Forms.Application.DoEvents();
10 object Nothing = System.Reflection.Missing.Value;
11 object filename = FilePath + "\\" + docName;
12 object docname = SavePath + "\\" + docName;
13 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
14 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing,
15 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
16 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
17 try
18 {
19 WordDoc.Application.ActiveWindow.Selection.Range.Select();
20 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
21 WordDoc.Application.Selection.HeaderFooter.Shapes.AddPicture(textBox3.Text.Trim(), ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing).Select(ref Nothing);
22 WordDoc.Application.Selection.ShapeRange.Name = "WordPictureWatermark1";
23 WordDoc.Application.Selection.ShapeRange.LockAspectRatio = MsoTriState.msoTrue;
24 WordDoc.Application.Selection.ShapeRange.Height = 845f;
25 WordDoc.Application.Selection.ShapeRange.Width = 595f;
26 WordDoc.Application.Selection.ShapeRange.Left = -999998f; //WdShapePosition.wdShapeCenter;置中
27 WordDoc.Application.Selection.ShapeRange.Top = -999999f;// WdShapePosition.wdShapeCenter;置中
28 WordDoc.Application.Selection.ShapeRange.WrapFormat.AllowOverlap = 0;
29 WordDoc.Application.Selection.ShapeRange.LayoutInCell =0;
30 WordDoc.Application.Selection.ShapeRange.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
31 WordDoc.Application.Selection.ShapeRange.WrapFormat.Type = WdWrapType.wdWrapNone; //
32 WordDoc.Application.Selection.ShapeRange.ZOrder(MsoZOrderCmd.msoSendBehindText);//文本底下
33 WordDoc.Application.Selection.ShapeRange.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
34 WordDoc.Application.Selection.ShapeRange.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
35 WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
36 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);
37 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
38 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
39 if (checkBox1.Checked)
40 {
41 DeleteWord(filename.ToString());
42 }
43 }
44 catch (Exception ee)
45 {
46 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
47 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
48 MessageBox.Show(ee.ToString());
49 }
50 finally
51 {
52 // WordDoc1.Close(ref Nothing, ref Nothing, ref Nothing);
53 //WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
54 //WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
55 }
56
57 }