DOCX.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Xml;
  6. namespace DOCX
  7. {
  8. public class Docx : IDisposable
  9. {
  10. private readonly ZipArchive _zip;
  11. private readonly XmlNamespaceManager _ns = new XmlNamespaceManager(new NameTable());
  12. private readonly Dictionary<string, string> _namespaces = new Dictionary<string, string>
  13. {
  14. {"w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}
  15. };
  16. private void LoadNamespaces()
  17. {
  18. foreach (var item in _namespaces)
  19. {
  20. _ns.AddNamespace(item.Key, item.Value);
  21. }
  22. }
  23. public Docx(string path)
  24. {
  25. _zip = ZipFile.Open(path, ZipArchiveMode.Update);
  26. LoadNamespaces();
  27. }
  28. public Docx(ZipArchive zipArchive)
  29. {
  30. _zip = zipArchive;
  31. LoadNamespaces();
  32. }
  33. private (XmlDocument doc, string message) GetXML(ZipArchiveEntry entry)
  34. {
  35. XmlDocument doc = new XmlDocument()
  36. {
  37. PreserveWhitespace = true // Disables auto-indent
  38. };
  39. try
  40. {
  41. using (StreamReader sr = new StreamReader(entry.Open()))
  42. {
  43. doc.Load(sr);
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. return (null, $"Error reading {entry.Name}!\n{e}");
  49. }
  50. return (doc, "OK");
  51. }
  52. private (bool status, string message) SaveXML(XmlDocument doc, ZipArchiveEntry entry)
  53. {
  54. try
  55. {
  56. using (var sr = entry.Open())
  57. {
  58. sr.SetLength(doc.OuterXml.Length);
  59. using (StreamWriter sw = new StreamWriter(sr))
  60. {
  61. doc.Save(sw);
  62. }
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. return (false, $"Error saving {entry.Name}!\n{e}");
  68. }
  69. return (true, "OK");
  70. }
  71. private (bool status, string message) AddTrackRevisions(ZipArchiveEntry settings)
  72. {
  73. var loadResult = GetXML(settings);
  74. if (loadResult.doc == null)
  75. return (false, loadResult.message);
  76. XmlDocument doc = loadResult.doc;
  77. if (doc.SelectSingleNode("//w:trackRevisions", _ns) != null) return (true, "No change needed.");
  78. XmlElement trackRevisions = doc.CreateElement("w", "trackRevisions", _namespaces["w"]);
  79. if (doc.DocumentElement == null)
  80. return (false, "No root element in settings.xml!");
  81. doc.DocumentElement.AppendChild(trackRevisions);
  82. return SaveXML(doc, settings);
  83. }
  84. public (bool status, string message) EnableTrackedChanges()
  85. {
  86. ZipArchiveEntry settings = _zip.GetEntry(@"word/settings.xml");
  87. if (settings == null)
  88. return (false,
  89. "Can't access settings.xml!");
  90. var result = AddTrackRevisions(settings);
  91. return !result.status ? (false, result.message) : (true, "OK");
  92. }
  93. private Dictionary<string, string> _authors = new Dictionary<string, string>();
  94. private string AnonymizeName(string name)
  95. {
  96. if (_authors.TryGetValue(name, out var anonymousName))
  97. return anonymousName;
  98. anonymousName = $"Author{_authors.Count + 1}";
  99. _authors.Add(name, anonymousName);
  100. return anonymousName;
  101. }
  102. private (bool status, string message) AnonymizeAuthors(ZipArchiveEntry comments)
  103. {
  104. var loadResult = GetXML(comments);
  105. if (loadResult.doc == null)
  106. return (false, loadResult.message);
  107. XmlDocument doc = loadResult.doc;
  108. var commentNodes = doc.SelectNodes("//w:comment", _ns);
  109. if (commentNodes == null)
  110. return (false, "There are no comments!");
  111. foreach (XmlNode node in commentNodes)
  112. {
  113. var author = node.Attributes["w:author"];
  114. author.Value = AnonymizeName(author.Value);
  115. }
  116. return SaveXML(doc, comments);
  117. }
  118. public (bool status, string message) AnonymizeComments()
  119. {
  120. ZipArchiveEntry comments = _zip.GetEntry(@"word/comments.xml");
  121. if (comments == null)
  122. return (false,
  123. "Can't access comments.xml!");
  124. var result = AnonymizeAuthors(comments);
  125. return !result.status ? (false, result.message) : (true, "OK");
  126. }
  127. public void Dispose()
  128. {
  129. Dispose(true);
  130. GC.SuppressFinalize(this);
  131. }
  132. ~Docx()
  133. {
  134. Dispose(false);
  135. }
  136. protected virtual void Dispose(bool disposing)
  137. {
  138. if (disposing)
  139. {
  140. _zip.Dispose();
  141. }
  142. }
  143. }
  144. }