C # generate a word watermark

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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.