I have successfully write some code to add a watermark to word document pages. This follows a standard form which adds a Shape to the header on each section.
The problem arises when you select odd/even pages and a different first page in the header
In this case the code fails and all the watermarks end up on the same last page in the seciton or similarly go wrong.
I'm aware you can select a header using WdHeaderFooterIndex.wdHeaderFooterPrimary, WdHeaderFooterIndex.wdHeaderFooterEvenPages, WdHeaderFooterIndex.wdHeaderFooterFirstPage to select the section.Headers.Item() item but even if I use these it does not work. Below is my code:
`
private static void CutPageHeader(Section section, HeaderFooter hdr)
{
hdr.Range.Cut();
}
private static void PastePageHeader(Section section, HeaderFooter hdr)
{
Word.Range headerRange = hdr.Range;
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
headerRange.Paste();
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
headerRange.Delete(Word.WdUnits.wdCharacter, -1); //deletes paste-generated extra carriage return (Word ignores this when extra carriage return does not exist)
}
public static bool InsertWatermark(string watermarkText, int opacity, int orientation, int fontSize, int YOffset, int XOffset, bool firstPageOnly, bool centerOnPage)
{
if (string.IsNullOrEmpty(watermarkText)) return true;
if (wordApp == null) wordApp = (Application)TraySelectorOne.AddinModule.CurrentInstance.WordApp;
doc = wordApp.ActiveDocument;
int sectionCount = 1;
foreach (Section section in doc.Sections)
{
foreach (HeaderFooter hdr in section.Headers)
{
Word.Shape shw;
CutPageHeader(section, hdr);
shw = InsertWatermarkInHeader(hdr, section, watermarkText,
opacity, orientation, fontSize, YOffset, XOffset, centerOnPage, firstPageOnly ? "" : "");
PastePageHeader(section, hdr);
}
WdHeaderFooterIndex.wdHeaderFooterPrimary
}
return true;
}
private static Word.Shape InsertWatermarkInHeader(HeaderFooter header, Section section, string watermarkText, int opacity, int orientation, int fontSize, int YOffset, int XOffset, bool centerOnPage, string postfix)
{
// Ensure we don't place in the same header twice when section breaks on same page
for (int i = 1; i <= header.Shapes.Count; i++)
{
try
{
//if (header.Shapes.Item(i).Name.StartsWith("TS-WaterMark")) return null;
}
catch (Exception) { }
}
Word.Shape wm = null;
try
{
wm = header.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, watermarkText, "Times New Roman", fontSize,
MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0, header.Range);
wm.Visible = MsoTriState.msoCTrue;
Random r = new Random();
int random = r.Next(0, int.MaxValue);
wm.Name = "TS-WaterMark_"+ postfix + random.ToString();
wm.TextEffect.NormalizedHeight = MsoTriState.msoFalse;
wm.Line.Visible = MsoTriState.msoFalse;
wm.Fill.Visible = MsoTriState.msoTrue;
wm.LockAspectRatio = MsoTriState.msoTrue;
float f = (float)(100-opacity) / 100 * 255;
int intf = (int)f;
wm.Fill.ForeColor.RGB = (intf + (int)(0x100 * intf) + (int)(0x10000 * intf));
wm.Fill.Transparency = (float)(100-opacity)/100;
wm.Fill.Solid();
wm.Rotation = orientation;
wm.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
wm.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
// Seems to scale up the watermark to the whole page
//wm.Height = section.PageSetup.PageHeight;
//wm.Width = section.PageSetup.PageWidth;
wm.WrapFormat.AllowOverlap = -1;
wm.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
wm.WrapFormat.Type = WdWrapType.wdWrapNone;
if (centerOnPage)
{
wm.Left = (float)WdShapePosition.wdShapeCenter;
wm.Top = (float)WdShapePosition.wdShapeCenter;
}
else
{
wm.Left = XOffset;
wm.Top = YOffset;
}
wm.Visible = MsoTriState.msoCTrue;
}
catch (Exception ex)
{
Logging.WriteLog("Error adding watermark " + ex.Message);
}
return wm;
}
`
I have tried multiple solutions and they have not so far worked. I've also recorded macros to see how Word implments putting a Watermark on a particular page but it doesn't work properly either (the water mark is overwritten by text).
It seems a pretty simple thing
