Adding text watermark using ms interop doesn't work if there is a table or an image on header

Jeongmin Lee 1 Reputation point
2022-12-12T12:41:09.407+00:00

here is my code to add text watermark to the center of a document.

foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)  
            {  
                Microsoft.Office.Interop.Word.Shape wordShape = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect  
                (Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "I am a watermark", "Arial", (float)30, Microsoft.Office.Core.MsoTriState.msoTrue,  
                Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, o);  
                  
                wordShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;  
                wordShape.Fill.Solid();  
                wordShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray15;  
                wordShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;  
                wordShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;  
                wordShape.Rotation = -45;  
                wordShape.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;  
                wordShape.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;  
                wordShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;  
                wordShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;  
            }  

The problem is that the watermark is placed oddly if there is a table or image added on header and not added on the first page of the document.It works well with documents without any header.p7Y6z.png

And I tried the following code to add the watermark on all pages.

WdHeaderFooterIndex hfIndex = WdHeaderFooterIndex.wdHeaderFooterPrimary;  
            HeaderFooter headerFooter;  
  
            foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)  
            {  
                Microsoft.Office.Interop.Word.Shape wordShapeFirstPage = section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Shapes.AddTextEffect  
                (Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "I am watermark", "Calibri", (float)30, Microsoft.Office.Core.MsoTriState.msoTrue,  
                Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, o);  
  
                Microsoft.Office.Interop.Word.Shape wordShapePrimary = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect  
                (Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "I am watermark", "Calibri", (float)30, Microsoft.Office.Core.MsoTriState.msoTrue,  
                Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, o);  
  
                List<Microsoft.Office.Interop.Word.Shape> wordShapeList = new List<Microsoft.Office.Interop.Word.Shape>();  
                 
                wordShapeList.Add(wordShapePrimary);  
                wordShapeList.Add(wordShapeFirstPage);  
  
                foreach (Shape wordShape in wordShapeList)  
                {  
                    wordShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;  
                    wordShape.Fill.Solid();  
                    wordShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray15;  
                    wordShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;  
                    wordShape.WrapFormat.AllowOverlap = -1;  
                    wordShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;  
                    wordShape.Rotation = -45;  
                    wordShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;  
                    wordShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;  
                    wordShape.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;  
                    wordShape.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;  
                    
                }  
            }  

Then the watermark is added on the first page, too, but still is misplaced on other pages. How can I fix it?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
0 comments No comments
{count} votes