PPTContainerShapeBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Math;
  3. using DocumentFormat.OpenXml.Office2010.Drawing;
  4. using DocumentFormat.OpenXml.Packaging;
  5. using DocumentFormat.OpenXml.Presentation;
  6. using HTEXLib.Helpers.ShapeHelpers;
  7. using HTEXLib.Models;
  8. using HTEXLib.Models.Inner;
  9. using HTEXLib.Models.PPTX;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Xml;
  16. using System.Xml.Linq;
  17. using System.Xml.Xsl;
  18. namespace HTEXLib.Builders
  19. {
  20. internal class PPTContainerShapeBuilder
  21. {
  22. public PPTContainerShape GetPPTContainerShape(SlideLayoutPart slidePart, PPTSlide slide)
  23. {
  24. var pptContainerShape = new PPTContainerShape();
  25. pptContainerShape.Elements = new List<PPTShapeBase>();
  26. var objs = slidePart.SlideLayout.Descendants();
  27. foreach (object obj in objs)
  28. {
  29. PPTShapeBase shape = null;
  30. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  31. {
  32. DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
  33. if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback")
  34. {
  35. continue;
  36. }
  37. //p:sp
  38. PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
  39. if (shapeppt.IsText)
  40. {
  41. if (shapeppt.placeholder == null)
  42. {
  43. shape = shapeppt;
  44. }
  45. }
  46. }
  47. else if (typeof(Picture).Equals(obj.GetType()))
  48. {
  49. //p:pic
  50. shape =new PPTImage(slidePart, (Picture)obj, slide);
  51. }
  52. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  53. {
  54. //p:graphicFrame Chart, Diagram, Table
  55. shape =new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
  56. }
  57. else if (typeof(GroupShape).Equals(obj.GetType()))
  58. {
  59. //p:grpSp
  60. shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
  61. }
  62. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  63. {
  64. //p:cxnSp
  65. shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
  66. }
  67. else if (typeof(AlternateContent).Equals(obj.GetType()))
  68. {
  69. ///Equations and formulas as Image 处理公式 方程等
  70. AlternateContent alternateContent = (AlternateContent)obj;
  71. // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
  72. var AlternateContentChoice = alternateContent.GetFirstChild<AlternateContentChoice>();
  73. var shp = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  74. if (shp != null)
  75. {
  76. var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
  77. foreach (var pa in Paragraphs)
  78. {
  79. // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
  80. // OfficeMath
  81. ///公式插入 线性 专用,普通文本区别
  82. var oMath = shp.GetTextByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
  83. if (oMath != null)
  84. {
  85. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  86. AlternateContentFallback alternateContentFallback = null;
  87. if (element != null)
  88. {
  89. alternateContentFallback = (AlternateContentFallback)element;
  90. }
  91. shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
  92. }
  93. }
  94. }
  95. //MathML
  96. // shape =new PPTAlternateContent(slidePart, (AlternateContent)obj, slide);
  97. /// 处理公式 方程等
  98. }
  99. if (shape != null)
  100. {
  101. bool exist = false;
  102. foreach (var elm in pptContainerShape.Elements)
  103. {
  104. if (elm.suid == shape.suid)
  105. {
  106. exist = true;
  107. }
  108. }
  109. if (!exist)
  110. {
  111. pptContainerShape.Elements.Add(shape);
  112. }
  113. }
  114. }
  115. return pptContainerShape;
  116. }
  117. public PPTContainerShape GetPPTContainerShape(SlidePart slidePart, PPTSlide slide)
  118. {
  119. var pptContainerShape = new PPTContainerShape();
  120. pptContainerShape.Elements = new List<PPTShapeBase>();
  121. var objs = slidePart.Slide.Descendants();
  122. foreach (object obj in objs)
  123. {
  124. PPTShapeBase shape = null;
  125. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  126. {
  127. DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
  128. if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback") {
  129. continue;
  130. }
  131. //p:sp
  132. PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
  133. if (shapeppt.IsText) {
  134. shape = shapeppt;
  135. }
  136. }
  137. else if (typeof(Picture).Equals(obj.GetType()))
  138. {
  139. //p:pic
  140. shape =new PPTImage(slidePart, (Picture)obj,slide);
  141. }
  142. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  143. {
  144. //p:graphicFrame Chart, Diagram, Table
  145. shape =new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
  146. }
  147. else if (typeof(GroupShape).Equals(obj.GetType()))
  148. {
  149. //p:grpSp
  150. shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
  151. }
  152. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  153. {
  154. //p:cxnSp
  155. shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
  156. }
  157. else if (typeof(AlternateContent).Equals(obj.GetType())) {
  158. ///Equations and formulas as Image 处理公式 方程等
  159. AlternateContent alternateContent = (AlternateContent)obj;
  160. // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
  161. var AlternateContentChoice= alternateContent.GetFirstChild<AlternateContentChoice>();
  162. var shp= AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  163. if (shp != null) {
  164. var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
  165. foreach (var pa in Paragraphs)
  166. {
  167. // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
  168. // OfficeMath
  169. ///公式插入 线性 专用,普通文本区别
  170. var oMath = shp.GetTextByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
  171. if (oMath != null)
  172. {
  173. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  174. AlternateContentFallback alternateContentFallback = null;
  175. if (element != null)
  176. {
  177. alternateContentFallback = (AlternateContentFallback)element;
  178. }
  179. shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
  180. }
  181. }
  182. }
  183. }
  184. if (shape != null) {
  185. bool exist = false;
  186. foreach (var elm in pptContainerShape.Elements)
  187. {
  188. if (elm.suid == shape.suid)
  189. {
  190. exist = true;
  191. }
  192. }
  193. if (!exist)
  194. {
  195. pptContainerShape.Elements.Add(shape);
  196. }
  197. }
  198. }
  199. return pptContainerShape;
  200. }
  201. /// <summary>
  202. /// 处理xml 中的非ASCII编码的特殊公式字符
  203. /// </summary>
  204. /// <param name="element"></param>
  205. public void DoXml(XElement element) {
  206. foreach (var elm in element.Elements()) {
  207. var child = elm.Elements().ToList();
  208. if (child != null && child.Count>0) {
  209. DoXml(elm);
  210. }
  211. else{
  212. if (!string.IsNullOrEmpty(elm.Value)) {
  213. string em = element.Value;
  214. var keys = CharHelper.MathChar.Keys;
  215. foreach (string key in keys) {
  216. em= em.Replace(key, CharHelper.MathChar[key]);
  217. }
  218. elm.Value = em;
  219. elm.ReplaceWith(elm);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }