PPTShapeBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Presentation;
  3. using DocumentFormat.OpenXml;
  4. using System.Collections.Generic;
  5. using System;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Xml.Linq;
  9. namespace HTEXLib.Models.Inner
  10. {
  11. public class PPTShapeBase
  12. {
  13. public string suid { get; set; }
  14. public PPTVisualPPTShapeProp VisualShapeProp { get; set; }
  15. public PPTNonVisualShapeProp NonVisualShapeProp { get; set; }
  16. public bool Invisible { get; set; }
  17. public bool Animatable { get; set; }
  18. public String ClickLinkUrl { get; set; }
  19. public String HoverLinkUrl { get; set; }
  20. public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleMaster { get; set; }
  21. public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleLayout { get; set; }
  22. public DocumentFormat.OpenXml.Drawing.TextAnchoringTypeValues VerticalAlign { get; set; }
  23. public DocumentFormat.OpenXml.Packaging.OpenXmlPart SlidePart { get; set; }
  24. public TableStylesPart tableStylesPart { get; set; }
  25. public int fontScale { get; set; } = 100000;
  26. public double BottomInset { get; set; }
  27. public double TopInset { get; set; }
  28. public double LeftInset { get; set; }
  29. public double RightInset { get; set; }
  30. public XElement Root { get; set; }
  31. public string PartForm { get;set;}
  32. /// <summary>
  33. /// 单页PPT
  34. /// </summary>
  35. public PPTSlide slide { get; set; }
  36. /// <summary>
  37. /// 所有的多媒体Base64
  38. /// </summary>
  39. public IEnumerable<XElement> Media { get; set; }
  40. public SlideMasterPart slideMasterPart { get; set; }
  41. public PPTShapeBase()
  42. {
  43. //Default values for the inserts are part of the inch. One inch is 72pixels
  44. int inchPixel = 72;
  45. BottomInset = inchPixel *1.0 / 20;
  46. TopInset = inchPixel * 1.0 / 20;
  47. LeftInset = inchPixel * 1.0 / 10;
  48. RightInset = inchPixel * 1.0 / 10;
  49. }
  50. protected void SetSlideLayoutVisualShapeProperties(OpenXmlPart openXmlPart, DocumentFormat.OpenXml.Presentation.Shape shape)
  51. {
  52. VisualShapeProp = new PPTVisualPPTShapeProp();
  53. if (openXmlPart is SlidePart slidePart)
  54. {
  55. ShapeTree shapeTree = slidePart.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree;
  56. if (shapeTree != null)
  57. {
  58. // var layoutShape = shapeTree.GetFirstChild<Shape>();
  59. GetShapesPropFromMasterPartLayout(slidePart, shape, 1);
  60. fillPropertiesFromMasterShape(shape, false, false);
  61. }
  62. }
  63. else if (openXmlPart is SlideLayoutPart layoutPart) {
  64. ShapeTree shapeTree = layoutPart.SlideLayout.CommonSlideData.ShapeTree;
  65. if (shapeTree != null)
  66. {
  67. // var layoutShape = shapeTree.GetFirstChild<Shape>();
  68. GetShapesPropFromMasterPartLayout(layoutPart, shape, 1);
  69. fillPropertiesFromMasterShape(shape, false, false);
  70. }
  71. }
  72. }
  73. private void FillFromInheritedShapes(IEnumerable<DocumentFormat.OpenXml.Presentation.Shape> masterShapes, PlaceholderShape placeholderFromShape, bool isLayout)
  74. {
  75. if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue &&
  76. placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
  77. {
  78. foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
  79. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  80. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
  81. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
  82. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
  83. placeholderFromShape.Index.Value &&
  84. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type != null &&
  85. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.HasValue &&
  86. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.Value ==
  87. placeholderFromShape.Type.Value)
  88. {
  89. fillPropertiesFromMasterShape(masterShape, isLayout, true);
  90. return;
  91. }
  92. }
  93. if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue)
  94. {
  95. foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
  96. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  97. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
  98. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
  99. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
  100. placeholderFromShape.Index.Value)
  101. {
  102. fillPropertiesFromMasterShape(masterShape, isLayout, true);
  103. return;
  104. }
  105. }
  106. if (placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
  107. {
  108. foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
  109. {
  110. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  111. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type != null &&
  112. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.HasValue &&
  113. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.Value ==
  114. placeholderFromShape.Type.Value)
  115. {
  116. fillPropertiesFromMasterShape(masterShape, isLayout, true);
  117. return;
  118. }
  119. }
  120. }
  121. }
  122. private void GetShapesPropFromMasterPartLayout(OpenXmlPart openXmlPart, DocumentFormat.OpenXml.Presentation.Shape shape, int slideNumber)
  123. {
  124. if (shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape == null) //If there is no placeholder
  125. return;
  126. if (openXmlPart is SlidePart slidePart)
  127. {
  128. IEnumerator<SlideLayoutPart> slPart = slidePart.SlideLayoutPart.
  129. SlideMasterPart.SlideLayoutParts.GetEnumerator();
  130. var masterShapes = slidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.CommonSlideData.ShapeTree.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
  131. FillFromInheritedShapes(masterShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, false);
  132. var layoutShapes = slidePart.SlideLayoutPart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
  133. FillFromInheritedShapes(layoutShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, true);
  134. }
  135. else if (openXmlPart is SlideLayoutPart layoutPart) {
  136. IEnumerator<SlideLayoutPart> slPart = layoutPart.
  137. SlideMasterPart.SlideLayoutParts.GetEnumerator();
  138. var masterShapes = layoutPart.SlideMasterPart.SlideMaster.CommonSlideData.ShapeTree.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
  139. FillFromInheritedShapes(masterShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, false);
  140. var layoutShapes = layoutPart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
  141. FillFromInheritedShapes(layoutShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, true);
  142. }
  143. }
  144. private void fillPropertiesFromMasterShape(DocumentFormat.OpenXml.Presentation.Shape masterShape, bool isLayout, bool addListStyle)
  145. {
  146. if (null != masterShape.TextBody)
  147. {
  148. if (masterShape.TextBody.ListStyle != null && addListStyle)
  149. {
  150. if (isLayout)
  151. shapeListStyleLayout = masterShape.TextBody.ListStyle;
  152. else
  153. shapeListStyleMaster = masterShape.TextBody.ListStyle;
  154. }
  155. if (masterShape.TextBody.BodyProperties != null && masterShape.TextBody.BodyProperties.Anchor != null)
  156. VerticalAlign = masterShape.TextBody.BodyProperties.Anchor;
  157. if (masterShape.TextBody.BodyProperties.TopInset != null)
  158. {
  159. TopInset = System. Math.Round((double)masterShape.TextBody.BodyProperties.TopInset.Value *1.0/ 12700, Globals.degree);
  160. }
  161. if (masterShape.TextBody.BodyProperties.BottomInset != null)
  162. {
  163. BottomInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.BottomInset.Value * 1.0 / 12700, Globals.degree);
  164. }
  165. if (masterShape.TextBody.BodyProperties.RightInset != null)
  166. {
  167. RightInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.RightInset.Value * 1.0 / 12700,Globals.degree);
  168. }
  169. if (masterShape.TextBody.BodyProperties.LeftInset != null)
  170. {
  171. LeftInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.LeftInset.Value * 1.0 / 12700, Globals.degree);
  172. }
  173. if (masterShape.TextBody.BodyProperties != null &&
  174. masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>() != null &&
  175. masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale != null)
  176. fontScale = masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale.Value;
  177. }
  178. if (masterShape.ShapeProperties.Transform2D != null)
  179. {
  180. VisualShapeProp.Extents = masterShape.ShapeProperties.Transform2D.Extents;
  181. VisualShapeProp.Offset = masterShape.ShapeProperties.Transform2D.Offset;
  182. }
  183. }
  184. }
  185. }