PPTShapeBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. namespace ClearSlideLibrary.Dom
  9. {
  10. public class PPTShapeBase
  11. {
  12. public PPTVisualPPTShapeProp VisualShapeProp { get; set; }
  13. public PPTNonVisualShapeProp NonVisualShapeProp { get; set; }
  14. public bool Invisible { get; set; }
  15. public bool Animatable { get; set; }
  16. public String ClickLinkUrl { get; set; }
  17. public String HoverLinkUrl { get; set; }
  18. public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleMaster { get; set; }
  19. public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleLayout { get; set; }
  20. public DocumentFormat.OpenXml.Drawing.TextAnchoringTypeValues VerticalAlign { get; set; }
  21. public int fontScale = 100000;
  22. public int BottomInset { get; set; }
  23. public int TopInset { get; set; }
  24. public int LeftInset { get; set; }
  25. public int RightInset { get; set; }
  26. public PPTShapeBase()
  27. {
  28. //Default values for the inserts are part of the inch. One inch is 72pixels
  29. int inchPixel=72;
  30. BottomInset= inchPixel/20;
  31. TopInset=inchPixel/20;
  32. LeftInset=inchPixel/10;
  33. RightInset = inchPixel/10;
  34. }
  35. protected void SetSlideLayoutVisualShapeProperties(SlidePart slidePart, Shape shape)
  36. {
  37. VisualShapeProp = new PPTVisualPPTShapeProp();
  38. ShapeTree shapeTree =
  39. slidePart.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree;
  40. if (shapeTree != null)
  41. {
  42. // var layoutShape = shapeTree.GetFirstChild<Shape>();
  43. GetShapesPropFromMasterPartLayout(slidePart, shape, 1);
  44. fillPropertiesFromMasterShape(shape, false, false);
  45. }
  46. }
  47. private void FillFromInheritedShapes(IEnumerable<Shape> masterShapes, PlaceholderShape placeholderFromShape, bool isLayout){
  48. if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue &&
  49. placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
  50. {
  51. foreach (Shape masterShape in masterShapes)
  52. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  53. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
  54. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
  55. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
  56. placeholderFromShape.Index.Value &&
  57. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type != null &&
  58. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.HasValue &&
  59. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.Value ==
  60. placeholderFromShape.Type.Value)
  61. {
  62. fillPropertiesFromMasterShape(masterShape, isLayout, true);
  63. return;
  64. }
  65. }
  66. if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue)
  67. {
  68. foreach (Shape masterShape in masterShapes)
  69. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  70. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
  71. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
  72. masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
  73. placeholderFromShape.Index.Value)
  74. {
  75. fillPropertiesFromMasterShape(masterShape, isLayout,true);
  76. return;
  77. }
  78. }
  79. if (placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
  80. {
  81. foreach (Shape masterShape in masterShapes)
  82. {
  83. if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
  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. }
  94. }
  95. private void GetShapesPropFromMasterPartLayout(SlidePart slidePart, Shape shape, int slideNumber)
  96. {
  97. if (shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape == null) //If there is no placeholder
  98. return;
  99. IEnumerator<SlideLayoutPart> slPart = slidePart.SlideLayoutPart.
  100. SlideMasterPart.SlideLayoutParts.GetEnumerator();
  101. var masterShapes =slidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.CommonSlideData.ShapeTree.Descendants<Shape>();
  102. FillFromInheritedShapes(masterShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape,false);
  103. var layoutShapes = slidePart.SlideLayoutPart.SlideLayout.Descendants<Shape>();
  104. FillFromInheritedShapes(layoutShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape,true );
  105. }
  106. private void fillPropertiesFromMasterShape(DocumentFormat.OpenXml.Presentation.Shape masterShape, bool isLayout, bool addListStyle)
  107. {
  108. if (null != masterShape.TextBody)
  109. {
  110. if (masterShape.TextBody.ListStyle != null && addListStyle)
  111. {
  112. if(isLayout)
  113. shapeListStyleLayout = masterShape.TextBody.ListStyle;
  114. else
  115. shapeListStyleMaster = masterShape.TextBody.ListStyle;
  116. }
  117. if (masterShape.TextBody.BodyProperties != null && masterShape.TextBody.BodyProperties.Anchor!=null)
  118. VerticalAlign = masterShape.TextBody.BodyProperties.Anchor;
  119. if(masterShape.TextBody.BodyProperties.TopInset != null){
  120. TopInset = (int)Math.Round((double)masterShape.TextBody.BodyProperties.TopInset.Value / 12700);
  121. }
  122. if(masterShape.TextBody.BodyProperties.BottomInset != null){
  123. BottomInset=(int)Math.Round((double)masterShape.TextBody.BodyProperties.BottomInset.Value / 12700);
  124. }
  125. if(masterShape.TextBody.BodyProperties.RightInset != null){
  126. RightInset = (int)Math.Round((double)masterShape.TextBody.BodyProperties.RightInset.Value / 12700);
  127. }
  128. if(masterShape.TextBody.BodyProperties.LeftInset != null){
  129. LeftInset = (int)Math.Round((double)masterShape.TextBody.BodyProperties.LeftInset.Value / 12700);
  130. }
  131. if (masterShape.TextBody.BodyProperties != null &&
  132. masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>() != null &&
  133. masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale != null)
  134. fontScale = masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale.Value;
  135. }
  136. if (masterShape.ShapeProperties.Transform2D != null)
  137. {
  138. VisualShapeProp.Extents = masterShape.ShapeProperties.Transform2D.Extents;
  139. VisualShapeProp.Offset = masterShape.ShapeProperties.Transform2D.Offset;
  140. }
  141. }
  142. }
  143. }