PPTContainerShapeBuilder.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml.Presentation;
  4. using HTEXLib.Models;
  5. using HTEXLib.Models.Inner;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace HTEXLib.Builders
  10. {
  11. internal class PPTContainerShapeBuilder
  12. {
  13. public PPTContainerShape GetPPTContainerShape(SlideLayoutPart slidePart, PPTSlide slide)
  14. {
  15. var pptContainerShape = new PPTContainerShape();
  16. pptContainerShape.Elements = new List<PPTShapeBase>();
  17. var objs = slidePart.SlideLayout.Descendants();
  18. foreach (object obj in objs)
  19. {
  20. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  21. {
  22. //p:sp
  23. pptContainerShape.Elements.Add(new PPTShape(slidePart, (DocumentFormat.OpenXml.Presentation.Shape)obj, slide));
  24. }
  25. else if (typeof(Picture).Equals(obj.GetType()))
  26. {
  27. //p:pic
  28. pptContainerShape.Elements.Add(new PPTImage(slidePart, (Picture)obj));
  29. }
  30. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  31. {
  32. //p:graphicFrame Chart, Diagram, Table
  33. pptContainerShape.Elements.Add(new PPTGraphicFrame(slidePart, (GraphicFrame)obj));
  34. }
  35. else if (typeof(GroupShape).Equals(obj.GetType()))
  36. {
  37. //p:grpSp
  38. pptContainerShape.Elements.Add(new PPTGroupShape(slidePart, (GroupShape)obj,slide));
  39. }
  40. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  41. {
  42. //p:cxnSp
  43. pptContainerShape.Elements.Add(new PPTConnectionShape(slidePart, (ConnectionShape)obj));
  44. }
  45. else if (typeof(AlternateContent).Equals(obj.GetType()))
  46. {
  47. ///Equations and formulas as Image 处理公式 方程等
  48. AlternateContent alternateContent = (AlternateContent)obj;
  49. var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  50. if (element != null)
  51. {
  52. AlternateContentFallback alternateContentFallback = (AlternateContentFallback)element;
  53. if (alternateContentFallback.ChildElements.First() is DocumentFormat.OpenXml.Presentation.Shape shapea)
  54. {
  55. pptContainerShape.Elements.Add(new PPTShape(slidePart, shapea, slide));
  56. }
  57. else if (alternateContentFallback.ChildElements.First() is DocumentFormat.OpenXml.Presentation.Picture Picturea)
  58. {
  59. pptContainerShape.Elements.Add(new PPTImage(slidePart, Picturea));
  60. }
  61. }
  62. //MathML
  63. // pptContainerShape.Elements.Add(new PPTAlternateContent(slidePart, (AlternateContent)obj, slide));
  64. /// 处理公式 方程等
  65. }
  66. }
  67. return pptContainerShape;
  68. }
  69. public PPTContainerShape GetPPTContainerShape(SlidePart slidePart, PPTSlide slide)
  70. {
  71. var pptContainerShape = new PPTContainerShape();
  72. pptContainerShape.Elements = new List<PPTShapeBase>();
  73. var objs = slidePart.Slide.Descendants();
  74. foreach (object obj in objs)
  75. {
  76. if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
  77. {
  78. //p:sp
  79. pptContainerShape.Elements.Add(new PPTShape(slidePart, (DocumentFormat.OpenXml.Presentation.Shape)obj, slide));
  80. }
  81. else if (typeof(Picture).Equals(obj.GetType()))
  82. {
  83. //p:pic
  84. pptContainerShape.Elements.Add(new PPTImage(slidePart, (Picture)obj));
  85. }
  86. else if (typeof(GraphicFrame).Equals(obj.GetType()))
  87. {
  88. //p:graphicFrame Chart, Diagram, Table
  89. pptContainerShape.Elements.Add(new PPTGraphicFrame(slidePart, (GraphicFrame)obj));
  90. }
  91. else if (typeof(GroupShape).Equals(obj.GetType()))
  92. {
  93. //p:grpSp
  94. pptContainerShape.Elements.Add(new PPTGroupShape(slidePart, (GroupShape)obj,slide));
  95. }
  96. else if (typeof(ConnectionShape).Equals(obj.GetType()))
  97. {
  98. //p:cxnSp
  99. pptContainerShape.Elements.Add(new PPTConnectionShape(slidePart, (ConnectionShape)obj));
  100. }
  101. else if (typeof(AlternateContent).Equals(obj.GetType())) {
  102. ///Equations and formulas as Image 处理公式 方程等
  103. AlternateContent alternateContent = (AlternateContent)obj;
  104. var element = alternateContent.ChildElements.Where(x =>typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
  105. if (element != null) {
  106. AlternateContentFallback alternateContentFallback = (AlternateContentFallback)element;
  107. if (alternateContentFallback.ChildElements.First() is DocumentFormat.OpenXml.Presentation.Shape shapea)
  108. {
  109. pptContainerShape.Elements.Add(new PPTShape(slidePart, shapea, slide));
  110. }
  111. else if (alternateContentFallback.ChildElements.First() is DocumentFormat.OpenXml.Presentation.Picture Picturea)
  112. {
  113. pptContainerShape.Elements.Add(new PPTImage(slidePart, Picturea));
  114. }
  115. }
  116. //MathML
  117. // pptContainerShape.Elements.Add(new PPTAlternateContent(slidePart, (AlternateContent)obj, slide));
  118. /// 处理公式 方程等
  119. }
  120. }
  121. return pptContainerShape;
  122. }
  123. }
  124. }