none
How do I print a document placing the word "Draft" as a watermark on each and every printed page?

    Question

  • I need to allow a user to print a draft copy of a VSTO Word document at anytime until the document is "Finalized" where it can be freely printed without the "draft" watermark.

    Somebody must have solved this problem by now.  I using Word 2003 and VSTO 2005 SE on Windows XP.

    Karl~
    Friday, August 14, 2009 8:21 PM

Answers

  • Thank you Jose.  Your input was very helpful.  Here is what I ended-up adding to my solution to solve this difficult problem.

    Enjoy!

       

        Public Overrides Sub AddWatermark()

            'Revised to print the watermark on the first page of ALL sections

            'Karl Fetterhoff  08/20/2009

     

            Me.ThawTemplate()

     

            Dim sectn As Interop.Word.Section

            Dim headr As Interop.Word.HeaderFooter

            Dim shaype As Interop.Word.Shape

            Dim countr As Integer = 0

            Dim shaypes As Interop.Word.Shapes

            Dim teLeft As Single = _doc.Application.InchesToPoints(2)

            Dim teTop As Single = _doc.Application.InchesToPoints(5)

            Dim teHeight As Single = _doc.Application.InchesToPoints(2)

            Dim teWidth As Single = _doc.Application.InchesToPoints(5)

     

            'If we don't force the cursor to the first page, it doesn't get a watermark

            _doc.Words.First.Characters.First.Select()

     

            For Each sectn In _doc.Sections

                For Each headr In sectn.Headers

                    If headr.IsHeader Then

                        headr.Range.Select()

                        shaypes = headr.Shapes

                        shaype = shaypes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, DRAFT", "Arial", 1, False, False, teLeft, teTop)

                        With shaype

                            countr += 1

                            .Name = "PowerPlusWaterMarkObject" & countr

                            .TextEffect.NormalizedHeight = False

                            .Line.Visible = Core.MsoTriState.msoTrue

                            .Line.Weight = 0.25

                            .Line.DashStyle = Core.MsoLineDashStyle.msoLineSolid

                            .Line.Style = Core.MsoLineStyle.msoLineSingle

                            .Fill.Visible = Core.MsoTriState.msoFalse

                            .Fill.Solid()

                            .Line.ForeColor.RGB = RGB(0, 0, 0)

                            .Line.BackColor.RGB = RGB(255, 255, 255)

                            .Fill.Transparency = 0.0#

                            .Rotation = 315

                            .Height = teHeight

                            .Width = teWidth

                            .WrapFormat.AllowOverlap = True

                            .WrapFormat.Side = Interop.Word.WdWrapSideType.wdWrapBoth

                            .WrapFormat.Type = Interop.Word.WdWrapType.wdWrapNone

                            .LockAspectRatio = True

                        End With

                    End If

                Next headr

            Next sectn

     

            With _doc.Application.ActiveWindow

                .ActivePane.View.SeekView = Interop.Word.WdSeekView.wdSeekMainDocument

                If .View.SplitSpecial = Interop.Word.WdSpecialPane.wdPaneNone Then

                    .ActivePane.View.Type = Interop.Word.WdViewType.wdPrintView

                Else

                    .View.Type = Interop.Word.WdViewType.wdPrintView

                End If

            End With

     

            Dim dlgType As Interop.Word.Dialog = _doc.Application.Dialogs(Interop.Word.WdWordDialog.wdDialogFilePrint)

            dlgType.Execute()

     

            For Each sectn In _doc.Sections

                For Each headr In sectn.Headers

                    For Each shaype In headr.Shapes

                        If InStr(shaype.Name, "PowerPlusWaterMarkObject") = 1 Then shaype.Delete()

                    Next

                Next

            Next

     

            Me.FreezeTemplate()

        End Sub

     

    Tuesday, August 18, 2009 6:24 PM

