Thursday, October 22, 2009

OpenXml – Adding a watermark to a document

Dependencies:

DocumentFormat.OpenXml.dll (version 2.0.3930.0)

Imports:

DocumentFormat.OpenXml.WordProcessing

DocumentFormat.OpenXml.Packaging

Code Snippet
  1. Private Sub CreateWatermarkedPageHeaderPart(ByRef myDocMainPart As MainDocumentPart, ByVal paragraphs As IEnumerable(Of XElement))
  2. Dim docPartObj As New DocPartObjectSdt
  3. Dim doc As New DocPartGallery() With {.Val = "Watermarks"}
  4. Dim docPartUnique As New DocPartUnique
  5. docPartObj.Append(doc, docPartUnique)
  6. Dim sdtContent As New SdtContentBlock
  7. Dim p As New Paragraph
  8. Dim pp As New ParagraphProperties
  9. Dim ps As New ParagraphStyleId With {.Val = "Header"}
  10. pp.Append(ps)
  11. Dim r As New Run
  12. Dim rPr As New RunProperties
  13. Dim no As New NoProof
  14. Dim la As New Languages With {.EastAsia = "zh-TW"}
  15. rPr.Append(no, la)
  16. Dim pict As New Picture
  17. Dim newId As String = (Guid.NewGuid.ToString("N"))
  18. Dim shap As New Shapetype With {.Id = newId, .CoordinateSize = "21600,21600", .OptionalNumber = 136, .Adjustment = "10800", .EdgePath = "m@7,l@8,m@5,21600l@6,21600e"}
  19. Dim form As New Formulas
  20. Dim fval1 As New Formula With {.Equation = "sum #0 0 10800"}
  21. Dim fval2 As New Formula With {.Equation = "prod #0 2 1"}
  22. Dim fval3 As New Formula With {.Equation = "sum 21600 0 @1"}
  23. Dim fval4 As New Formula With {.Equation = "sum 0 0 @2"}
  24. Dim fval4a As New Formula With {.Equation = "sum 21600 0 @3"}
  25. Dim fval5 As New Formula With {.Equation = "if @0 @3 0"}
  26. Dim fval6 As New Formula With {.Equation = "if @0 21600 @1"}
  27. Dim fval7 As New Formula With {.Equation = "if @0 0 @2"}
  28. Dim fval8 As New Formula With {.Equation = "if @0 @4 21600"}
  29. Dim fval9 As New Formula With {.Equation = "mid @5 @6"}
  30. Dim fval10 As New Formula With {.Equation = "mid @8 @5"}
  31. Dim fval11 As New Formula With {.Equation = "mid @7 @8"}
  32. Dim fval12 As New Formula With {.Equation = "mid @6 @7"}
  33. Dim fval13 As New Formula With {.Equation = "sum @6 0 @5"}
  34. form.Append(fval1, fval2, fval3, fval4, fval4a, fval5, fval6, fval7, fval8, fval9, fval10, fval11, fval12, fval13)
  35. Dim path As New Vml.Path With {.AllowTextPath = Vml.BooleanValues.T, .ConnectionPointType = Vml.Office.ConnectValues.Custom, .ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", .ConnectAngles = "270,180,90,0"}
  36. Dim txtP As New Vml.TextPath With {.On = Vml.BooleanValues.T, .FitShape = Vml.BooleanValues.T}
  37. Dim hdls As New Vml.Handles
  38. Dim hdl As New Vml.Handle With {.Position = "#0,bottomRight", .XRange = "6629,14971"}
  39. hdls.Append(hdl)
  40. shap.Append(form, path, txtP, hdls)
  41. Dim shape As New Vml.Shape With {.Id = "PowerPlusWaterMarkObject357831064", .OptionalString = "", .Type = "#" & newId, .Style = "position:absolute;margin-left:0;margin-top:0;width:412.4pt;height:247.45pt;rotation:315;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", .AllowInCell = Vml.BooleanValues.F, .FillColor = "#7f7f7f [1612]", .Stroked = Vml.BooleanValues.F}
  42. Dim fill As New Vml.Fill With {.Opacity = ".5"}
  43. Dim shTxt As New Vml.TextPath With {.Style = "font-family:""Calibri"";font-size:1pt", .String = "DRAFT"}
  44. Dim w10 As New Vml.Wordprocessing.TextWrap With {.AnchorX = Vml.Wordprocessing.HorizontalAnchorValues.Margin, .AnchorY = Vml.Wordprocessing.VerticalAnchorValues.Margin}
  45. shape.Append(fill, shTxt, w10)
  46. pict.Append(shap, shape)
  47. r.Append(rPr, pict)
  48. p.Append(pp, r)
  49. sdtContent.Append(p)
  50. Dim sdt As New SdtBlock
  51. Dim sdtPr As New SdtProperties
  52. sdtPr.Append(docPartObj)
  53. sdt.Append(sdtPr, sdtContent)
  54. Dim head As New Header
  55. head.Append(sdt)
  56. Dim headerPart As HeaderPart = myDocMainPart.AddNewPart(Of HeaderPart)()
  57. head.Save(headerPart.GetStream())
  58. Dim relId As String = myDocMainPart.GetIdOfPart(headerPart)
  59. Dim mainPart As XDocument = myDocMainPart.GetXDocument()
  60. Dim section As XElement = mainPart.Root.Element(ns + "body").Element(ns + "sectPr")
  61. Dim headAttId As XAttribute = New XAttribute(ns_r + "id", relId)
  62. Dim headAttType As XAttribute = New XAttribute("type", "default")
  63. Dim headerElement As XElement = New XElement(ns + "headerReference", headAttId, headAttType)
  64. section.AddFirst(headerElement)
  65. 'Add draft to each section in paragraphs
  66. For Each sectionPr As XElement In paragraphs.Descendants(ns + "sectPr")
  67. sectionPr.AddFirst(headerElement)
  68. Next
  69. End Sub

No comments:

Post a Comment

Calling .NET From COM Originally posted by Mikec276 on C Sharp Friends It might be hard to convince your IT manager to let you build y...