forked from visualon/OpenXmlPowerTools-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PtOpenXmlDocument.cs
535 lines (477 loc) · 20.5 KB
/
PtOpenXmlDocument.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/***************************************************************************
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
***************************************************************************/
/*
Here is modification of a WmlDocument:
public static WmlDocument SimplifyMarkup(WmlDocument doc, SimplifyMarkupSettings settings)
{
using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
{
using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
{
SimplifyMarkup(document, settings);
}
return streamDoc.GetModifiedWmlDocument();
}
}
Here is read-only of a WmlDocument:
public static string GetBackgroundColor(WmlDocument doc)
{
using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
{
XDocument mainDocument = document.MainDocumentPart.GetXDocument();
XElement backgroundElement = mainDocument.Descendants(W.background).FirstOrDefault();
return (backgroundElement == null) ? string.Empty : backgroundElement.Attribute(W.color).Value;
}
}
Here is creating a new WmlDocument:
private OpenXmlPowerToolsDocument CreateSplitDocument(WordprocessingDocument source, List<XElement> contents, string newFileName)
{
using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateWordprocessingDocument())
{
using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
{
DocumentBuilder.FixRanges(source.MainDocumentPart.GetXDocument(), contents);
PowerToolsExtensions.SetContent(document, contents);
}
OpenXmlPowerToolsDocument newDoc = streamDoc.GetModifiedDocument();
newDoc.FileName = newFileName;
return newDoc;
}
}
*/
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.IO.Packaging;
using DocumentFormat.OpenXml.Packaging;
namespace OpenXmlPowerTools
{
public class PowerToolsDocumentException : Exception
{
public PowerToolsDocumentException(string message) : base(message) { }
}
public class PowerToolsInvalidDataException : Exception
{
public PowerToolsInvalidDataException(string message) : base(message) { }
}
public class OpenXmlPowerToolsDocument
{
public string FileName { get; set; }
public byte[] DocumentByteArray { get; set; }
public static OpenXmlPowerToolsDocument FromFileName(string fileName)
{
byte[] bytes = File.ReadAllBytes(fileName);
Type type;
try
{
type = GetDocumentType(bytes);
}
catch (FileFormatException)
{
throw new PowerToolsDocumentException("Not an Open XML document.");
}
if (type == typeof(WordprocessingDocument))
return new WmlDocument(fileName, bytes);
if (type == typeof(SpreadsheetDocument))
return new SmlDocument(fileName, bytes);
if (type == typeof(PresentationDocument))
return new PmlDocument(fileName, bytes);
if (type == typeof(Package))
{
OpenXmlPowerToolsDocument pkg = new OpenXmlPowerToolsDocument(bytes);
pkg.FileName = fileName;
return pkg;
}
throw new PowerToolsDocumentException("Not an Open XML document.");
}
public static OpenXmlPowerToolsDocument FromDocument(OpenXmlPowerToolsDocument doc)
{
Type type = doc.GetDocumentType();
if (type == typeof(WordprocessingDocument))
return new WmlDocument(doc);
if (type == typeof(SpreadsheetDocument))
return new SmlDocument(doc);
if (type == typeof(PresentationDocument))
return new PmlDocument(doc);
return null; // This should not be possible from a valid OpenXmlPowerToolsDocument object
}
public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original)
{
DocumentByteArray = new byte[original.DocumentByteArray.Length];
Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
FileName = original.FileName;
}
public OpenXmlPowerToolsDocument(string fileName)
{
this.FileName = fileName;
DocumentByteArray = File.ReadAllBytes(fileName);
}
public OpenXmlPowerToolsDocument(byte[] byteArray)
{
DocumentByteArray = new byte[byteArray.Length];
Array.Copy(byteArray, DocumentByteArray, byteArray.Length);
this.FileName = null;
}
public OpenXmlPowerToolsDocument(string fileName, MemoryStream memStream)
{
FileName = fileName;
DocumentByteArray = new byte[memStream.Length];
Array.Copy(memStream.GetBuffer(), DocumentByteArray, memStream.Length);
}
public string GetName()
{
if (FileName == null)
return "Unnamed Document";
FileInfo file = new FileInfo(FileName);
return file.Name;
}
public void SaveAs(string fileName)
{
File.WriteAllBytes(fileName, DocumentByteArray);
}
public void Save()
{
if (this.FileName == null)
throw new InvalidOperationException("Attempting to Save a document that has no file name. Use SaveAs instead.");
File.WriteAllBytes(this.FileName, DocumentByteArray);
}
public void WriteByteArray(Stream stream)
{
stream.Write(DocumentByteArray, 0, DocumentByteArray.Length);
}
public Type GetDocumentType()
{
return GetDocumentType(DocumentByteArray);
}
private static Type GetDocumentType(byte[] bytes)
{
using (MemoryStream stream = new MemoryStream(bytes))
using (Package package = Package.Open(stream, FileMode.Open))
{
PackageRelationship relationship = package.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument").FirstOrDefault();
if (relationship != null)
{
PackagePart part = package.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
switch (part.ContentType)
{
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml":
return typeof(WordprocessingDocument);
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml":
return typeof(SpreadsheetDocument);
case "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml":
return typeof(PresentationDocument);
}
return typeof(Package);
}
return null;
}
}
public static void SavePartAs(OpenXmlPart part, string filePath)
{
Stream partStream = part.GetStream(FileMode.Open, FileAccess.Read);
byte[] partContent = new byte[partStream.Length];
partStream.Read(partContent, 0, (int)partStream.Length);
File.WriteAllBytes(filePath, partContent);
}
}
public partial class WmlDocument : OpenXmlPowerToolsDocument
{
public WmlDocument(OpenXmlPowerToolsDocument original)
: base(original)
{
if (GetDocumentType() != typeof(WordprocessingDocument))
throw new PowerToolsDocumentException("Not a Wordprocessing document.");
}
public WmlDocument(string fileName)
: base(fileName)
{
if (GetDocumentType() != typeof(WordprocessingDocument))
throw new PowerToolsDocumentException("Not a Wordprocessing document.");
}
public WmlDocument(string fileName, byte[] byteArray)
: base(byteArray)
{
FileName = fileName;
if (GetDocumentType() != typeof(WordprocessingDocument))
throw new PowerToolsDocumentException("Not a Wordprocessing document.");
}
public WmlDocument(string fileName, MemoryStream memStream)
: base(fileName, memStream)
{
}
}
public partial class SmlDocument : OpenXmlPowerToolsDocument
{
public SmlDocument(OpenXmlPowerToolsDocument original)
: base(original)
{
if (GetDocumentType() != typeof(SpreadsheetDocument))
throw new PowerToolsDocumentException("Not a Spreadsheet document.");
}
public SmlDocument(string fileName)
: base(fileName)
{
if (GetDocumentType() != typeof(SpreadsheetDocument))
throw new PowerToolsDocumentException("Not a Spreadsheet document.");
}
public SmlDocument(string fileName, byte[] byteArray)
: base(byteArray)
{
FileName = fileName;
if (GetDocumentType() != typeof(SpreadsheetDocument))
throw new PowerToolsDocumentException("Not a Spreadsheet document.");
}
public SmlDocument(string fileName, MemoryStream memStream)
: base(fileName, memStream)
{
}
}
public partial class PmlDocument : OpenXmlPowerToolsDocument
{
public PmlDocument(OpenXmlPowerToolsDocument original)
: base(original)
{
if (GetDocumentType() != typeof(PresentationDocument))
throw new PowerToolsDocumentException("Not a Presentation document.");
}
public PmlDocument(string fileName)
: base(fileName)
{
if (GetDocumentType() != typeof(PresentationDocument))
throw new PowerToolsDocumentException("Not a Presentation document.");
}
public PmlDocument(string fileName, byte[] byteArray)
: base(byteArray)
{
FileName = fileName;
if (GetDocumentType() != typeof(PresentationDocument))
throw new PowerToolsDocumentException("Not a Presentation document.");
}
public PmlDocument(string fileName, MemoryStream memStream)
: base(fileName, memStream)
{
}
}
public class OpenXmlMemoryStreamDocument : IDisposable
{
private OpenXmlPowerToolsDocument Document;
private MemoryStream DocMemoryStream;
private Package DocPackage;
public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
{
Document = doc;
DocMemoryStream = new MemoryStream();
doc.WriteByteArray(DocMemoryStream);
try
{
DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
}
catch (Exception e)
{
throw new PowerToolsDocumentException(e.Message);
}
}
internal OpenXmlMemoryStreamDocument(MemoryStream stream)
{
DocMemoryStream = stream;
try
{
DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
}
catch (Exception e)
{
throw new PowerToolsDocumentException(e.Message);
}
}
public static OpenXmlMemoryStreamDocument CreateWordprocessingDocument()
{
MemoryStream stream = new MemoryStream();
using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
doc.AddMainDocumentPart();
doc.MainDocumentPart.PutXDocument(new XDocument(
new XElement(W.document,
new XAttribute(XNamespace.Xmlns + "w", W.w),
new XAttribute(XNamespace.Xmlns + "r", R.r),
new XElement(W.body))));
doc.Close();
return new OpenXmlMemoryStreamDocument(stream);
}
}
public static OpenXmlMemoryStreamDocument CreateSpreadsheetDocument()
{
MemoryStream stream = new MemoryStream();
using (SpreadsheetDocument doc = SpreadsheetDocument.Create(stream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
doc.AddWorkbookPart();
XNamespace ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
XNamespace relationshipsns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
doc.WorkbookPart.PutXDocument(new XDocument(
new XElement(ns + "workbook",
new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "r", relationshipsns),
new XElement(ns + "sheets"))));
doc.Close();
return new OpenXmlMemoryStreamDocument(stream);
}
}
public static OpenXmlMemoryStreamDocument CreatePresentationDocument()
{
MemoryStream stream = new MemoryStream();
using (PresentationDocument doc = PresentationDocument.Create(stream, DocumentFormat.OpenXml.PresentationDocumentType.Presentation))
{
doc.AddPresentationPart();
XNamespace ns = "http://schemas.openxmlformats.org/presentationml/2006/main";
XNamespace relationshipsns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
XNamespace drawingns = "http://schemas.openxmlformats.org/drawingml/2006/main";
doc.PresentationPart.PutXDocument(new XDocument(
new XElement(ns + "presentation",
new XAttribute(XNamespace.Xmlns + "a", drawingns),
new XAttribute(XNamespace.Xmlns + "r", relationshipsns),
new XAttribute(XNamespace.Xmlns + "p", ns),
new XElement(ns + "sldMasterIdLst"),
new XElement(ns + "sldIdLst"),
new XElement(ns + "notesSz", new XAttribute("cx", "6858000"), new XAttribute("cy", "9144000")))));
doc.Close();
return new OpenXmlMemoryStreamDocument(stream);
}
}
public static OpenXmlMemoryStreamDocument CreatePackage()
{
MemoryStream stream = new MemoryStream();
Package package = Package.Open(stream, FileMode.Create);
package.Close();
return new OpenXmlMemoryStreamDocument(stream);
}
public Package GetPackage()
{
return DocPackage;
}
public WordprocessingDocument GetWordprocessingDocument()
{
try
{
if (GetDocumentType() != typeof(WordprocessingDocument))
throw new PowerToolsDocumentException("Not a Wordprocessing document.");
return WordprocessingDocument.Open(DocPackage);
}
catch (Exception e)
{
throw new PowerToolsDocumentException(e.Message);
}
}
public SpreadsheetDocument GetSpreadsheetDocument()
{
try
{
if (GetDocumentType() != typeof(SpreadsheetDocument))
throw new PowerToolsDocumentException("Not a Spreadsheet document.");
return SpreadsheetDocument.Open(DocPackage);
}
catch (Exception e)
{
throw new PowerToolsDocumentException(e.Message);
}
}
public PresentationDocument GetPresentationDocument()
{
try
{
if (GetDocumentType() != typeof(PresentationDocument))
throw new PowerToolsDocumentException("Not a Presentation document.");
return PresentationDocument.Open(DocPackage);
}
catch (Exception e)
{
throw new PowerToolsDocumentException(e.Message);
}
}
public Type GetDocumentType()
{
PackageRelationship relationship = DocPackage.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument").FirstOrDefault();
if (relationship == null)
throw new PowerToolsDocumentException("Not an Open XML Document.");
PackagePart part = DocPackage.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
switch (part.ContentType)
{
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml":
return typeof(WordprocessingDocument);
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml":
return typeof(SpreadsheetDocument);
case "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml":
return typeof(PresentationDocument);
}
return null;
}
public OpenXmlPowerToolsDocument GetModifiedDocument()
{
DocPackage.Close();
DocPackage = null;
return new OpenXmlPowerToolsDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
}
public WmlDocument GetModifiedWmlDocument()
{
DocPackage.Close();
DocPackage = null;
return new WmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
}
public SmlDocument GetModifiedSmlDocument()
{
DocPackage.Close();
DocPackage = null;
return new SmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
}
public PmlDocument GetModifiedPmlDocument()
{
DocPackage.Close();
DocPackage = null;
return new PmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
}
public void Close()
{
Dispose(true);
}
public void Dispose()
{
Dispose(true);
}
~OpenXmlMemoryStreamDocument()
{
Dispose(false);
}
private void Dispose(Boolean disposing)
{
if (disposing)
{
if (DocPackage != null)
{
DocPackage.Close();
}
if (DocMemoryStream != null)
{
DocMemoryStream.Dispose();
}
}
if (DocPackage == null && DocMemoryStream == null)
return;
DocPackage = null;
DocMemoryStream = null;
GC.SuppressFinalize(this);
}
}
}