Script to add text watermark to writer doc

I have worked out a script to take data from a calc file and use that to alter a writer file and export it to a PDF. I need to modify this script to add a text watermark using the Format > Watermark functionality. How can I do that?

Current version of the script follows:

Dim path As String

Dim PTGuideDoc As Object
Dim PTListDoc As Object
Dim PTListSheet as Object
Dim PTLIDCell as Object
Dim PTLNameCell as Object
Dim rg as Object
Dim uR as Long

Dim args()
Dim PDFargs(0) As New “com.sun.star.beans.PropertyValue”
PDFargs(0).Name = “FilterName”
PDFargs(0).Value = “writer_pdf_Export”

path = ConvertToUrl("/home/wrg/Downloads/myDoc.odt")
PTGuideDoc = StarDesktop.loadComponentFromURL(path, "default", 0, args())

path = ConvertToUrl("/home/wrg/Downloads/listing.ods")
PTListDoc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
PTListSheet = PTListDoc.Sheets(0)
PTListRanage = PTListSheet.getCellRangeByName("A2:C251")

uR = PTListRanage.Rows.Count - 1
For r = 1 To uR
PTLIDCell = PTListSheet.getCellByPosition(0,r)
PTLNameCell = PTListSheet.getCellByPosition(1,r)

  If PTLNameCell.String <> "" Then
  	
  	if PTGuideDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor") then
  		oVar = PTGuideDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor")
  		oVar.DependentTextFields(0).content = PTLNameCell.String
  	else
  		msgbox "WhoFor not found"
  	end if
  	
  	if PTGuideDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID") then
  		oVar = PTGuideDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID")
  		oVar.DependentTextFields(0).content = PTLIDCell.String
  	else
  		msgbox "WhoForID not found"
  	end if
  	
  	
      REM code to alter text watermark in ODT file.
  	
  	
  	
      path = ConvertToURL("/home/wrg/myPDF_ED_"+PTLIDCell.String+").pdf")
      PTGuideDoc.storeToURL(path, PDFargs())
  End If

Next r

PTListDoc.close(True)
PTGuideDoc.close(True)

Worked it out myself. If it helps anyone else here is the updated script:

Dim path As String

Dim PTGuideDoc As Object
Dim PTListDoc As Object
Dim PTListSheet as Object
Dim PTLIDCell as Object
Dim PTLNameCell as Object
Dim rg as Object
Dim uR as Long

Dim args()
Dim PDFargs(0) As New "com.sun.star.beans.PropertyValue"
Dim Watermarkargs(4) As New "com.sun.star.beans.PropertyValue"

PDFargs(0).Name = "FilterName"
PDFargs(0).Value = "writer_pdf_Export"

path = ConvertToUrl("/home/USER/myDoc.odt")
PTGuideDoc = StarDesktop.loadComponentFromURL(path, "default", 0, args())

path = ConvertToUrl("/home/USER/listing.ods")
PTListDoc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
PTListSheet = PTListDoc.Sheets(0)
PTListRanage = PTListSheet.getCellRangeByName("A2:C251")

Watermarkargs(0).Name = "Text"
Watermarkargs(0).Value = "Init" 
Watermarkargs(1).Name = "Font"
Watermarkargs(1).Value = "Malgun Gothic Semilight" 
Watermarkargs(2).Name = "Angle"
Watermarkargs(2).Value = 315 ' Degrees, int '
Watermarkargs(3).Name = "Transparency"
Watermarkargs(3).Value = 88 ' Percent, int '
Watermarkargs(4).Name = "Color"
Watermarkargs(4).Value = 12632256 ' C9C9C9 = grey; number only '

document   = PTGuideDoc.CurrentController.Frame
oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")

uR = PTListRanage.Rows.Count - 1
For r = 1 To uR
	PTLIDCell = PTListSheet.getCellByPosition(0,r)
	PTLNameCell = PTListSheet.getCellByPosition(1,r)
	
	If PTLNameCell.String <> "" Then
		
		if PTGuideDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor") then
			oVar = PTGuideDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor")
			oVar.DependentTextFields(0).content = PTLNameCell.String
		else
			msgbox "WhoFor not found"
		end if

		if PTGuideDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID") then
			oVar = PTGuideDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID")
			oVar.DependentTextFields(0).content = PTLIDCell.String
		else
			msgbox "WhoForID not found"
		end if

		REM add text watermark
		Watermarkargs(0).Value = "Some Text "+PTLIDCell.String 
		oDispHelper.executeDispatch(document, ".uno:Watermark", "", 0, Watermarkargs())

		path = ConvertToURL("/home/USER/myPDF"+PTLIDCell.String+").pdf")
		PTGuideDoc.storeToURL(path, PDFargs())
	End If
Next r

PTListDoc.close(True)
PTGuideDoc.close(True)