从 VSTO Word 2013 中的活动文档中删除水印

Removing a Watermark from the Active document in VSTO Word 2013

我正在尝试从文档中删除我之前在代码中创建的水印。这是创建和应用水印的代码:

 foreach (Word.Section section in document.Sections)
        {
            nShape = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, tag, "Calibri", 10, MsoTriState.msoTrue, MsoTriState.msoFalse, 0, 0);
            nShape.Name = "securityTagWaterMark";
            nShape.Line.Visible = MsoTriState.msoFalse;
            nShape.Fill.Solid();
            nShape.Fill.ForeColor.RGB = (Int32)Word.WdColor.wdColorGray20;
            nShape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
            nShape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
            // bottom right location
            nShape.Left = (float)Word.WdShapePosition.wdShapeRight;
            nShape.Top = (float)Word.WdShapePosition.wdShapeBottom;
            nShape.LockAspectRatio = MsoTriState.msoTrue;
        }

如何检查文档以找到任何形状对象或替换页面上已有的水印文本。这是我尝试过但不起作用的方法:

 Word.Document currentDoc = Globals.ThisAddIn.Application.ActiveDocument;

        Word.Shapes shapeCollection = Globals.ThisAddIn.Application.ActiveDocument.Shapes;

        foreach (Word.Shape shape in shapeCollection)
        {
            if (shape.Name == "securityTagWaterMark")
            {
                shape.TextEffect.Text = newText;
            } 
        }

您正在将其添加到页眉,但在主要内容中寻找形状。 Word 不会 return Document.Shapes 对象中的所有形状。对于页眉中的对象是这样,对于文档内容中确实存在的嵌套形状也是如此。

Word.Document currentDoc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Shapes shapeCollection = Globals.ThisAddIn.Application.ActiveDocument.Shapes;
foreach (Word.Shape shape in currentDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes)
{
    if (shape.Name == "securityTagWaterMark")
    {
        shape.TextEffect.Text = newText;
    } 
}