All replies

  • Hi Karl!

    The addition of watermarks during printing is a feature inherent only to the printer that you use.
    VSTO has no control over that.

    Since I don't know the full details of your project,
    the only way I can think of is to add the watermark as being a part of your document:



    Word.Application wdApplication = Globals.ThisAddIn.Application;
    object missing = Type.Missing;
    string watermarkText = "Draft";
    string fontName = "Arial";
    float fontSize = 14;
    float positionLeft = 100;
    float positionTop = 400;

    wdApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;

    Word.Shape watermark = wdApplication.Selection.HeaderFooter.Shapes.AddTextEffect(
        Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
        watermarkText,
        fontName,
        fontSize,
        Microsoft.Office.Core.MsoTriState.msoTrue,
        Microsoft.Office.Core.MsoTriState.msoFalse,
        positionLeft,
        positionTop,
        missing);

    watermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
    watermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
    watermark.Fill.ForeColor.RGB = Microsoft.VisualBasic.Information.RGB(192, 192, 192);
    watermark.Fill.Transparency = 0.5f;

    watermark.Rotation = 315;
    watermark.Height = Application.InchesToPoints(3.00f);
    watermark.Width = Application.InchesToPoints(5.00f);

    wdApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;

    Sunday, August 16, 2009 5:25 AM
  • Thank you Jose.  Your input was very helpful.  Here is what I ended-up adding to my solution to solve this difficult problem.

    Enjoy!

       

        Public Overrides Sub AddWatermark()

            'Revised to print the watermark on the first page of ALL sections

            'Karl Fetterhoff  08/20/2009

     

            Me.ThawTemplate()

     

            Dim sectn As Interop.Word.Section

            Dim headr As Interop.Word.HeaderFooter

            Dim shaype As Interop.Word.Shape

            Dim countr As Integer = 0

            Dim shaypes As Interop.Word.Shapes

            Dim teLeft As Single = _doc.Application.InchesToPoints(2)

            Dim teTop As Single = _doc.Application.InchesToPoints(5)

            Dim teHeight As Single = _doc.Application.InchesToPoints(2)

            Dim teWidth As Single = _doc.Application.InchesToPoints(5)

     

            'If we don't force the cursor to the first page, it doesn't get a watermark

            _doc.Words.First.Characters.First.Select()

     

            For Each sectn In _doc.Sections

                For Each headr In sectn.Headers

                    If headr.IsHeader Then

                        headr.Range.Select()

                        shaypes = headr.Shapes

                        shaype = shaypes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, DRAFT", "Arial", 1, False, False, teLeft, teTop)

                        With shaype

                            countr += 1

                            .Name = "PowerPlusWaterMarkObject" & countr

                            .TextEffect.NormalizedHeight = False

                            .Line.Visible = Core.MsoTriState.msoTrue

                            .Line.Weight = 0.25

                            .Line.DashStyle = Core.MsoLineDashStyle.msoLineSolid

                            .Line.Style = Core.MsoLineStyle.msoLineSingle

                            .Fill.Visible = Core.MsoTriState.msoFalse

                            .Fill.Solid()

                            .Line.ForeColor.RGB = RGB(0, 0, 0)

                            .Line.BackColor.RGB = RGB(255, 255, 255)

                            .Fill.Transparency = 0.0#

                            .Rotation = 315

                            .Height = teHeight

                            .Width = teWidth

                            .WrapFormat.AllowOverlap = True

                            .WrapFormat.Side = Interop.Word.WdWrapSideType.wdWrapBoth

                            .WrapFormat.Type = Interop.Word.WdWrapType.wdWrapNone

                            .LockAspectRatio = True

                        End With

                    End If

                Next headr

            Next sectn

     

            With _doc.Application.ActiveWindow

                .ActivePane.View.SeekView = Interop.Word.WdSeekView.wdSeekMainDocument

                If .View.SplitSpecial = Interop.Word.WdSpecialPane.wdPaneNone Then

                    .ActivePane.View.Type = Interop.Word.WdViewType.wdPrintView

                Else

                    .View.Type = Interop.Word.WdViewType.wdPrintView

                End If

            End With

     

            Dim dlgType As Interop.Word.Dialog = _doc.Application.Dialogs(Interop.Word.WdWordDialog.wdDialogFilePrint)

            dlgType.Execute()

     

            For Each sectn In _doc.Sections

                For Each headr In sectn.Headers

                    For Each shaype In headr.Shapes

                        If InStr(shaype.Name, "PowerPlusWaterMarkObject") = 1 Then shaype.Delete()

                    Next

                Next

            Next

     

            Me.FreezeTemplate()

        End Sub

     

    Tuesday, August 18, 2009 6:24 PM
  • Thanks for this solution, none of the other online examples worked for me and this one did. I made some minor changes, but the basic code worked.
    Tuesday, December 07, 2010 2:30 PM