2014-02-13 103 views
2

当我点击我自己的功能区中的按钮时,需要为文档添加水印。有没有办法用VSTO或OpenXML做到这一点?为Word 2013添加水印

我发现的例子都是针对VSTO 2005的,并且不会产生所需的结果。他们只是在背景中的文件形状。它总是显示在最后一页上。所以额外的页面不加水印。

有没有办法让Watermark显示出来,就好像你用OpenXML或VSTO 2010的功能构建它一样?一个在整个页面上创建一个,在创建和创建的每个页面上。

回答

2

这可能证明是有帮助的:(Taken from here)虽然它适用于2010年,但它可能对您有用。

Sub SetWatermarks() 
    Dim scn As Word.Section, hdft As Word.HeaderFooter, shp As Word.Shape 
    With Word.ActiveDocument 
     For Each scn In .Sections 
     For Each hdft In scn.Headers 
      Set shp = hdft.Shapes.AddTextEffect(msoTextEffect2, "Evaluation Only", "Tahoma", 10, False, False, 0, 0) 
      With shp 
      .line.Visible = False 
      With .TextEffect 
       .NormalizedHeight = False 
       .FontItalic = False 
       .FontBold = True 
      End With 
      With .Fill 
       .Visible = True 
       .Solid 
       .ForeColor.RGB = 12632256 
       .Transparency = 0.5 
      End With 
      .Rotation = 315 
      .LockAspectRatio = True 
      .Height = Word.InchesToPoints(1.96) 
      .Width = Word.InchesToPoints(7.2) 
      With .WrapFormat 
       .AllowOverlap = True 
       .Side = Word.wdWrapNone 
       .Type = 3 
      End With 
      .RelativeHorizontalPosition = Word.wdRelativeHorizontalPositionMargin 
      .RelativeVerticalPosition = Word.wdRelativeVerticalPositionMargin 
      .Left = wdShapeCenter 
      .top = wdShapeCenter 
      End With 
     Next hdft 
     Next scn 
    End With 

编辑而刚刚包住你想在这里取代现有的水印是有用的代码另一位,找到水印。

Sub FindWaterMark() 

    Dim doc As Word.Document 
    Dim scn As Word.Section 
    Dim shp As Word.Shape 
    Dim hdft As Word.HeaderFooter 

    Set doc = Word.ActiveDocument 

    With doc 
     For Each scn In .Sections 
     For Each hdft In scn.Headers 
      For Each shp In hdft.Range.ShapeRange 
       If InStr(1, shp.Name, "WordArt") <> 0 Or InStr(1, shp.Name, "Power") <> 0 Then 
        If shp.TextEffect.Text = "Evaluation Only" Then 
         Debug.Print shp.Name 
        End If 
       End If 
      Next shp 
     Next hdft 
     Next scn 
    End With 

End Sub 
1

是水印仅仅是被插入到文档的形状。当您使用VSTO时,您需要寻找标题然后添加形状。

如果您有不同的首页页眉,奇数页和偶数页的页眉,您需要为每个部分中的每种页眉类型执行此操作。

所以这里是伪代码。我在正确的高度,宽度和位置的构建块中有我的图像,所以我的代码只是插入它,并始终显示在中间。如果你通过代码插入你的形状,你需要照顾它。

foreach (Section sec in document.Sections) 
{ 
    foreach (HeaderFooter headerFooter in sec.GetHeadersFooters()) 
    { 
     document.ActiveWindow.View.set_SeekView(headerFooter.IsHeader 
       ? WdSeekView.wdSeekCurrentPageHeader:WdSeekView.wdSeekCurrentPageFooter); 
       **//Insert the shape** 
     InsertFromBuildingBlocks(headerFooter.Range); 
    } 
    document.ActiveWindow.View.set_SeekView(WdSeekView.wdSeekMainDocument); 
} 

    //This is extension method used above 
    public static IEnumerable<HeaderFooter> GetHeadersFooters(this Section section) 
    { 
     List<HeaderFooter> headerFooterlist = new List<HeaderFooter> 
      { 
       section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary], 
       section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage], 
       section.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages], 
       section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary], 
       section.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage], 
       section.Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages] 
      }; 

     return headerFooterlist; 
    }