none
OpenXml - Insert Watermark in Word with VB.NET RRS feed

  • Question

  • I have a web applications (asp.net) and I need to insert text diagonal Watermark, in a word file (word 2010) 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) 
        
    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
    
    wdDoc.MainDocumentPart.Document.Save()
    wdDoc.Close()
    
    End Using

    The function CreateWatermarkRun is this

    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()
    
        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 = ";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
    
    End Function

    When I run my web application, not return errors, but not print nothing.

    Thanks!

    Wednesday, April 23, 2014 7:59 AM

Answers

  • 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)

    Wednesday, April 23, 2014 2:33 PM

All replies

  • 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)

    Wednesday, April 23, 2014 2:33 PM
  • Hi

    Thank you for sharing the solution.

    It would be helpful for other community members.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 24, 2014 2:51 AM
    Moderator