15th January 2015

Open XML Usage - Part Four

Open XML Usage - Part Four

Part Four

Adding a text watermark.

Please refer to Part One of this series to see the Word document output and calling code.

The TextWatermark class is standard code to add a watermark in a diagonal line.

Also note that it is also possible to add an image watermark.

This is the calling code which takes the following parameters:

  • Document to insert watermark into
    • Watermark text
    • Watermark font
    • Watermark fill colour
//// Add a text watermark
TextWatermark textWatermark = new TextWatermark();
textWatermark.AddWatermark(wordDoc, "DRAFT", "font-family:\"Calibri\";font-size:medium", "#C0C0C0");

Below is the class to add a text watermark.

The watermark transparency effect is governed by the falling code:

//// Switches on the transparency
Fill fill1 = new Fill() 

Opacity = ".5"

; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Vml; using DocumentFormat.OpenXml.Vml.Office; using DocumentFormat.OpenXml.Vml.Wordprocessing; using DocumentFormat.OpenXml.Wordprocessing; using HorizontalAnchorValues = DocumentFormat.OpenXml.Vml.Wordprocessing.HorizontalAnchorValues; using Lock = DocumentFormat.OpenXml.Vml.Office.Lock; using VerticalAnchorValues = DocumentFormat.OpenXml.Vml.Wordprocessing.VerticalAnchorValues; namespace ExportToWord

public class TextWatermark

/// <summary> /// Adds the watermark. /// </summary> /// <param name="doc">The document.</param> /// <param name="watermarkText">The watermark text.</param> /// <param name="watermarkStyle">The watermark style.</param> /// <param name="watermarkFillColor">Color of the watermark fill.</param> public void AddWatermark(WordprocessingDocument doc, string watermarkText, string watermarkStyle, string watermarkFillColor)

foreach (HeaderPart headerPart in doc.MainDocumentPart.HeaderParts)

SdtBlock sdtBlock1 = new SdtBlock(); SdtProperties sdtProperties1 = new SdtProperties(); SdtId sdtId1 = new SdtId()

Val = 87908844

; SdtContentDocPartObject sdtContentDocPartObject1 = new SdtContentDocPartObject(); DocPartGallery docPartGallery1 = new DocPartGallery()

Val = "Watermarks"

; DocPartUnique docPartUnique1 = new DocPartUnique(); sdtContentDocPartObject1.Append(docPartGallery1); sdtContentDocPartObject1.Append(docPartUnique1); sdtProperties1.Append(sdtId1); sdtProperties1.Append(sdtContentDocPartObject1); SdtContentBlock sdtContentBlock1 = new SdtContentBlock(); Paragraph paragraph2 = new Paragraph()

RsidParagraphAddition = "00656E18", RsidRunAdditionDefault = "00656E18"

; ParagraphProperties paragraphProperties2 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId()

Val = "Header"

; paragraphProperties2.Append(paragraphStyleId2); Run run1 = new Run(); RunProperties runProperties1 = new RunProperties(); NoProof noProof1 = new NoProof(); Languages languages1 = new Languages()

EastAsia = "zh-TW"

; runProperties1.Append(noProof1); runProperties1.Append(languages1); Picture picture1 = new Picture(); Shapetype shapetype1 = new Shapetype()

Id = "_x0000_t136", CoordinateSize = "21600,21600", OptionalNumber = 136, Adjustment = "10800", EdgePath = "m@7,l@8,m@5,21600l@6,21600e"

; Formulas formulas1 = new Formulas(); Formula formula1 = new Formula()

Equation = "sum #0 0 10800"

; Formula formula2 = new Formula()

Equation = "prod #0 2 1"

; Formula formula3 = new Formula()

Equation = "sum 21600 0 @1"

; Formula formula4 = new Formula()

Equation = "sum 0 0 @2"

; Formula formula5 = new Formula()

Equation = "sum 21600 0 @3"

; Formula formula6 = new Formula()

Equation = "if @0 @3 0"

; Formula formula7 = new Formula()

Equation = "if @0 21600 @1"

; Formula formula8 = new Formula()

Equation = "if @0 0 @2"

; Formula formula9 = new Formula()

Equation = "if @0 @4 21600"

; Formula formula10 = new Formula()

Equation = "mid @5 @6"

; Formula formula11 = new Formula()

Equation = "mid @8 @5"

; Formula formula12 = new Formula()

Equation = "mid @7 @8"

; Formula formula13 = new Formula()

Equation = "mid @6 @7"

; Formula formula14 = new Formula()

Equation = "sum @6 0 @5"

; formulas1.Append(formula1); formulas1.Append(formula2); formulas1.Append(formula3); formulas1.Append(formula4); formulas1.Append(formula5); formulas1.Append(formula6); formulas1.Append(formula7); formulas1.Append(formula8); formulas1.Append(formula9); formulas1.Append(formula10); formulas1.Append(formula11); formulas1.Append(formula12); formulas1.Append(formula13); formulas1.Append(formula14); DocumentFormat.OpenXml.Vml.Path path1 = new DocumentFormat.OpenXml.Vml.Path()

AllowTextPath = true, ConnectionPointType = ConnectValues.Custom, ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", ConnectAngles = "270,180,90,0"

; TextPath textPath1 = new TextPath()

On = true, FitShape = true

; ShapeHandles shapeHandles1 = new ShapeHandles(); ShapeHandle shapeHandle1 = new ShapeHandle()

Position = "#0,bottomRight", XRange = "6629,14971"

; shapeHandles1.Append(shapeHandle1); var lock1 = new Lock

Extension = ExtensionHandlingBehaviorValues.Edit, TextLock = true, ShapeType = true

; shapetype1.Append(formulas1); shapetype1.Append(path1); shapetype1.Append(textPath1); shapetype1.Append(shapeHandles1); shapetype1.Append(lock1); //// Create the diagonal watermark shape Shape shape1 = new Shape()

Id = "PowerPlusWaterMarkObject357476642", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2049", AllowInCell = true, FillColor = watermarkFillColor, Stroked = false, Type = "#_x0000_t136"

; //// Switches on the transparency Fill fill1 = new Fill()

Opacity = ".5"

; //// Font Style and display text TextPath textPath2 = new TextPath()

Style = watermarkStyle, String = watermarkText

; TextWrap textWrap1 = new TextWrap()

AnchorX = HorizontalAnchorValues.Margin, AnchorY = VerticalAnchorValues.Margin

; shape1.Append(fill1); shape1.Append(textPath2); shape1.Append(textWrap1); picture1.Append(shapetype1); picture1.Append(shape1); run1.Append(runProperties1); run1.Append(picture1); paragraph2.Append(paragraphProperties2); paragraph2.Append(run1); sdtContentBlock1.Append(paragraph2); sdtBlock1.Append(sdtProperties1); sdtBlock1.Append(sdtContentBlock1); headerPart.Header.Append(sdtBlock1); headerPart.Header.Save();

Choose your interests: