2014-02-13 158 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; 
    }