private void AddWatermark(string WatermarkText)
{
Word.Selection Selection = ThisApplication.Selection;
Word.Shape wmShape;
//Select the section
this.Sections[1].Range.Select();
ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageHeader;
//Create the watermar shape
wmShape = Selection.HeaderFooter.Shapes.AddTextEffect(
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
WatermarkText, "Times New Roman", 1,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
0, 0, ref missing);
//Set all of the attributes of the watermark
wmShape.Select(ref missing);
wmShape.Name = "PowerPlusWaterMarkObject1";
wmShape.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;
wmShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
wmShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
wmShape.Fill.Solid();
wmShape.Fill.ForeColor.RGB = (int)Word.WdColor.wdColorGray25;
wmShape.Fill.Transparency = 0.5f;
wmShape.Rotation = 315.0f;
wmShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
wmShape.Height = ThisApplication.InchesToPoints(2.82f);
wmShape.Width = ThisApplication.InchesToPoints(5.64f);
wmShape.WrapFormat.AllowOverlap = -1; //true
wmShape.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth;
wmShape.WrapFormat.Type = Word.WdWrapType.wdWrapNone; //3
wmShape.RelativeHorizontalPosition =
Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
wmShape.RelativeVerticalPosition =
Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
wmShape.Left = (float)Word.WdShapePosition.wdShapeCenter;
wmShape.Top = (float)Word.WdShapePosition.wdShapeCenter;
//set focus back to document
ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekMainDocument;
}
private void DeleteWatermark()
{
Word.Selection Selection = ThisApplication.Selection;
//Select the section
this.Sections[1].Range.Select();
ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageHeader;
object shapeName = "PowerPlusWaterMarkObject1";
Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Delete();
//set focus back to document
ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekMainDocument;
}
private void AddWM_Click(object sender, EventArgs e)
{
try
{
AddWatermark("Hello World");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DeleteWm_Click(object sender, EventArgs e)
{
try
{
DeleteWatermark();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}