1: /// <summary>
2: /// Inserts a bookmark in the document.
3: /// </summary>
4: /// <param name="document">The document.</param>
5: /// <param name="bookmarkName">Name of the bookmark.</param>
6: public static void InsertBookmark(string document, string bookmarkName)
7: { 8: using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
9: { 10: using (Stream stream = wordDoc.MainDocumentPart.GetStream())
11: { 12: //create a xmldocument from the passed xml stream
13: XmlDocument xmlDocument = new XmlDocument();
14: xmlDocument.LoadXml(new StreamReader(stream).ReadToEnd());
15:
16: //find all paragraph nodes and add the bookmark at the latest position.
17: XmlNodeList nodes = FindNodes(xmlDocument, "/w:document/w:body/w:p");
18: if (nodes.Count > 0)
19: { 20: string bookmarkID = Guid.NewGuid().ToString();
21:
22: //create the bookmark string
23: string bookmark = string.Format("<w:bookmarkStart w:id=\"{0}\" w:name=\"{1}\"/><w:bookmarkEnd w:id=\"{2}\" />", bookmarkID, bookmarkName, bookmarkID); 24:
25: //add the bookmark at the latest position
26: nodes[nodes.Count - 1].CreateNavigator().InsertAfter(bookmark);
27:
28: //reset the stream and fill it with the new content
29: byte[] buf = (new UTF8Encoding()).GetBytes(xmlDocument.OuterXml);
30: stream.Seek(0, 0);
31: stream.Write(buf, 0, buf.Length);
32: }
33: }
34: }
35: }
36:
37:
38: /// <summary>
39: /// Finds some nodes in the xml.
40: /// This is extracted to a method, because so many namespaces.
41: /// </summary>
42: /// <param name="xmlDocument">The XML document.</param>
43: /// <param name="xPathExpression">The x path expression.</param>
44: /// <returns></returns>
45: public static XmlNodeList FindNodes(XmlDocument xmlDocument, string xPathExpression)
46: { 47: //create the namespace manager and add some namespaces
48: XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
49: namespaceManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); 50: namespaceManager.AddNamespace("tns", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"); 51: namespaceManager.AddNamespace("dcmitype", "http://purl.org/dc/dcmitype/"); 52: namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); 53: namespaceManager.AddNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"); 54: namespaceManager.AddNamespace("ds", "http://schemas.openxmlformats.org/officeDocument/2006/customXml"); 55: namespaceManager.AddNamespace("vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"); 56: namespaceManager.AddNamespace("v", "urn:schemas-microsoft-com:vml"); 57: namespaceManager.AddNamespace("w10", "urn:schemas-microsoft-com:office:word"); 58: namespaceManager.AddNamespace("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); 59: namespaceManager.AddNamespace("b", "http://schemas.openxmlformats.org/officeDocument/2006/bibliography"); 60: namespaceManager.AddNamespace("sl", "http://schemas.openxmlformats.org/schemaLibrary/2006/main"); 61: namespaceManager.AddNamespace("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); 62: namespaceManager.AddNamespace("o", "urn:schemas-microsoft-com:office:office"); 63: namespaceManager.AddNamespace("dcterms", "http://purl.org/dc/terms/"); 64: namespaceManager.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); 65: namespaceManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); 66: namespaceManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); 67: namespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 68: namespaceManager.AddNamespace("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006"); 69: namespaceManager.AddNamespace("pkg", "http://schemas.microsoft.com/office/2006/xmlPackage"); 70:
71: return xmlDocument.SelectNodes(xPathExpression, namespaceManager);
72: }