/// <summary>
 /// Gets the document structure related to watermark description
 /// </summary>
 /// <returns>Document structure related to watermark description</returns>
 public static IEnumerable <XElement> GetWatermark(WmlDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
         using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
         {
             //  to get the watermark text, we have to look inside the document
             //  get the default header reference and get the header reference id part
             XElement defaultHeaderReference = HeaderAccessor.GetHeaderReference(document, HeaderType.Default, 0);
             if (defaultHeaderReference != null)
             {
                 string      headerReferenceId = defaultHeaderReference.Attribute(relationshipsns + "id").Value;
                 OpenXmlPart headerPart        = document.MainDocumentPart.GetPartById(headerReferenceId);
                 if (headerPart != null)
                 {
                     XDocument headerPartXml = headerPart.GetXDocument();
                     return(headerPartXml.Descendants(ns + "pict"));
                 }
             }
             return(null);
         }
 }
        /// <summary>
        /// Inserts a watermark text inside a document
        /// </summary>
        /// <param name="watermarkText">text to show in the watermark</param>
        /// <param name="diagonalOrientation">specify that the text orientation will be in a diagonal way</param>
        public static OpenXmlPowerToolsDocument InsertWatermark(WmlDocument doc, string watermarkText, bool diagonalOrientation)
        {
            using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
            {
                using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
                {
                    Collection <XDocument> headers = new Collection <XDocument>();

                    if (HeaderAccessor.GetHeaderReference(document, HeaderType.First, 0) == null)
                    {
                        headers.Add(HeaderAccessor.AddNewHeader(document, HeaderType.First));
                    }
                    else
                    {
                        headers.Add(HeaderAccessor.GetHeader(document, HeaderType.First, 0));
                    }

                    if (HeaderAccessor.GetHeaderReference(document, HeaderType.Even, 0) == null)
                    {
                        headers.Add(HeaderAccessor.AddNewHeader(document, HeaderType.Even));
                    }
                    else
                    {
                        headers.Add(HeaderAccessor.GetHeader(document, HeaderType.Even, 0));
                    }

                    if (HeaderAccessor.GetHeaderReference(document, HeaderType.Default, 0) == null)
                    {
                        headers.Add(HeaderAccessor.AddNewHeader(document, HeaderType.Default));
                    }
                    else
                    {
                        headers.Add(HeaderAccessor.GetHeader(document, HeaderType.Default, 0));
                    }

                    foreach (XDocument header in headers)
                    {
                        var runElement = header.Descendants(ns + "r").FirstOrDefault();
                        if (runElement == null)
                        {
                            header.Root.Add(
                                new XElement(ns + "sdt",
                                             new XElement(ns + "sdtContent",
                                                          new XElement(ns + "p",
                                                                       new XElement(ns + "pPr",
                                                                                    new XElement(ns + "pStyle",
                                                                                                 new XAttribute(ns + "val", "Header")
                                                                                                 )
                                                                                    ),
                                                                       runElement = new XElement(ns + "r")
                                                                       )
                                                          )
                                             )
                                );
                        }
                        runElement.AddBeforeSelf(CreateWatermarkVml(watermarkText, diagonalOrientation));
                    }

                    HeaderAccessor.GetHeaderPart(document, HeaderType.First, 0).PutXDocument();
                    HeaderAccessor.GetHeaderPart(document, HeaderType.Even, 0).PutXDocument();
                    HeaderAccessor.GetHeaderPart(document, HeaderType.Default, 0).PutXDocument();
                }
                return(streamDoc.GetModifiedDocument());
            }
        }