C# 对Word文件添加水印图片

**////初始化文档对象
Word.Document WordDoc= wordAppObj.Documents.Open(ref _filename, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
///根据文档对象的高度与宽度生成文档背景的图片
string bkpic = CreateBackGroup(WordDoc.ActiveWindow.Height, WordDoc.ActiveWindow.Width, picFilePath);
Word.Shape oShape;
///创建背景水印图征
WordDoc.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
                WordDoc.ActiveWindow.View.SeekView 
= Word.WdSeekView.wdSeekPrimaryHeader;
                WordDoc.ActiveWindow.ActivePane.Selection.InsertAfter(
"");
object top = 30;
///将背景水印图片插入到Word
oShape = WordDoc.ActiveWindow.ActivePane.Selection.HeaderFooter.Shapes.AddPicture(bkpic, ref missing, ref missing, ref missing, ref top, ref missing, ref missing, ref missing);
oShape.WrapFormat.Type 
= Word.WdWrapType.wdWrapInline;
                oShape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText);

 

 

 

/// <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        }

 

 

 

VBS:

 

<head>
<title>打印企业标准文件</title>
<link href="../../common/css/sunhoo_20040509.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../../common/include/ess_common.js"></script>

<SCRIPT LANGUAGE="vbscript">
  Set myDocApp = CreateObject("Word.Application")
  myDocApp.Visible = True
  myDocApp.Activate
'  set myDoc = myDocApp.Documents.Open("C:/Documents and Settings/Administrator/桌面/sycs/sytest.doc")
  set myDoc = myDocApp.Documents.Open("C:/Documents and Settings/Administrator/桌面/sycs/sytest.doc")
  dim water
  water = "企业标准备案号:" + chr(13) + "测试标准号00011444" + chr(13) + "有效期至" + "2009年12月01日"
'  myDocApp.ActiveDocument.Bookmarks("StdRECCODE").Range.text="备案号:Q3301Y897756-2005"
    myDocApp.ActiveDocument.Shapes.AddTextbox(1, 63.85, 53.85, 265.85, 30).Select
'    myDocApp.ActiveDocument.Shapes.AddTextEffect(1, "ddddddd", "宋体", 1, False, False, 0, 0).Select
    myDocApp.Selection.ShapeRange.TextFrame.TextRange.Select
    myDocApp.Selection.Collapse
    myDocApp.Selection.TypeText("备案号:测试标准号00011444")

'    myDocApp.Selection.ShapeRange.Select
    myDocApp.Selection.ShapeRange.Fill.Visible = True
    myDocApp.Selection.ShapeRange.Fill.Solid
    myDocApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
    myDocApp.Selection.ShapeRange.Fill.Transparency = 1
'    myDocApp.Selection.ShapeRange.Line.Weight = 0.75
    myDocApp.Selection.ShapeRange.Line.DashStyle = 1
    myDocApp.Selection.ShapeRange.Line.Style = 1
    myDocApp.Selection.ShapeRange.Line.Transparency = 0
    myDocApp.Selection.ShapeRange.Line.Visible = False
