我正试图从文档中删除我以前在代码中创建的水印。下面是创建和应用水印的代码:
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不返回文档中的所有形状。图形对象。这不仅适用于header中的对象,也适用于文档内容中存在的嵌套形状。
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;
}
}