Dependencies:
DocumentFormat.OpenXml.dll (version 2.0.3930.0)
Imports:
DocumentFormat.OpenXml.WordProcessing
DocumentFormat.OpenXml.Packaging
Code Snippet
- Private Sub CreateWatermarkedPageHeaderPart(ByRef myDocMainPart As MainDocumentPart, ByVal paragraphs As IEnumerable(Of XElement))
- Dim docPartObj As New DocPartObjectSdt
- Dim doc As New DocPartGallery() With {.Val = "Watermarks"}
- Dim docPartUnique As New DocPartUnique
- docPartObj.Append(doc, docPartUnique)
- Dim sdtContent As New SdtContentBlock
- Dim p As New Paragraph
- Dim pp As New ParagraphProperties
- Dim ps As New ParagraphStyleId With {.Val = "Header"}
- pp.Append(ps)
- Dim r As New Run
- Dim rPr As New RunProperties
- Dim no As New NoProof
- Dim la As New Languages With {.EastAsia = "zh-TW"}
- rPr.Append(no, la)
- Dim pict As New Picture
- Dim newId As String = (Guid.NewGuid.ToString("N"))
- 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"}
- Dim form As New Formulas
- Dim fval1 As New Formula With {.Equation = "sum #0 0 10800"}
- Dim fval2 As New Formula With {.Equation = "prod #0 2 1"}
- Dim fval3 As New Formula With {.Equation = "sum 21600 0 @1"}
- Dim fval4 As New Formula With {.Equation = "sum 0 0 @2"}
- Dim fval4a As New Formula With {.Equation = "sum 21600 0 @3"}
- Dim fval5 As New Formula With {.Equation = "if @0 @3 0"}
- Dim fval6 As New Formula With {.Equation = "if @0 21600 @1"}
- Dim fval7 As New Formula With {.Equation = "if @0 0 @2"}
- Dim fval8 As New Formula With {.Equation = "if @0 @4 21600"}
- Dim fval9 As New Formula With {.Equation = "mid @5 @6"}
- Dim fval10 As New Formula With {.Equation = "mid @8 @5"}
- Dim fval11 As New Formula With {.Equation = "mid @7 @8"}
- Dim fval12 As New Formula With {.Equation = "mid @6 @7"}
- Dim fval13 As New Formula With {.Equation = "sum @6 0 @5"}
- form.Append(fval1, fval2, fval3, fval4, fval4a, fval5, fval6, fval7, fval8, fval9, fval10, fval11, fval12, fval13)
- 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"}
- Dim txtP As New Vml.TextPath With {.On = Vml.BooleanValues.T, .FitShape = Vml.BooleanValues.T}
- Dim hdls As New Vml.Handles
- Dim hdl As New Vml.Handle With {.Position = "#0,bottomRight", .XRange = "6629,14971"}
- hdls.Append(hdl)
- shap.Append(form, path, txtP, hdls)
- 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}
- Dim fill As New Vml.Fill With {.Opacity = ".5"}
- Dim shTxt As New Vml.TextPath With {.Style = "font-family:""Calibri"";font-size:1pt", .String = "DRAFT"}
- Dim w10 As New Vml.Wordprocessing.TextWrap With {.AnchorX = Vml.Wordprocessing.HorizontalAnchorValues.Margin, .AnchorY = Vml.Wordprocessing.VerticalAnchorValues.Margin}
- shape.Append(fill, shTxt, w10)
- pict.Append(shap, shape)
- r.Append(rPr, pict)
- p.Append(pp, r)
- sdtContent.Append(p)
- Dim sdt As New SdtBlock
- Dim sdtPr As New SdtProperties
- sdtPr.Append(docPartObj)
- sdt.Append(sdtPr, sdtContent)
- Dim head As New Header
- head.Append(sdt)
- Dim headerPart As HeaderPart = myDocMainPart.AddNewPart(Of HeaderPart)()
- head.Save(headerPart.GetStream())
- Dim relId As String = myDocMainPart.GetIdOfPart(headerPart)
- Dim mainPart As XDocument = myDocMainPart.GetXDocument()
- Dim section As XElement = mainPart.Root.Element(ns + "body").Element(ns + "sectPr")
- Dim headAttId As XAttribute = New XAttribute(ns_r + "id", relId)
- Dim headAttType As XAttribute = New XAttribute("type", "default")
- Dim headerElement As XElement = New XElement(ns + "headerReference", headAttId, headAttType)
- section.AddFirst(headerElement)
- 'Add draft to each section in paragraphs
- For Each sectionPr As XElement In paragraphs.Descendants(ns + "sectPr")
- sectionPr.AddFirst(headerElement)
- Next
- End Sub
No comments:
Post a Comment