forked from visualon/OpenXmlPowerTools-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WmlDocument.cs
124 lines (109 loc) · 4.87 KB
/
WmlDocument.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/***************************************************************************
Copyright (c) Microsoft Corporation 2012-2013.
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
Published at http://OpenXmlDeveloper.org
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
Developer: Eric White
Blog: http://www.ericwhite.com
Twitter: @EricWhiteDev
Email: [email protected]
Version: 2.6.00
* Enhancements to support HtmlConverter.cs
***************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
namespace OpenXmlPowerTools
{
public class PtMainDocumentPart : XElement
{
private WmlDocument ParentWmlDocument;
public PtWordprocessingCommentsPart WordprocessingCommentsPart
{
get
{
using (MemoryStream ms = new MemoryStream(ParentWmlDocument.DocumentByteArray))
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, false))
{
WordprocessingCommentsPart commentsPart = wDoc.MainDocumentPart.WordprocessingCommentsPart;
if (commentsPart == null)
return null;
XElement partElement = commentsPart.GetXDocument().Root;
var childNodes = partElement.Nodes().ToList();
foreach (var item in childNodes)
item.Remove();
return new PtWordprocessingCommentsPart(this.ParentWmlDocument, commentsPart.Uri, partElement.Name, partElement.Attributes(), childNodes);
}
}
}
public PtMainDocumentPart(WmlDocument wmlDocument, Uri uri, XName name, params object[] values)
: base(name, values)
{
ParentWmlDocument = wmlDocument;
this.Add(
new XAttribute(PtOpenXml.Uri, uri),
new XAttribute(XNamespace.Xmlns + "pt", PtOpenXml.pt)
);
}
}
public class PtWordprocessingCommentsPart : XElement
{
private WmlDocument ParentWmlDocument;
public PtWordprocessingCommentsPart(WmlDocument wmlDocument, Uri uri, XName name, params object[] values)
: base(name, values)
{
ParentWmlDocument = wmlDocument;
this.Add(
new XAttribute(PtOpenXml.Uri, uri),
new XAttribute(XNamespace.Xmlns + "pt", PtOpenXml.pt)
);
}
}
public partial class WmlDocument
{
public PtMainDocumentPart MainDocumentPart
{
get
{
using (MemoryStream ms = new MemoryStream(this.DocumentByteArray))
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, false))
{
XElement partElement = wDoc.MainDocumentPart.GetXDocument().Root;
var childNodes = partElement.Nodes().ToList();
foreach (var item in childNodes)
item.Remove();
return new PtMainDocumentPart(this, wDoc.MainDocumentPart.Uri, partElement.Name, partElement.Attributes(), childNodes);
}
}
}
public WmlDocument(WmlDocument other, params XElement[] replacementParts)
: base(other)
{
using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(this))
{
using (Package package = streamDoc.GetPackage())
{
foreach (var replacementPart in replacementParts)
{
XAttribute uriAttribute = replacementPart.Attribute(PtOpenXml.Uri);
if (uriAttribute == null)
throw new OpenXmlPowerToolsException("Replacement part does not contain a Uri as an attribute");
String uri = uriAttribute.Value;
var part = package.GetParts().FirstOrDefault(p => p.Uri.ToString() == uri);
using (Stream partStream = part.GetStream(FileMode.Create, FileAccess.Write))
using (XmlWriter partXmlWriter = XmlWriter.Create(partStream))
replacementPart.Save(partXmlWriter);
}
}
this.DocumentByteArray = streamDoc.GetModifiedDocument().DocumentByteArray;
}
}
}
}