PPTImage.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Presentation;
  3. using System;
  4. namespace ClearSlideLibrary.Dom
  5. {
  6. public class PPTImage : PPTShapeBase
  7. {
  8. public PPTImage(SlidePart slidePart, Picture picture)
  9. {
  10. GetShapeVisualProperties(slidePart, picture);
  11. GetShapeNonVisualProperties(slidePart, picture);
  12. }
  13. //Image specific properties
  14. public string RelatedImageId { get; set; }
  15. public string FileName { get; set; }
  16. public string TargetName { get; set; }
  17. public string FileExtension { get; set; }
  18. public bool HasEffects { get; set; }
  19. private void GetShapeVisualProperties(SlidePart slidePart, Picture picture)
  20. {
  21. base.VisualShapeProp = new PPTVisualPPTShapeProp();
  22. if (picture.ShapeProperties.Transform2D != null)
  23. {
  24. base.VisualShapeProp.Extents = picture.ShapeProperties.Transform2D.Extents;
  25. base.VisualShapeProp.Offset = picture.ShapeProperties.Transform2D.Offset;
  26. string rId = picture.BlipFill.Blip.Embed.Value;
  27. ImagePart imagePart = (ImagePart)slidePart.GetPartById(rId);
  28. FileExtension = imagePart.Uri.OriginalString.Substring(imagePart.Uri.OriginalString.LastIndexOf(".") + 1).ToLower();
  29. }
  30. else
  31. {
  32. ShapeTree shapeTree =
  33. slidePart.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree;
  34. if (shapeTree != null)
  35. {
  36. var layoutShape = shapeTree.GetFirstChild<Picture>();
  37. if (layoutShape.ShapeProperties.Transform2D != null)
  38. {
  39. VisualShapeProp.Extents = layoutShape.ShapeProperties.Transform2D.Extents;
  40. VisualShapeProp.Offset = layoutShape.ShapeProperties.Transform2D.Offset;
  41. }
  42. }
  43. //base.SetSlideLayoutVisualShapeProperties(slidePart,picture);
  44. }
  45. DocumentFormat.OpenXml.Drawing.EffectList effectList = picture.ShapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.EffectList>();
  46. if (effectList != null)
  47. {
  48. recalculatePropertiesWithEffect(effectList);
  49. }
  50. }
  51. private void recalculatePropertiesWithEffect(DocumentFormat.OpenXml.Drawing.EffectList effectList)
  52. {
  53. if (effectList.Reflection != null)
  54. {
  55. //Reflection doubles the image size - check for other animations!
  56. if (effectList.Reflection.HorizontalRatio != null)
  57. {
  58. VisualShapeProp.Extents.Cx *= 2;
  59. }
  60. if (effectList.Reflection.VerticalRatio != null)
  61. {
  62. VisualShapeProp.Extents.Cy *= 2;
  63. }
  64. }
  65. }
  66. private void GetShapeNonVisualProperties(SlidePart slidePart, Picture shape)
  67. {
  68. if (shape.NonVisualPictureProperties.NonVisualDrawingProperties.HyperlinkOnClick != null)
  69. foreach (HyperlinkRelationship link in slidePart.HyperlinkRelationships)
  70. {
  71. if (link.Id.Equals(shape.NonVisualPictureProperties.NonVisualDrawingProperties.HyperlinkOnClick.Id))
  72. {
  73. ClickLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  74. }
  75. }
  76. if (shape.NonVisualPictureProperties.NonVisualDrawingProperties.HyperlinkOnHover != null)
  77. foreach (HyperlinkRelationship link in slidePart.HyperlinkRelationships)
  78. {
  79. if (link.Id.Equals(shape.NonVisualPictureProperties.NonVisualDrawingProperties.HyperlinkOnHover.Id))
  80. {
  81. HoverLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  82. }
  83. }
  84. var nonVisualShapeProp = new PPTNonVisualShapeProp
  85. {
  86. Id = "s1s" + //HARD CODED: we split it into separate HTML files!
  87. shape.NonVisualPictureProperties.NonVisualDrawingProperties.Id,
  88. Name = shape.LocalName,
  89. Type = "PPTImage"
  90. };
  91. base.NonVisualShapeProp = nonVisualShapeProp;
  92. }
  93. }
  94. }