none
Adding water mark to docx, pptx, xlsx

    Question

  • Hi,

         I have office 2007 installed on my machine. I am trying to add water mark image to docx, pptx and xlsx file problematically using c#. I am using .net framework 2.0. Can somebody please help with this?

    Thursday, March 08, 2012 8:13 AM

Answers

  • Hi Jude,

    Here's some code that shows you how to work with Slide Masters.  Your watermark would be an image that you inserted into the master and then applied to the slides.

    Sub Working_With_Slide_Masters()
        Dim ppt As Presentation
        Set ppt = ActivePresentation
        Dim slide As slide
        
        'The following will add a new custom layout
        Dim ct As Integer
        ct = ppt.SlideMaster.CustomLayouts.Count
        ct = ct + 1
        ppt.SlideMaster.CustomLayouts.Add (ct)
        ppt.SlideMaster.CustomLayouts.Item(ct).Name = "MyCustomLayout"
        ppt.SlideMaster.CustomLayouts.Item(ct).FollowMasterBackground = msoFalse
        ppt.SlideMaster.CustomLayouts.Item(ct).Background.Fill.UserPicture "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
        'End of adding new custom layout
        
        'The following will find which slide has a specific custom layout
        For Each slide In ppt.Slides
            If ppt.Slides(slide.SlideNumber).CustomLayout.Name = "MyCustomLayout" Then
                Debug.Print slide.SlideNumber
            End If
        Next slide
        'End of Find Custom Layout
        
        'The following will apply a custom layout to a given slide number
        ppt.Slides(1).Select
        Dim lay As String
        lay = "MyCustomLayout"
        ppt.Slides(1).CustomLayout.MatchingName = lay
        'End of apply custom layout
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, March 12, 2012 1:55 PM

All replies

  • What specific problem are you having? The code below is a simple macro recording of how it's done in Word. You can use it as a starting point for refining your final solution, at least for Word. In PowerPoint you will have to use Master pages.

    Sub Macro17()
    '
    ' Macro17 Macro
    '
    '
        ActiveDocument.Sections(1).Range.Select
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.HeaderFooter.Shapes.AddPicture(fileName:= _
            "C:\Users\<Your Id>\Pictures\YourPicture.jpg" _
            , LinkToFile:=False, SaveWithDocument:=True).Select
        Selection.ShapeRange.Name = "WordPictureWatermark1914207491"
        Selection.ShapeRange.PictureFormat.Brightness = 0.85
        Selection.ShapeRange.PictureFormat.Contrast = 0.15
        Selection.ShapeRange.LockAspectRatio = True
        Selection.ShapeRange.Height = InchesToPoints(2.9)
        Selection.ShapeRange.Width = InchesToPoints(6.5)
        Selection.ShapeRange.WrapFormat.AllowOverlap = True
        Selection.ShapeRange.WrapFormat.Side = wdWrapNone
        Selection.ShapeRange.WrapFormat.Type = 3
        Selection.ShapeRange.RelativeHorizontalPosition = _
            wdRelativeVerticalPositionMargin
        Selection.ShapeRange.RelativeVerticalPosition = _
            wdRelativeVerticalPositionMargin
        Selection.ShapeRange.Left = wdShapeCenter
        Selection.ShapeRange.top = wdShapeCenter
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, March 08, 2012 11:58 AM
  • Hi Rich,

          I have successfully added watermarks to WORD and Excel file, however I am not able to do the same in Power point file. Can you please elaborate about Master page and how to add watermark in ppt.

    Friday, March 09, 2012 2:02 PM
  • Hi Jude,

    Here's some code that shows you how to work with Slide Masters.  Your watermark would be an image that you inserted into the master and then applied to the slides.

    Sub Working_With_Slide_Masters()
        Dim ppt As Presentation
        Set ppt = ActivePresentation
        Dim slide As slide
        
        'The following will add a new custom layout
        Dim ct As Integer
        ct = ppt.SlideMaster.CustomLayouts.Count
        ct = ct + 1
        ppt.SlideMaster.CustomLayouts.Add (ct)
        ppt.SlideMaster.CustomLayouts.Item(ct).Name = "MyCustomLayout"
        ppt.SlideMaster.CustomLayouts.Item(ct).FollowMasterBackground = msoFalse
        ppt.SlideMaster.CustomLayouts.Item(ct).Background.Fill.UserPicture "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
        'End of adding new custom layout
        
        'The following will find which slide has a specific custom layout
        For Each slide In ppt.Slides
            If ppt.Slides(slide.SlideNumber).CustomLayout.Name = "MyCustomLayout" Then
                Debug.Print slide.SlideNumber
            End If
        Next slide
        'End of Find Custom Layout
        
        'The following will apply a custom layout to a given slide number
        ppt.Slides(1).Select
        Dim lay As String
        lay = "MyCustomLayout"
        ppt.Slides(1).CustomLayout.MatchingName = lay
        'End of apply custom layout
    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, March 12, 2012 1:55 PM
  • Hi Rich

    I need to add watermark into active Word document 2010 before printing, do you have any code samples? Could you also advise on the best way to add watermark, by customising and saving into Normal template's Watermark gallery, to add to autotext entries list or to draw the watermark?

    Thanks in advance

    Friday, October 26, 2012 12:22 PM
  • Hi Amy,

    In my Word add-ins I have the following two routines.

        Sub SetWatermarks(ByRef wApp As Word.Application, ByRef doc As Word.Document)
    
            Dim scn As Word.Section, hdft As Word.HeaderFooter, shp As Word.Shape
            Dim missingWatermark As Boolean = False
            Dim markFound As Boolean = False
    
            RemoveWatermarks(wApp, doc)
    
            With doc
                For Each scn In .Sections
                    For Each hdft In scn.Headers
                        shp = hdft.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect2, My.Resources.cMsg_EvaluationOnly, "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 = wApp.InchesToPoints(1.96)
                            .Width = wApp.InchesToPoints(7.2)
                            With .WrapFormat
                                .AllowOverlap = True
                                .Side = Word.WdWrapSideType.wdWrapBoth
                                .Type = 3
                            End With
                            .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
                            .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
                            .Left = Word.WdShapePosition.wdShapeCenter
                            .Top = Word.WdShapePosition.wdShapeCenter
                        End With
                    Next hdft
                Next scn
            End With
        End Sub
    
        Sub RemoveWatermarks(ByRef wApp As Word.Application, ByRef doc As Word.Document)
            Dim scn As Word.Section, hdft As Word.HeaderFooter, shp As Word.Shape
            With doc
                For Each scn In .Sections
                    For Each hdft In scn.Headers
                        For Each shp In hdft.Shapes
                            If shp.TextEffect.Text = My.Resources.cMsg_EvaluationOnly Then
                                shp.Delete()
                            End If
                        Next
                    Next
                Next
            End With
        End Sub


    In the following application sub I call the SetWatermarks routine:

    Private Sub Application_DocumentBeforePrint(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles Application.DocumentBeforePrint

    When you look at the SetWatermarks code you will note that it calls the RemoveWatermarks routine. This is important so that you are not doubling up on the watermark images being placed into the section headers of the document.

    As for PowerPoint and Excel they are handled in a different manner and I don't readily have a sample to show you. Hopefully someone else will.

    Finally, as to your inquiry about adding custom watermarks to the Normal template gallery, I have not pursued doing that so I don't have any practical experience to share.

    Hope this helps

     


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, October 26, 2012 1:44 PM