Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Word C# Watermark

130 views
Skip to first unread message

tartancli

unread,
Apr 24, 2006, 3:08:02 PM4/24/06
to
Hi,

I'm trying to insert a watermark with a Word Add-in. I'm using a variant
of Paul Stubbs code that i found from a Google. The method I'm using is as
follows:

private void addWatermark(Word.Document doc, string
WatermarkText)
{

doc.Application.ActiveWindow.View.Type =
Word.WdViewType.wdPrintView;

doc.Sections[1].Range.Select();
doc.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageHeader;

Word.Selection Selection = doc.Application.Selection;
Word.Shape wmShape;

//Create the watermark shape
Object missing = Type.Missing;
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 =
doc.Application.InchesToPoints(2.82f);
wmShape.Width = doc.Application.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

doc.Application.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekMainDocument;

} // end addWatermark

The problem i have is that I get an exception at line:
wmShape.WrapFormat.AllowOverlap = -1;

this excpetion indicated that the method or property is not available
because the drawing operation cannot be applied to the current selection?????

Any help greatly appreciated!!! Thnx

Cindy M -WordMVP-

unread,
Apr 26, 2006, 3:44:28 AM4/26/06
to
Hi =?Utf-8?B?dGFydGFuY2xp?=,

> The problem i have is that I get an exception at line:
> wmShape.WrapFormat.AllowOverlap = -1;
>
> this excpetion indicated that the method or property is not available
> because the drawing operation cannot be applied to the current selection?????
>

Do you need to set this property? As I recall, it's only relevant for HTML
documents, and since headers/footes don't exist in HTML...

I see another potential problem in your code, BTW. Opening the Header/Footer
view and working with the selection in that view is NOT reliable if you're
dealing with documents with multiple headers/footers. That may not be a problem
in this particular case; I can't judge that, of course :-)

But, on the whole, it's much better to work with a RANGE, than the Selection
object. (Pseudo C# code - you need to check the enum!)
Word.Range rng = doc.Sections[1].Headers[wdHeaderFooterPrimary].Range;
wmShape = rng.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);

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)

tartancli

unread,
Apr 26, 2006, 10:42:01 AM4/26/06
to
Thanks again Cindy.

The rng object doesn't seem to have an AddTextEffect method but i used
doc.Shapes with range selected as follows.

Word.Range rng =
doc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
rng.Select();
Word.Shape wmShape;

//Create the watermark shape
Object missing = Type.Missing;

wmShape = doc.Shapes.AddTextEffect(
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
"Text","Times New Roman", 20,


Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
0, 0, ref missing);


That seemed to work fine. So using range will also mean that there won't be
any problems with documents having multiple headers/footers?

Many thanks

Cindy M -WordMVP-

unread,
Apr 27, 2006, 9:32:34 AM4/27/06
to
Hi =?Utf-8?B?dGFydGFuY2xp?=,

> So using range will also mean that there won't be
> any problems with documents having multiple headers/footers?
>

Yes and no... You'll want to do some testing.

Problems can crop up, due to the way Word manages Shapes in
the Header/Footer "Story". Creating them usually isn't a
problem. Doing anything with them at a later point can be
nerve-wracking. Even though they're displayed in the various
headers and footers, they're all managed in the HeaderFooter
story, common to all headers and footers. This can make it
"hairy" if you try to work with an index ( Shapes[n] ); much
better to assign any shapes you generate or plan to work with
a NAME so that you can be sure you're working with the correct
one. And to work with a specific range's ShapeRange[index]
rather than accessing Shapes[n].

But as I said, as long as you're just creating them, it
shouldn't be too big a hassle :-)

Sudumeedum

unread,
Nov 18, 2009, 4:23:02 AM11/18/09
to
HI,

Can any one let me know the way to applying watermark to Excel and
PowerPoint using c#

0 new messages