'    myDocApp.Selection.ShapeRange.LockAspectRatio = False
'    myDocApp.Selection.ShapeRange.Height = 30.05
'    myDocApp.Selection.ShapeRange.Width = 165.85
'    myDocApp.Selection.ShapeRange.Left = 70.85
'    myDocApp.Selection.ShapeRange.Top = 40.5
'    myDocApp.Selection.ShapeRange.TextFrame.MarginLeft = 7.09
'    myDocApp.Selection.ShapeRange.TextFrame.MarginRight = 7.09
'    myDocApp.Selection.ShapeRange.TextFrame.MarginTop = 3.69
'    myDocApp.Selection.ShapeRange.TextFrame.MarginBottom = 3.69
'    myDocApp.Selection.ShapeRange.RelativeHorizontalPosition = 2
'    myDocApp.Selection.ShapeRange.RelativeVerticalPosition = 2
'    myDocApp.Selection.ShapeRange.Left = 0
'    myDocApp.Selection.ShapeRange.Top = 1
'    myDocApp.Selection.ShapeRange.LockAnchor = False
'    myDocApp.Selection.ShapeRange.LayoutInCell = True
'    myDocApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
'    myDocApp.Selection.ShapeRange.WrapFormat.Side = 0
'    myDocApp.Selection.ShapeRange.WrapFormat.DistanceTop = 0
'    myDocApp.Selection.ShapeRange.WrapFormat.DistanceBottom = 0
'    myDocApp.Selection.ShapeRange.WrapFormat.DistanceLeft = 1
'    myDocApp.Selection.ShapeRange.WrapFormat.DistanceRight = 1
'    myDocApp.Selection.ShapeRange.WrapFormat.Type = 3
'    myDocApp.Selection.ShapeRange.ZOrder 4
'    myDocApp.Selection.ShapeRange.TextFrame.AutoSize = False
'    myDocApp.Selection.ShapeRange.TextFrame.WordWrap = True

    myDocApp.ActiveDocument.Sections(1).Range.Select
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 9 'wdSeekCurrentPageHeader
    myDocApp.Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1, water, "宋体", 1, False, False, 0, 0).Select
    myDocApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1"
    myDocApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
    myDocApp.Selection.ShapeRange.TextEffect.FontBold = True
    myDocApp.Selection.ShapeRange.Line.Visible = False
    myDocApp.Selection.ShapeRange.Fill.Visible = True
    myDocApp.Selection.ShapeRange.Fill.Solid
    myDocApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 25, 100)
    myDocApp.Selection.ShapeRange.Fill.Transparency = 0 '0.5
    myDocApp.Selection.ShapeRange.Rotation = 355 '325
    myDocApp.Selection.ShapeRange.LockAspectRatio = True
    myDocApp.Selection.ShapeRange.Height = 50 'CentimetersToPoints(4.79)
    myDocApp.Selection.ShapeRange.Width = 300 'CentimetersToPoints(19.17)
    myDocApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
    myDocApp.Selection.ShapeRange.WrapFormat.Side = 3 'wdWrapNone
    myDocApp.Selection.ShapeRange.WrapFormat.Type = 3
    myDocApp.Selection.ShapeRange.RelativeHorizontalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.RelativeVerticalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.Left = 70  '-999995 'wdShapeCenter
    myDocApp.Selection.ShapeRange.Top = 190 '-999995 'wdShapeCenter
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 0 'wdSeekMainDocument

    myDocApp.ActiveDocument.Sections(2).Range.Select
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 9 'wdSeekCurrentPageHeader
    myDocApp.Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject2, water, "宋体", 1, False, False, 0, 0).Select
    myDocApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject2"
    myDocApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
    myDocApp.Selection.ShapeRange.TextEffect.FontBold = True
    myDocApp.Selection.ShapeRange.Line.Visible = False
    myDocApp.Selection.ShapeRange.Fill.Visible = True
    myDocApp.Selection.ShapeRange.Fill.Solid
    myDocApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 25, 100)
    myDocApp.Selection.ShapeRange.Fill.Transparency = 0 '0.5
    myDocApp.Selection.ShapeRange.Rotation = 355 '325
    myDocApp.Selection.ShapeRange.LockAspectRatio = True
    myDocApp.Selection.ShapeRange.Height = 50 'CentimetersToPoints(4.79)
    myDocApp.Selection.ShapeRange.Width = 300 'CentimetersToPoints(19.17)
    myDocApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
    myDocApp.Selection.ShapeRange.WrapFormat.Side = 3 'wdWrapNone
    myDocApp.Selection.ShapeRange.WrapFormat.Type = 3
    myDocApp.Selection.ShapeRange.RelativeHorizontalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.RelativeVerticalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.Left = 70  '-999995 'wdShapeCenter
    myDocApp.Selection.ShapeRange.Top = 190 '-999995 'wdShapeCenter
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 0 'wdSeekMainDocument

    myDocApp.ActiveDocument.Sections(3).Range.Select
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 9 'wdSeekCurrentPageHeader
    myDocApp.Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject3, water, "宋体", 1, False, False, 0, 0).Select
    myDocApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject3"
    myDocApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
    myDocApp.Selection.ShapeRange.TextEffect.FontBold = True
    myDocApp.Selection.ShapeRange.Line.Visible = False
    myDocApp.Selection.ShapeRange.Fill.Visible = True
    myDocApp.Selection.ShapeRange.Fill.Solid
    myDocApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 25, 100)
    myDocApp.Selection.ShapeRange.Fill.Transparency = 0 '0.5
    myDocApp.Selection.ShapeRange.Rotation = 355 '325
    myDocApp.Selection.ShapeRange.LockAspectRatio = True
    myDocApp.Selection.ShapeRange.Height = 50 'CentimetersToPoints(4.79)
    myDocApp.Selection.ShapeRange.Width = 300 'CentimetersToPoints(19.17)
    myDocApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
    myDocApp.Selection.ShapeRange.WrapFormat.Side = 3 'wdWrapNone
    myDocApp.Selection.ShapeRange.WrapFormat.Type = 3
    myDocApp.Selection.ShapeRange.RelativeHorizontalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.RelativeVerticalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.Left = 70  '-999995 'wdShapeCenter
    myDocApp.Selection.ShapeRange.Top = 190 '-999995 'wdShapeCenter
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 0 'wdSeekMainDocument

    myDocApp.ActiveDocument.Sections(4).Range.Select
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 9 'wdSeekCurrentPageHeader
    myDocApp.Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject4, water, "宋体", 1, False, False, 0, 0).Select
    myDocApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject4"
    myDocApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
    myDocApp.Selection.ShapeRange.TextEffect.FontBold = True
    myDocApp.Selection.ShapeRange.Line.Visible = False
    myDocApp.Selection.ShapeRange.Fill.Visible = True
    myDocApp.Selection.ShapeRange.Fill.Solid
    myDocApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 25, 100)
    myDocApp.Selection.ShapeRange.Fill.Transparency = 0 '0.5
    myDocApp.Selection.ShapeRange.Rotation = 355 '325
    myDocApp.Selection.ShapeRange.LockAspectRatio = True
    myDocApp.Selection.ShapeRange.Height = 50 'CentimetersToPoints(4.79)
    myDocApp.Selection.ShapeRange.Width = 300 'CentimetersToPoints(19.17)
    myDocApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
    myDocApp.Selection.ShapeRange.WrapFormat.Side = 3 'wdWrapNone
    myDocApp.Selection.ShapeRange.WrapFormat.Type = 3
    myDocApp.Selection.ShapeRange.RelativeHorizontalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.RelativeVerticalPosition = 0 'wdRelativeVerticalPositionMargin
    myDocApp.Selection.ShapeRange.Left = 70  '-999995 'wdShapeCenter
    myDocApp.Selection.ShapeRange.Top = 190 '-999995 'wdShapeCenter
    myDocApp.ActiveWindow.ActivePane.View.SeekView = 0 'wdSeekMainDocument

    'myDoc.Save()

  function cancel()
