-
May 2nd, 2014, 09:54 AM
#1
Thread Starter
New Member
Programmatically add Watermark to Word document - VB.Net
Morning everyone,
Thought I would give back a bit of code that I have been working on all morning so others won't have to search and hack their way to the answer. The function below adds a centered watermark to all pages of a word document. Main class must import Microsoft.Office.Interop and (I think) System.Runtime.InteropServices.
With a little more work this could also be adapted to watermark photos but will leave that for someone else
Code:
Private Function AddWatermark(ByVal pdocument As Word.Document) As Word.Document
Dim nShape As Word.Shape
For Each section As Word.Section In pdocument.Sections
nShape = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "DRAFT", "Rockwell Extra Bold", 90, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0)
nShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
nShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
nShape.Fill.Solid()
nShape.Rotation = -45
nShape.Fill.ForeColor.RGB = Word.WdColor.wdColorGray20
nShape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
nShape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
nShape.Left = Word.WdShapePosition.wdShapeCenter
nShape.Top = Word.WdShapePosition.wdShapeCenter
Next
Return pdocument
End Function
-
Sep 22nd, 2014, 10:29 PM
#2
New Member
Re: Programmatically add Watermark to Word document - VB.Net
Thanks for the great information. I'm currently working on this and I find the following method pretty easy to add text watermark to an existing Word file, check
Add text watermark:
Dim txtWatermark As TextWatermark = New TextWatermark
txtWatermark.Text = "Microsoft"
txtWatermark.FontSize = 90
txtWatermark.Layout = WatermarkLayout.Diagonal
document.Watermark = txtWatermark
Add image watermark:
Dim picture As PictureWatermark = New PictureWatermark
picture.Picture = System.Drawing.Image.FromFile("..\imagess.jpeg")
picture.Scaling = 250
document.Watermark = picture
Before using this code snippet, you need to add a free .NET word dll to your project.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|