Question

I need to insert text diagonal Watermark, in a word file with OpenXML, I use the following steps to open the file:

Dim wordmlNamespace As String = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"

Using wdDoc As WordprocessingDocument = WordprocessingDocument.Open(newPath, True) 
    'AddWatermarkFunction()
    wdDoc.MainDocumentPart.Document.Save()
    wdDoc.Close()
End Using

I have already seen this link, but I can not play the correct functioning.

UPDATE

I tried to write the following code (replaces 'AddWatermarkFunction() of the above code), but I do not print the watermark

  If wdDoc.MainDocumentPart.HeaderParts IsNot Nothing Then
       If wdDoc.MainDocumentPart.HeaderParts IsNot Nothing Then
            For Each Headerpart In wdDoc.MainDocumentPart.HeaderParts
                 Dim r As DocumentFormat.OpenXml.Wordprocessing.Run = CreateWatermarkRun("watermarkText")
                 Dim para As New DocumentFormat.OpenXml.Wordprocessing.Paragraph
                 para.Append(r)
                 Headerpart.Header.Save(Headerpart)
             Next
        End If
   End If

the function "CreateWatermarkRun":

    Private Function CreateWatermarkRun(ByVal name As String) As DocumentFormat.OpenXml.Wordprocessing.Run

    Dim runWatermark As New DocumentFormat.OpenXml.Wordprocessing.Run()

    Dim runWMProperties As New DocumentFormat.OpenXml.Wordprocessing.RunProperties()
    Dim noProofWM As New DocumentFormat.OpenXml.Wordprocessing.NoProof()
    Try

    runWMProperties.Append(noProofWM)

    Dim pictureWM As New DocumentFormat.OpenXml.Wordprocessing.Picture()

    Dim shapetypeWM As New DocumentFormat.OpenXml.Vml.Shapetype() With
    { _
        .Id = "_x0000_t136", _
        .CoordinateSize = "21600,21600", _
        .OptionalNumber = 136, _
        .Adjustment = "10800", _
        .EdgePath = "m@7,l@8,m@5,21600l@6,21600e" _
    }

    Dim formulasWM As New DocumentFormat.OpenXml.Vml.Formulas()
    Dim formula1 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "sum #0 0 10800" _
    }

    Dim formula2 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "prod #0 2 1" _
    }

    Dim formula3 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "sum 21600 0 @1" _
    }

    Dim formula4 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "sum 0 0 @2" _
    }

    Dim formula5 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "sum 21600 0 @3" _
    }

    Dim formula6 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "if @0 @3 0" _
    }

    Dim formula7 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "if @0 21600 @1" _
    }

    Dim formula8 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "if @0 0 @2" _
    }

    Dim formula9 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "if @0 @4 21600" _
    }

    Dim formula10 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "mid @5 @6" _
    }

    Dim formula11 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "mid @8 @5" _
    }
    Dim formula12 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "mid @7 @8" _
    }

    Dim formula13 As New DocumentFormat.OpenXml.Vml.Formula() With { _
      .Equation = "mid @6 @7" _
    }

    Dim formula14 As New DocumentFormat.OpenXml.Vml.Formula() With { _
     .Equation = "sum @6 0 @5" _
    }

    formulasWM.Append(formula1)
    formulasWM.Append(formula2)
    formulasWM.Append(formula3)
    formulasWM.Append(formula4)
    formulasWM.Append(formula5)
    formulasWM.Append(formula6)
    formulasWM.Append(formula7)
    formulasWM.Append(formula8)
    formulasWM.Append(formula9)
    formulasWM.Append(formula10)
    formulasWM.Append(formula11)
    formulasWM.Append(formula12)
    formulasWM.Append(formula13)
    formulasWM.Append(formula14)
    Dim pathWM As New DocumentFormat.OpenXml.Vml.Path() With { _
      .AllowTextPath = True, _
      .ConnectionPointType = DocumentFormat.OpenXml.Vml.Office.ConnectValues.[Custom], _
      .ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", _
      .ConnectAngles = "270,180,90,0" _
    }
    Dim textPathWM As New DocumentFormat.OpenXml.Vml.TextPath() With { _
      .[On] = True, _
      .FitShape = True _
    }

    Dim shapeHandlesWM As New DocumentFormat.OpenXml.Vml.ShapeHandles()
    Dim shapeHandleWM As New DocumentFormat.OpenXml.Vml.ShapeHandle() With { _
     .Position = "#0,bottomRight", _
     .XRange = "6629,14971" _
    }

    shapeHandlesWM.Append(shapeHandleWM)
    Dim lockWM As New DocumentFormat.OpenXml.Vml.Office.Lock() With { _
     .Extension = DocumentFormat.OpenXml.Vml.ExtensionHandlingBehaviorValues.Edit, _
     .TextLock = True, _
     .ShapeType = True _
    }

    shapetypeWM.Append(formulasWM)
    shapetypeWM.Append(pathWM)
    shapetypeWM.Append(textPathWM)
    shapetypeWM.Append(shapeHandlesWM)
    shapetypeWM.Append(lockWM)

    Dim shapeWM As New DocumentFormat.OpenXml.Vml.Shape() With { _
     .Id = "PowerPlusWaterMarkObject346762751", _
     .Style = "position:absolute;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:-251655168;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", _
     .OptionalString = "_x0000_s2050", _
      .AllowInCell = False, _
      .FillColor = "silver", _
      .Stroked = False, _
      .Type = "#_x0000_t136" _
    }

    Dim fillWM As New DocumentFormat.OpenXml.Vml.Fill() With { _
      .Opacity = ".5" _
    }

    Dim textPath2WM As New DocumentFormat.OpenXml.Vml.TextPath() With { _
     .Style = "font-family:""Arial"";font-size:1pt", _
     .[String] = name _
    }

    shapeWM.Append(fillWM)
    shapeWM.Append(textPath2WM)

    pictureWM.Append(shapetypeWM)
    pictureWM.Append(shapeWM)

    runWatermark.Append(runWMProperties)
    runWatermark.Append(pictureWM)

    Return runWatermark
    Catch ex As Exception
        ex.Message.ToString()
    End Try

End Function

Any suggestions?

Was it helpful?

Solution

I found the solution, missing only the portion of the code that added a paragraph to the document that is

....
Dim para As New DocumentFormat.OpenXml.Wordprocessing.Paragraph
DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
para.Append(r)

Headerpart.Header.Append(para) 'missing code

Headerpart.Header.Save(Headerpart)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top