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

时间:2016-08-10 12:02:32

标签: vsto add-in office-addins word-2013 word-addins

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

 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;
            } 
        }

1 个答案:

答案 0 :(得分:0)

您将其添加到标题中,但在主要内容中查找形状。 Word不会返回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;
    } 
}