PtOpenXmlDocument.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. /*
  4. Here is modification of a WmlDocument:
  5. public static WmlDocument SimplifyMarkup(WmlDocument doc, SimplifyMarkupSettings settings)
  6. {
  7. using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
  8. {
  9. using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
  10. {
  11. SimplifyMarkup(document, settings);
  12. }
  13. return streamDoc.GetModifiedWmlDocument();
  14. }
  15. }
  16. Here is read-only of a WmlDocument:
  17. public static string GetBackgroundColor(WmlDocument doc)
  18. {
  19. using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
  20. using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
  21. {
  22. XDocument mainDocument = document.MainDocumentPart.GetXDocument();
  23. XElement backgroundElement = mainDocument.Descendants(W.background).FirstOrDefault();
  24. return (backgroundElement == null) ? string.Empty : backgroundElement.Attribute(W.color).Value;
  25. }
  26. }
  27. Here is creating a new WmlDocument:
  28. private OpenXmlPowerToolsDocument CreateSplitDocument(WordprocessingDocument source, List<XElement> contents, string newFileName)
  29. {
  30. using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateWordprocessingDocument())
  31. {
  32. using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument())
  33. {
  34. DocumentBuilder.FixRanges(source.MainDocumentPart.GetXDocument(), contents);
  35. PowerToolsExtensions.SetContent(document, contents);
  36. }
  37. OpenXmlPowerToolsDocument newDoc = streamDoc.GetModifiedDocument();
  38. newDoc.FileName = newFileName;
  39. return newDoc;
  40. }
  41. }
  42. */
  43. using System;
  44. using System.IO;
  45. using System.Linq;
  46. using System.Xml.Linq;
  47. using System.IO.Packaging;
  48. using DocumentFormat.OpenXml.Packaging;
  49. namespace OpenXmlPowerTools
  50. {
  51. public class PowerToolsDocumentException : Exception
  52. {
  53. public PowerToolsDocumentException(string message) : base(message) { }
  54. }
  55. public class PowerToolsInvalidDataException : Exception
  56. {
  57. public PowerToolsInvalidDataException(string message) : base(message) { }
  58. }
  59. public class OpenXmlPowerToolsDocument
  60. {
  61. public string FileName { get; set; }
  62. public byte[] DocumentByteArray { get; set; }
  63. public static OpenXmlPowerToolsDocument FromFileName(string fileName)
  64. {
  65. byte[] bytes = File.ReadAllBytes(fileName);
  66. Type type;
  67. try
  68. {
  69. type = GetDocumentType(bytes);
  70. }
  71. catch (FileFormatException)
  72. {
  73. throw new PowerToolsDocumentException("Not an Open XML document.");
  74. }
  75. if (type == typeof(WordprocessingDocument))
  76. return new WmlDocument(fileName, bytes);
  77. if (type == typeof(SpreadsheetDocument))
  78. return new SmlDocument(fileName, bytes);
  79. if (type == typeof(PresentationDocument))
  80. return new PmlDocument(fileName, bytes);
  81. if (type == typeof(Package))
  82. {
  83. OpenXmlPowerToolsDocument pkg = new OpenXmlPowerToolsDocument(bytes);
  84. pkg.FileName = fileName;
  85. return pkg;
  86. }
  87. throw new PowerToolsDocumentException("Not an Open XML document.");
  88. }
  89. public static OpenXmlPowerToolsDocument FromDocument(OpenXmlPowerToolsDocument doc)
  90. {
  91. Type type = doc.GetDocumentType();
  92. if (type == typeof(WordprocessingDocument))
  93. return new WmlDocument(doc);
  94. if (type == typeof(SpreadsheetDocument))
  95. return new SmlDocument(doc);
  96. if (type == typeof(PresentationDocument))
  97. return new PmlDocument(doc);
  98. return null; // This should not be possible from a valid OpenXmlPowerToolsDocument object
  99. }
  100. public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original)
  101. {
  102. DocumentByteArray = new byte[original.DocumentByteArray.Length];
  103. Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
  104. FileName = original.FileName;
  105. }
  106. public OpenXmlPowerToolsDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
  107. {
  108. if (convertToTransitional)
  109. {
  110. ConvertToTransitional(original.FileName, original.DocumentByteArray);
  111. }
  112. else
  113. {
  114. DocumentByteArray = new byte[original.DocumentByteArray.Length];
  115. Array.Copy(original.DocumentByteArray, DocumentByteArray, original.DocumentByteArray.Length);
  116. FileName = original.FileName;
  117. }
  118. }
  119. public OpenXmlPowerToolsDocument(string fileName)
  120. {
  121. this.FileName = fileName;
  122. DocumentByteArray = File.ReadAllBytes(fileName);
  123. }
  124. public OpenXmlPowerToolsDocument(string fileName, bool convertToTransitional)
  125. {
  126. this.FileName = fileName;
  127. if (convertToTransitional)
  128. {
  129. var tempByteArray = File.ReadAllBytes(fileName);
  130. ConvertToTransitional(fileName, tempByteArray);
  131. }
  132. else
  133. {
  134. this.FileName = fileName;
  135. DocumentByteArray = File.ReadAllBytes(fileName);
  136. }
  137. }
  138. private void ConvertToTransitional(string fileName, byte[] tempByteArray)
  139. {
  140. Type type;
  141. try
  142. {
  143. type = GetDocumentType(tempByteArray);
  144. }
  145. catch (FileFormatException)
  146. {
  147. throw new PowerToolsDocumentException("Not an Open XML document.");
  148. }
  149. using (MemoryStream ms = new MemoryStream())
  150. {
  151. ms.Write(tempByteArray, 0, tempByteArray.Length);
  152. if (type == typeof(WordprocessingDocument))
  153. {
  154. using (WordprocessingDocument sDoc = WordprocessingDocument.Open(ms, true))
  155. {
  156. // following code forces the SDK to serialize
  157. foreach (var part in sDoc.Parts)
  158. {
  159. try
  160. {
  161. var z = part.OpenXmlPart.RootElement;
  162. }
  163. catch (Exception)
  164. {
  165. continue;
  166. }
  167. }
  168. }
  169. }
  170. else if (type == typeof(SpreadsheetDocument))
  171. {
  172. using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, true))
  173. {
  174. // following code forces the SDK to serialize
  175. foreach (var part in sDoc.Parts)
  176. {
  177. try
  178. {
  179. var z = part.OpenXmlPart.RootElement;
  180. }
  181. catch (Exception)
  182. {
  183. continue;
  184. }
  185. }
  186. }
  187. }
  188. else if (type == typeof(PresentationDocument))
  189. {
  190. using (PresentationDocument sDoc = PresentationDocument.Open(ms, true))
  191. {
  192. // following code forces the SDK to serialize
  193. foreach (var part in sDoc.Parts)
  194. {
  195. try
  196. {
  197. var z = part.OpenXmlPart.RootElement;
  198. }
  199. catch (Exception)
  200. {
  201. continue;
  202. }
  203. }
  204. }
  205. }
  206. this.FileName = fileName;
  207. DocumentByteArray = ms.ToArray();
  208. }
  209. }
  210. public OpenXmlPowerToolsDocument(byte[] byteArray)
  211. {
  212. DocumentByteArray = new byte[byteArray.Length];
  213. Array.Copy(byteArray, DocumentByteArray, byteArray.Length);
  214. this.FileName = null;
  215. }
  216. public OpenXmlPowerToolsDocument(byte[] byteArray, bool convertToTransitional)
  217. {
  218. if (convertToTransitional)
  219. {
  220. ConvertToTransitional(null, byteArray);
  221. }
  222. else
  223. {
  224. DocumentByteArray = new byte[byteArray.Length];
  225. Array.Copy(byteArray, DocumentByteArray, byteArray.Length);
  226. this.FileName = null;
  227. }
  228. }
  229. public OpenXmlPowerToolsDocument(string fileName, MemoryStream memStream)
  230. {
  231. FileName = fileName;
  232. DocumentByteArray = new byte[memStream.Length];
  233. Array.Copy(memStream.GetBuffer(), DocumentByteArray, memStream.Length);
  234. }
  235. public OpenXmlPowerToolsDocument(string fileName, MemoryStream memStream, bool convertToTransitional)
  236. {
  237. if (convertToTransitional)
  238. {
  239. ConvertToTransitional(fileName, memStream.ToArray());
  240. }
  241. else
  242. {
  243. FileName = fileName;
  244. DocumentByteArray = new byte[memStream.Length];
  245. Array.Copy(memStream.GetBuffer(), DocumentByteArray, memStream.Length);
  246. }
  247. }
  248. public string GetName()
  249. {
  250. if (FileName == null)
  251. return "Unnamed Document";
  252. FileInfo file = new FileInfo(FileName);
  253. return file.Name;
  254. }
  255. public void SaveAs(string fileName)
  256. {
  257. File.WriteAllBytes(fileName, DocumentByteArray);
  258. }
  259. public void Save()
  260. {
  261. if (this.FileName == null)
  262. throw new InvalidOperationException("Attempting to Save a document that has no file name. Use SaveAs instead.");
  263. File.WriteAllBytes(this.FileName, DocumentByteArray);
  264. }
  265. public void WriteByteArray(Stream stream)
  266. {
  267. stream.Write(DocumentByteArray, 0, DocumentByteArray.Length);
  268. }
  269. public Type GetDocumentType()
  270. {
  271. return GetDocumentType(DocumentByteArray);
  272. }
  273. private static Type GetDocumentType(byte[] bytes)
  274. {
  275. using (MemoryStream stream = new MemoryStream())
  276. {
  277. stream.Write(bytes, 0, bytes.Length);
  278. using (Package package = Package.Open(stream, FileMode.Open))
  279. {
  280. PackageRelationship relationship = package.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument").FirstOrDefault();
  281. if (relationship == null)
  282. relationship = package.GetRelationshipsByType("http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument").FirstOrDefault();
  283. if (relationship != null)
  284. {
  285. PackagePart part = package.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
  286. switch (part.ContentType)
  287. {
  288. case "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml":
  289. case "application/vnd.ms-word.document.macroEnabled.main+xml":
  290. case "application/vnd.ms-word.template.macroEnabledTemplate.main+xml":
  291. case "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml":
  292. return typeof(WordprocessingDocument);
  293. case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml":
  294. case "application/vnd.ms-excel.sheet.macroEnabled.main+xml":
  295. case "application/vnd.ms-excel.template.macroEnabled.main+xml":
  296. case "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml":
  297. return typeof(SpreadsheetDocument);
  298. case "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml":
  299. case "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml":
  300. case "application/vnd.ms-powerpoint.template.macroEnabled.main+xml":
  301. case "application/vnd.ms-powerpoint.addin.macroEnabled.main+xml":
  302. case "application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml":
  303. case "application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml":
  304. return typeof(PresentationDocument);
  305. }
  306. return typeof(Package);
  307. }
  308. return null;
  309. }
  310. }
  311. }
  312. public static void SavePartAs(OpenXmlPart part, string filePath)
  313. {
  314. Stream partStream = part.GetStream(FileMode.Open, FileAccess.Read);
  315. byte[] partContent = new byte[partStream.Length];
  316. partStream.Read(partContent, 0, (int)partStream.Length);
  317. File.WriteAllBytes(filePath, partContent);
  318. }
  319. }
  320. public partial class WmlDocument : OpenXmlPowerToolsDocument
  321. {
  322. public WmlDocument(OpenXmlPowerToolsDocument original)
  323. : base(original)
  324. {
  325. if (GetDocumentType() != typeof(WordprocessingDocument))
  326. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  327. }
  328. public WmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
  329. : base(original, convertToTransitional)
  330. {
  331. if (GetDocumentType() != typeof(WordprocessingDocument))
  332. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  333. }
  334. public WmlDocument(string fileName)
  335. : base(fileName)
  336. {
  337. if (GetDocumentType() != typeof(WordprocessingDocument))
  338. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  339. }
  340. public WmlDocument(string fileName, bool convertToTransitional)
  341. : base(fileName, convertToTransitional)
  342. {
  343. if (GetDocumentType() != typeof(WordprocessingDocument))
  344. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  345. }
  346. public WmlDocument(string fileName, byte[] byteArray)
  347. : base(byteArray)
  348. {
  349. FileName = fileName;
  350. if (GetDocumentType() != typeof(WordprocessingDocument))
  351. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  352. }
  353. public WmlDocument(string fileName, byte[] byteArray, bool convertToTransitional)
  354. : base(byteArray, convertToTransitional)
  355. {
  356. FileName = fileName;
  357. if (GetDocumentType() != typeof(WordprocessingDocument))
  358. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  359. }
  360. public WmlDocument(string fileName, MemoryStream memStream)
  361. : base(fileName, memStream)
  362. {
  363. }
  364. public WmlDocument(string fileName, MemoryStream memStream, bool convertToTransitional)
  365. : base(fileName, memStream, convertToTransitional)
  366. {
  367. }
  368. }
  369. public partial class SmlDocument : OpenXmlPowerToolsDocument
  370. {
  371. public SmlDocument(OpenXmlPowerToolsDocument original)
  372. : base(original)
  373. {
  374. if (GetDocumentType() != typeof(SpreadsheetDocument))
  375. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  376. }
  377. public SmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
  378. : base(original, convertToTransitional)
  379. {
  380. if (GetDocumentType() != typeof(SpreadsheetDocument))
  381. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  382. }
  383. public SmlDocument(string fileName)
  384. : base(fileName)
  385. {
  386. if (GetDocumentType() != typeof(SpreadsheetDocument))
  387. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  388. }
  389. public SmlDocument(string fileName, bool convertToTransitional)
  390. : base(fileName, convertToTransitional)
  391. {
  392. if (GetDocumentType() != typeof(SpreadsheetDocument))
  393. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  394. }
  395. public SmlDocument(string fileName, byte[] byteArray)
  396. : base(byteArray)
  397. {
  398. FileName = fileName;
  399. if (GetDocumentType() != typeof(SpreadsheetDocument))
  400. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  401. }
  402. public SmlDocument(string fileName, byte[] byteArray, bool convertToTransitional)
  403. : base(byteArray, convertToTransitional)
  404. {
  405. FileName = fileName;
  406. if (GetDocumentType() != typeof(SpreadsheetDocument))
  407. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  408. }
  409. public SmlDocument(string fileName, MemoryStream memStream)
  410. : base(fileName, memStream)
  411. {
  412. }
  413. public SmlDocument(string fileName, MemoryStream memStream, bool convertToTransitional)
  414. : base(fileName, memStream, convertToTransitional)
  415. {
  416. }
  417. }
  418. public partial class PmlDocument : OpenXmlPowerToolsDocument
  419. {
  420. public PmlDocument(OpenXmlPowerToolsDocument original)
  421. : base(original)
  422. {
  423. if (GetDocumentType() != typeof(PresentationDocument))
  424. throw new PowerToolsDocumentException("Not a Presentation document.");
  425. }
  426. public PmlDocument(OpenXmlPowerToolsDocument original, bool convertToTransitional)
  427. : base(original, convertToTransitional)
  428. {
  429. if (GetDocumentType() != typeof(PresentationDocument))
  430. throw new PowerToolsDocumentException("Not a Presentation document.");
  431. }
  432. public PmlDocument(string fileName)
  433. : base(fileName)
  434. {
  435. if (GetDocumentType() != typeof(PresentationDocument))
  436. throw new PowerToolsDocumentException("Not a Presentation document.");
  437. }
  438. public PmlDocument(string fileName, bool convertToTransitional)
  439. : base(fileName, convertToTransitional)
  440. {
  441. if (GetDocumentType() != typeof(PresentationDocument))
  442. throw new PowerToolsDocumentException("Not a Presentation document.");
  443. }
  444. public PmlDocument(string fileName, byte[] byteArray)
  445. : base(byteArray)
  446. {
  447. FileName = fileName;
  448. if (GetDocumentType() != typeof(PresentationDocument))
  449. throw new PowerToolsDocumentException("Not a Presentation document.");
  450. }
  451. public PmlDocument(string fileName, byte[] byteArray, bool convertToTransitional)
  452. : base(byteArray, convertToTransitional)
  453. {
  454. FileName = fileName;
  455. if (GetDocumentType() != typeof(PresentationDocument))
  456. throw new PowerToolsDocumentException("Not a Presentation document.");
  457. }
  458. public PmlDocument(string fileName, MemoryStream memStream)
  459. : base(fileName, memStream)
  460. {
  461. }
  462. public PmlDocument(string fileName, MemoryStream memStream, bool convertToTransitional)
  463. : base(fileName, memStream, convertToTransitional)
  464. {
  465. }
  466. }
  467. public class OpenXmlMemoryStreamDocument : IDisposable
  468. {
  469. private OpenXmlPowerToolsDocument Document;
  470. private MemoryStream DocMemoryStream;
  471. private Package DocPackage;
  472. public OpenXmlMemoryStreamDocument(OpenXmlPowerToolsDocument doc)
  473. {
  474. Document = doc;
  475. DocMemoryStream = new MemoryStream();
  476. DocMemoryStream.Write(doc.DocumentByteArray, 0, doc.DocumentByteArray.Length);
  477. try
  478. {
  479. DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
  480. }
  481. catch (Exception e)
  482. {
  483. throw new PowerToolsDocumentException(e.Message);
  484. }
  485. }
  486. internal OpenXmlMemoryStreamDocument(MemoryStream stream)
  487. {
  488. DocMemoryStream = stream;
  489. try
  490. {
  491. DocPackage = Package.Open(DocMemoryStream, FileMode.Open);
  492. }
  493. catch (Exception e)
  494. {
  495. throw new PowerToolsDocumentException(e.Message);
  496. }
  497. }
  498. public static OpenXmlMemoryStreamDocument CreateWordprocessingDocument()
  499. {
  500. MemoryStream stream = new MemoryStream();
  501. using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
  502. {
  503. doc.AddMainDocumentPart();
  504. doc.MainDocumentPart.PutXDocument(new XDocument(
  505. new XElement(W.document,
  506. new XAttribute(XNamespace.Xmlns + "w", W.w),
  507. new XAttribute(XNamespace.Xmlns + "r", R.r),
  508. new XElement(W.body))));
  509. doc.Close();
  510. return new OpenXmlMemoryStreamDocument(stream);
  511. }
  512. }
  513. public static OpenXmlMemoryStreamDocument CreateSpreadsheetDocument()
  514. {
  515. MemoryStream stream = new MemoryStream();
  516. using (SpreadsheetDocument doc = SpreadsheetDocument.Create(stream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
  517. {
  518. doc.AddWorkbookPart();
  519. XNamespace ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
  520. XNamespace relationshipsns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
  521. doc.WorkbookPart.PutXDocument(new XDocument(
  522. new XElement(ns + "workbook",
  523. new XAttribute("xmlns", ns),
  524. new XAttribute(XNamespace.Xmlns + "r", relationshipsns),
  525. new XElement(ns + "sheets"))));
  526. doc.Close();
  527. return new OpenXmlMemoryStreamDocument(stream);
  528. }
  529. }
  530. public static OpenXmlMemoryStreamDocument CreatePresentationDocument()
  531. {
  532. MemoryStream stream = new MemoryStream();
  533. using (PresentationDocument doc = PresentationDocument.Create(stream, DocumentFormat.OpenXml.PresentationDocumentType.Presentation))
  534. {
  535. doc.AddPresentationPart();
  536. XNamespace ns = "http://schemas.openxmlformats.org/presentationml/2006/main";
  537. XNamespace relationshipsns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
  538. XNamespace drawingns = "http://schemas.openxmlformats.org/drawingml/2006/main";
  539. doc.PresentationPart.PutXDocument(new XDocument(
  540. new XElement(ns + "presentation",
  541. new XAttribute(XNamespace.Xmlns + "a", drawingns),
  542. new XAttribute(XNamespace.Xmlns + "r", relationshipsns),
  543. new XAttribute(XNamespace.Xmlns + "p", ns),
  544. new XElement(ns + "sldMasterIdLst"),
  545. new XElement(ns + "sldIdLst"),
  546. new XElement(ns + "notesSz", new XAttribute("cx", "6858000"), new XAttribute("cy", "9144000")))));
  547. doc.Close();
  548. return new OpenXmlMemoryStreamDocument(stream);
  549. }
  550. }
  551. public static OpenXmlMemoryStreamDocument CreatePackage()
  552. {
  553. MemoryStream stream = new MemoryStream();
  554. Package package = Package.Open(stream, FileMode.Create);
  555. package.Close();
  556. return new OpenXmlMemoryStreamDocument(stream);
  557. }
  558. public Package GetPackage()
  559. {
  560. return DocPackage;
  561. }
  562. public WordprocessingDocument GetWordprocessingDocument()
  563. {
  564. try
  565. {
  566. if (GetDocumentType() != typeof(WordprocessingDocument))
  567. throw new PowerToolsDocumentException("Not a Wordprocessing document.");
  568. return WordprocessingDocument.Open(DocPackage);
  569. }
  570. catch (Exception e)
  571. {
  572. throw new PowerToolsDocumentException(e.Message);
  573. }
  574. }
  575. public SpreadsheetDocument GetSpreadsheetDocument()
  576. {
  577. try
  578. {
  579. if (GetDocumentType() != typeof(SpreadsheetDocument))
  580. throw new PowerToolsDocumentException("Not a Spreadsheet document.");
  581. return SpreadsheetDocument.Open(DocPackage);
  582. }
  583. catch (Exception e)
  584. {
  585. throw new PowerToolsDocumentException(e.Message);
  586. }
  587. }
  588. public PresentationDocument GetPresentationDocument()
  589. {
  590. try
  591. {
  592. if (GetDocumentType() != typeof(PresentationDocument))
  593. throw new PowerToolsDocumentException("Not a Presentation document.");
  594. return PresentationDocument.Open(DocPackage);
  595. }
  596. catch (Exception e)
  597. {
  598. throw new PowerToolsDocumentException(e.Message);
  599. }
  600. }
  601. public Type GetDocumentType()
  602. {
  603. PackageRelationship relationship = DocPackage.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument").FirstOrDefault();
  604. if (relationship == null)
  605. relationship = DocPackage.GetRelationshipsByType("http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument").FirstOrDefault();
  606. if (relationship == null)
  607. throw new PowerToolsDocumentException("Not an Open XML Document.");
  608. PackagePart part = DocPackage.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
  609. switch (part.ContentType)
  610. {
  611. case "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml":
  612. case "application/vnd.ms-word.document.macroEnabled.main+xml":
  613. case "application/vnd.ms-word.template.macroEnabledTemplate.main+xml":
  614. case "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml":
  615. return typeof(WordprocessingDocument);
  616. case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml":
  617. case "application/vnd.ms-excel.sheet.macroEnabled.main+xml":
  618. case "application/vnd.ms-excel.template.macroEnabled.main+xml":
  619. case "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml":
  620. return typeof(SpreadsheetDocument);
  621. case "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml":
  622. case "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml":
  623. case "application/vnd.ms-powerpoint.template.macroEnabled.main+xml":
  624. case "application/vnd.ms-powerpoint.addin.macroEnabled.main+xml":
  625. case "application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml":
  626. case "application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml":
  627. return typeof(PresentationDocument);
  628. }
  629. return null;
  630. }
  631. public OpenXmlPowerToolsDocument GetModifiedDocument()
  632. {
  633. DocPackage.Close();
  634. DocPackage = null;
  635. return new OpenXmlPowerToolsDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
  636. }
  637. public WmlDocument GetModifiedWmlDocument()
  638. {
  639. DocPackage.Close();
  640. DocPackage = null;
  641. return new WmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
  642. }
  643. public SmlDocument GetModifiedSmlDocument()
  644. {
  645. DocPackage.Close();
  646. DocPackage = null;
  647. return new SmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
  648. }
  649. public PmlDocument GetModifiedPmlDocument()
  650. {
  651. DocPackage.Close();
  652. DocPackage = null;
  653. return new PmlDocument((Document == null) ? null : Document.FileName, DocMemoryStream);
  654. }
  655. public void Close()
  656. {
  657. Dispose(true);
  658. }
  659. public void Dispose()
  660. {
  661. Dispose(true);
  662. }
  663. ~OpenXmlMemoryStreamDocument()
  664. {
  665. Dispose(false);
  666. }
  667. private void Dispose(Boolean disposing)
  668. {
  669. if (disposing)
  670. {
  671. if (DocPackage != null)
  672. {
  673. DocPackage.Close();
  674. }
  675. if (DocMemoryStream != null)
  676. {
  677. DocMemoryStream.Dispose();
  678. }
  679. }
  680. if (DocPackage == null && DocMemoryStream == null)
  681. return;
  682. DocPackage = null;
  683. DocMemoryStream = null;
  684. GC.SuppressFinalize(this);
  685. }
  686. }
  687. }