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        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.