Question

I need to add a watermark to a document when I click on a button in my own Ribbon. Is there a way to do it with VSTO or OpenXML?

The examples I found are all for VSTO 2005 and don't produce the desired result. They just put a shape on the document in the background. It always shows up on just the last page. So additional pages are not watermarked.

Is there a way to get a Watermark to show up as if you created it with the build in functionality of OpenXML or VSTO 2010? One that creates one across an entire page, on each page that is created and will be created.

Was it helpful?

Solution

This may prove helpful: (Taken from here) Although it is for 2010 it may prove useful to you.

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

EDIT And just encase you wanted to replace existing watermarks here is another bit of useful code, to find watermarks.

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

OTHER TIPS

Yes Watermark is just a shape being inserted in to document. When you use VSTO you need to seek in to Header and then add the shape.

If you have different first page header, odd and even page header you need to do it for each type of headers in every section.

so here is the psuedo code. I had my image in building blocks with correct height, width and position so my code just inserts it and always displayed in the middle. If you are inserting your shape through code you need to take care of it.

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top