PPTContainerShapeBuilder.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. var ShapeTree = slidePart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
  28. if (ShapeTree == null)
  29. {
  30. return null;
  31. }
  32. var objs = ShapeTree.ChildElements;
  33. foreach (object obj in objs)
  34. {
  35. PPTShapeBase shape = null;
  36. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  37. {
  38. DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
  39. if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback")
  40. {
  41. continue;
  42. }
  43. //p:sp
  44. PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
  45. if (shapeppt.IsText)
  46. {
  47. if (shapeppt.placeholder == null)
  48. {
  49. shape = shapeppt;
  50. }
  51. }
  52. }
  53. else if (typeof(Picture).Equals(obj.GetType()))
  54. {
  55. //p:pic
  56. shape =new PPTImage(slidePart, (Picture)obj, slide);
  57. }
  58. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  59. {
  60. //p:graphicFrame Chart, Diagram, Table
  61. var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
  62. var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
  63. foreach (var clild in GraphicDataChildren)
  64. {
  65. if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
  66. {
  67. shape = new PPTTable(slidePart, graphicFrame, Table, slide);
  68. }
  69. if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
  70. {
  71. shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
  72. }
  73. if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
  74. {
  75. shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
  76. }
  77. }
  78. }
  79. else if (typeof(GroupShape).Equals(obj.GetType()))
  80. {
  81. //p:grpSp
  82. shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
  83. }
  84. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  85. {
  86. //p:cxnSp
  87. shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
  88. }
  89. else if (typeof(AlternateContent).Equals(obj.GetType()))
  90. {
  91. ///Equations and formulas as Image 处理公式 方程等
  92. AlternateContent alternateContent = (AlternateContent)obj;
  93. // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
  94. var AlternateContentChoice = alternateContent.GetFirstChild<AlternateContentChoice>();
  95. var shp = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  96. if (shp != null)
  97. {
  98. var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
  99. foreach (var pa in Paragraphs)
  100. {
  101. // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
  102. // OfficeMath
  103. ///公式插入 线性 专用,普通文本区别
  104. var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
  105. if (oMath != null)
  106. {
  107. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  108. AlternateContentFallback alternateContentFallback = null;
  109. if (element != null)
  110. {
  111. alternateContentFallback = (AlternateContentFallback)element;
  112. }
  113. shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
  114. }
  115. }
  116. }
  117. //MathML
  118. // shape =new PPTAlternateContent(slidePart, (AlternateContent)obj, slide);
  119. /// 处理公式 方程等
  120. }
  121. if (shape != null)
  122. {
  123. bool exist = false;
  124. foreach (var elm in pptContainerShape.Elements)
  125. {
  126. if (elm.suid == shape.suid)
  127. {
  128. exist = true;
  129. }
  130. }
  131. if (!exist)
  132. {
  133. pptContainerShape.Elements.Add(shape);
  134. }
  135. }
  136. }
  137. return pptContainerShape;
  138. }
  139. public PPTContainerShape GetPPTContainerShape(SlidePart slidePart, PPTSlide slide)
  140. {
  141. var pptContainerShape = new PPTContainerShape();
  142. pptContainerShape.Elements = new List<PPTShapeBase>();
  143. var ShapeTree = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
  144. if (ShapeTree == null) {
  145. return null;
  146. }
  147. var objs = ShapeTree.ChildElements;
  148. foreach (object obj in objs)
  149. {
  150. PPTShapeBase shape = null;
  151. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  152. {
  153. DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
  154. if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback") {
  155. continue;
  156. }
  157. //p:sp
  158. PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
  159. if (shapeppt.IsText) {
  160. shape = shapeppt;
  161. }
  162. }
  163. else if (typeof(Picture).Equals(obj.GetType()))
  164. {
  165. //p:pic
  166. shape =new PPTImage(slidePart, (Picture)obj,slide);
  167. }
  168. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  169. {
  170. //p:graphicFrame Chart, Diagram, Table
  171. var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
  172. var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
  173. foreach (var clild in GraphicDataChildren)
  174. {
  175. if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
  176. {
  177. shape = new PPTTable(slidePart, graphicFrame, Table, slide);
  178. }
  179. if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
  180. {
  181. shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
  182. }
  183. if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
  184. {
  185. shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
  186. }
  187. }
  188. }
  189. else if (typeof(GroupShape).Equals(obj.GetType()))
  190. {
  191. //p:grpSp
  192. shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
  193. }
  194. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  195. {
  196. //p:cxnSp
  197. shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
  198. }
  199. else if (typeof(AlternateContent).Equals(obj.GetType())) {
  200. ///Equations and formulas as Image 处理公式 方程等
  201. AlternateContent alternateContent = (AlternateContent)obj;
  202. // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
  203. var AlternateContentChoice= alternateContent.GetFirstChild<AlternateContentChoice>();
  204. var shp= AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  205. if (shp != null) {
  206. var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
  207. foreach (var pa in Paragraphs)
  208. {
  209. // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
  210. // OfficeMath
  211. ///公式插入 线性 专用,普通文本区别
  212. var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
  213. if (oMath != null)
  214. {
  215. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  216. AlternateContentFallback alternateContentFallback = null;
  217. if (element != null)
  218. {
  219. alternateContentFallback = (AlternateContentFallback)element;
  220. }
  221. shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
  222. }
  223. }
  224. }
  225. }
  226. if (shape != null) {
  227. bool exist = false;
  228. foreach (var elm in pptContainerShape.Elements)
  229. {
  230. if (elm.suid == shape.suid)
  231. {
  232. exist = true;
  233. }
  234. }
  235. if (!exist)
  236. {
  237. pptContainerShape.Elements.Add(shape);
  238. }
  239. }
  240. }
  241. return pptContainerShape;
  242. }
  243. public PPTContainerShape GetPPTContainerShape(SlideMasterPart slidePart, PPTSlide slide)
  244. {
  245. var pptContainerShape = new PPTContainerShape();
  246. pptContainerShape.Elements = new List<PPTShapeBase>();
  247. var ShapeTree = slidePart.SlideMaster.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
  248. if (ShapeTree == null)
  249. {
  250. return null;
  251. }
  252. var objs = ShapeTree.ChildElements;
  253. foreach (object obj in objs)
  254. {
  255. PPTShapeBase shape = null;
  256. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  257. {
  258. DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
  259. if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback")
  260. {
  261. continue;
  262. }
  263. //p:sp
  264. PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
  265. if (shapeppt.IsText)
  266. {
  267. shape = shapeppt;
  268. }
  269. }
  270. else if (typeof(Picture).Equals(obj.GetType()))
  271. {
  272. //p:pic
  273. shape = new PPTImage(slidePart, (Picture)obj, slide);
  274. }
  275. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  276. {
  277. //p:graphicFrame Chart, Diagram, Table
  278. var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
  279. var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
  280. foreach (var clild in GraphicDataChildren)
  281. {
  282. if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
  283. {
  284. shape = new PPTTable(slidePart, graphicFrame, Table, slide);
  285. }
  286. if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
  287. {
  288. shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
  289. }
  290. if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
  291. {
  292. shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
  293. }
  294. }
  295. }
  296. else if (typeof(GroupShape).Equals(obj.GetType()))
  297. {
  298. //p:grpSp
  299. shape = new PPTGroupShape(slidePart, (GroupShape)obj, slide);
  300. }
  301. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  302. {
  303. //p:cxnSp
  304. shape = new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
  305. }
  306. else if (typeof(AlternateContent).Equals(obj.GetType()))
  307. {
  308. ///Equations and formulas as Image 处理公式 方程等
  309. AlternateContent alternateContent = (AlternateContent)obj;
  310. // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
  311. var AlternateContentChoice = alternateContent.GetFirstChild<AlternateContentChoice>();
  312. var shp = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  313. if (shp != null)
  314. {
  315. var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
  316. foreach (var pa in Paragraphs)
  317. {
  318. // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
  319. // OfficeMath
  320. ///公式插入 线性 专用,普通文本区别
  321. var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
  322. if (oMath != null)
  323. {
  324. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  325. AlternateContentFallback alternateContentFallback = null;
  326. if (element != null)
  327. {
  328. alternateContentFallback = (AlternateContentFallback)element;
  329. }
  330. shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
  331. }
  332. }
  333. }
  334. }
  335. if (shape != null)
  336. {
  337. bool exist = false;
  338. foreach (var elm in pptContainerShape.Elements)
  339. {
  340. if (elm.suid == shape.suid)
  341. {
  342. exist = true;
  343. }
  344. }
  345. if (!exist)
  346. {
  347. pptContainerShape.Elements.Add(shape);
  348. }
  349. }
  350. }
  351. return pptContainerShape;
  352. }
  353. }
  354. }