'    myDoc.Save()
On Error Resume Next
    myDocApp.Quit()
    self.close()
  end function
</script>

</head>

<html>
<base target="_self">
<body background="../../images/home_13.gif" leftmargin="5" topmargin="10">
<table width="50%" border="0" cellspacing="0" cellpadding="6" align="center">
  <tr>
    <td bgcolor="#F7F9FF">
      <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#9AB0ED">
        <tr>
          <td valign="top" background="../images/home_13.gif">
             <table width="100%" border="0" cellpadding="0" cellspacing="4">
                <tr nowrap >

          <td align="center">
            <br></br>
            <table align="center">
              <tr>
                <td valign="top" width="100%" >打印企业标准文件(已加水印)</td>
              </tr>
            </table>
            <p>
              <br></br>
            </p>
            <hr></hr>
            <table width="20%" align="center">
              <tr>
                <td width="50%" align="center">
                  <input value="保存并关闭文件" type="button" οnclick="cancel()"></input>
                </td>
                <td width="50%" align="center">
                  <input value="关 闭" type="button" οnclick="self.close();"></input>
                </td>
             </tr>
            </table>
          </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>
</body>

<!--<body οnlοad="setValue();" background="../../images/home_13.gif" leftmargin="5" topmargin="10">
<form name="form1" action="/queryunitinfo?action=queryunit" method="post">
    <input type="button" name="Submit" value=" 确  定 " οnclick="setValue();"/>
    <input type="button" name="Submit" value=" 取  消 " οnclick="cancel();"/>
</form>

</body>-->
</html>

 


           

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值