Adding a DRAFT watermark

I would like to add a DRAFT watermark to my documents using your Aspose.Word tool. Currently I am doing this using word automation and would like to stop using that.

This is how I am currently doing this. How would I do this using your tool?:

Dim oDoc As Word.Document
Dim oWordApp As Word.Application
Dim oSelection As Word.Selection
Dim bRet As Boolean = True
oWordApp = New Word.Application
oDoc = oWordApp.Documents.Open(szFile)
oDoc.Sections(1).Range.Select()
oDoc.ActiveWindow.View.Type = Word.WdViewType.wdPrintView
oSelection = oWordApp.Selection
oWordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
oSelection.HeaderFooter.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "DRAFT", "Times New Roman", 1, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0).Select()
oSelection.ShapeRange.Name = "DRAFT\_WaterMark"
oSelection.ShapeRange.TextEffect.NormalizedHeight = False
oSelection.ShapeRange.Line.Visible = False
oSelection.ShapeRange.Fill.Visible = True
oSelection.ShapeRange.Fill.Solid()
oSelection.ShapeRange.Fill.ForeColor.RGB = Word.WdColor.wdColorGray25
oSelection.ShapeRange.Fill.Transparency = 0.5
oSelection.ShapeRange.Rotation = 315
oSelection.ShapeRange.LockAspectRatio = True
oSelection.ShapeRange.Height = oWordApp.InchesToPoints(2.82)
oSelection.ShapeRange.Width = oWordApp.InchesToPoints(5.64)
oSelection.ShapeRange.WrapFormat.AllowOverlap = True
oSelection.ShapeRange.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth
oSelection.ShapeRange.WrapFormat.Type = 3
oSelection.ShapeRange.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
oSelection.ShapeRange.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
oSelection.ShapeRange.Left = Word.WdShapePosition.wdShapeCenter
oSelection.ShapeRange.Top = Word.WdShapePosition.wdShapeCenter
oWordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
oDoc.SaveAs(szFile)
oDoc.Close()

Please try my code given in

https://forum.aspose.com/t/119770

and let me know if you have any problems or further questions on this issue.

I will then try to help you further.

Best regards,

I tried this and I received an error on the following line of code:

builder.InsertNode(shape);

It looks like InsertNode takes a node object as a parameter, not a shape object.

The code I have given in the link above works correctly in my test. Please let me know what exactly the error is.

I just tried it again and it seemed to work. I’m not sure what I did incorrectly the first time.

The following is the error message that I am receiving:

Message "Object reference not set to an instance of an object." String
StackTrace " at Aspose.Words.DocumentBuilder.InsertNode(Node node)
at Aspose.Words.DocumentBuilder.InsertImage(Byte[] imageBytes, RelativeHorizontalPosition horzPos, Double left, RelativeVerticalPosition vertPos, Double top, Double width, Double height, WrapType wrapType)
at Aspose.Words.DocumentBuilder.InsertImage(Stream stream, RelativeHorizontalPosition horzPos, Double left, RelativeVerticalPosition vertPos, Double top, Double width, Double height, WrapType wrapType)
at Aspose.Words.DocumentBuilder.InsertImage(String fileName, RelativeHorizontalPosition horzPos, Double left, RelativeVerticalPosition vertPos, Double top, Double width, Double height, WrapType wrapType)
at Aspose.Words.DocumentBuilder.InsertImage(String fileName, Double width, Double height)
at Aspose.Words.DocumentBuilder.InsertImage(String fileName)
at Test.Net.Form1.Button7\_Click(Object sender, EventArgs e) in C:\Projects\Test.Net\Form1.vb:line 674" String

The first time the following code runs, it works fine. I get this error after I run the following code on an existing document a second time. It happens on the call to InsertImage. It is the same error I get when I use InsertNode.

oDialog.Filter = "Word Document (\*.doc)|\*.doc"
oDialog.ShowDialog()
szFile = oDialog.FileName
'Open the template document
oDoc = New Aspose.Words.Document(szFile)
'Create a document builder that will allow us to modify the document.
oBuilder = New DocumentBuilder(oDoc)
'The cursor is in the first section already, navigate to the header as this is where
'we want to insert the watermark.
oBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)
'remove any existing watermarks
For Each oSection In oDoc.Sections
oSection.DeleteHeaderFooterShapes()
Next
oShape = oBuilder.InsertImage("C:\Program Files\Aspose\Aspose.Words\Demos\Documents\Aspose.Logo.gif")
'oShape = oBuilder.InsertImage("C:\Test Reports\draft8.GIF")
oShape.WrapType = WrapType.None
oShape.BehindText = True
' Position the image in the center of the page.
oShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
oShape.HorizontalAlignment = HorizontalAlignment.Center
oShape.RelativeVerticalPosition = RelativeVerticalPosition.Page
oShape.VerticalAlignment = VerticalAlignment.Center
'Insert a table with other office information in the footer.
oBuilder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary)
Dim topBorder As Aspose.Words.Border = oBuilder.CellFormat.Borders(BorderType.Top)
topBorder.LineStyle = LineStyle.Single
topBorder.LineWidth = 1
oBuilder.InsertCell()
oBuilder.CellFormat.Width = 3 \* 72
oBuilder.Write(String.Format("Processed on: {0}", DateTime.Today.ToLongDateString()))
oBuilder.InsertCell()
oBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Right
oBuilder.Write("Version 1.00")
oBuilder.EndRow()
oBuilder.EndTable()
oDoc.Save(szFile, SaveFormat.Doc)

Also I noticed that I will lose pages on the document. Is this because I am using the trial version of Aspose.Words?

Yes, it maybe caused by trial version limitations as trial version truncates the document. Try requesting a temporary license as described in
https://docs.aspose.com/words/net/licensing/