C # generate a word watermark
Last Update:2018-12-07
Source: Internet
Author: User
Code
1/** // <summary>
2 // watermark Function
3 /// </summary>
4 /// <param name = "docName"> file name </param>
5 // <param name = "FilePath"> file path </param>
6 /// <param name = "SavePath"> Save path </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,
16 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 ). 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; Center
27 WordDoc. Application. Selection. ShapeRange. Top =-999999f; // WdShapePosition. wdShapeCenter; Center
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); // under the text
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 );
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}