123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using DocumentFormat.OpenXml;
- using System.Collections.Generic;
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Xml.Linq;
- namespace HTEXLib.Models.Inner
- {
- public class PPTShapeBase
- {
- public string suid { get; set; }
- public PPTVisualPPTShapeProp VisualShapeProp { get; set; }
- public PPTNonVisualShapeProp NonVisualShapeProp { get; set; }
- public bool Invisible { get; set; }
- public bool Animatable { get; set; }
- public String ClickLinkUrl { get; set; }
- public String HoverLinkUrl { get; set; }
- public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleMaster { get; set; }
- public DocumentFormat.OpenXml.Drawing.ListStyle shapeListStyleLayout { get; set; }
- public DocumentFormat.OpenXml.Drawing.TextAnchoringTypeValues VerticalAlign { get; set; }
- public DocumentFormat.OpenXml.Packaging.OpenXmlPart SlidePart { get; set; }
- public TableStylesPart tableStylesPart { get; set; }
- public int fontScale { get; set; } = 100000;
- public double BottomInset { get; set; }
- public double TopInset { get; set; }
- public double LeftInset { get; set; }
- public double RightInset { get; set; }
- public XElement Root { get; set; }
- public string PartForm { get;set;}
- /// <summary>
- /// 单页PPT
- /// </summary>
- public PPTSlide slide { get; set; }
-
- /// <summary>
- /// 所有的多媒体Base64
- /// </summary>
- public IEnumerable<XElement> Media { get; set; }
- public SlideMasterPart slideMasterPart { get; set; }
- public PPTShapeBase()
- {
- //Default values for the inserts are part of the inch. One inch is 72pixels
- int inchPixel = 72;
- BottomInset = inchPixel *1.0 / 20;
- TopInset = inchPixel * 1.0 / 20;
- LeftInset = inchPixel * 1.0 / 10;
- RightInset = inchPixel * 1.0 / 10;
- }
- protected void SetSlideLayoutVisualShapeProperties(OpenXmlPart openXmlPart, DocumentFormat.OpenXml.Presentation.Shape shape)
- {
- VisualShapeProp = new PPTVisualPPTShapeProp();
- if (openXmlPart is SlidePart slidePart)
- {
- ShapeTree shapeTree = slidePart.SlideLayoutPart.SlideLayout.CommonSlideData.ShapeTree;
- if (shapeTree != null)
- {
- // var layoutShape = shapeTree.GetFirstChild<Shape>();
- GetShapesPropFromMasterPartLayout(slidePart, shape, 1);
- fillPropertiesFromMasterShape(shape, false, false);
- }
- }
- else if (openXmlPart is SlideLayoutPart layoutPart) {
- ShapeTree shapeTree = layoutPart.SlideLayout.CommonSlideData.ShapeTree;
- if (shapeTree != null)
- {
- // var layoutShape = shapeTree.GetFirstChild<Shape>();
- GetShapesPropFromMasterPartLayout(layoutPart, shape, 1);
- fillPropertiesFromMasterShape(shape, false, false);
- }
- }
-
- }
- private void FillFromInheritedShapes(IEnumerable<DocumentFormat.OpenXml.Presentation.Shape> masterShapes, PlaceholderShape placeholderFromShape, bool isLayout)
- {
- if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue &&
- placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
- {
- foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
- if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
- placeholderFromShape.Index.Value &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.HasValue &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.Value ==
- placeholderFromShape.Type.Value)
- {
- fillPropertiesFromMasterShape(masterShape, isLayout, true);
- return;
- }
- }
- if (placeholderFromShape.Index != null && placeholderFromShape.Index.HasValue)
- {
- foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
- if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.HasValue &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Index.Value ==
- placeholderFromShape.Index.Value)
- {
- fillPropertiesFromMasterShape(masterShape, isLayout, true);
- return;
- }
- }
- if (placeholderFromShape.Type != null && placeholderFromShape.Type.HasValue)
- {
- foreach (DocumentFormat.OpenXml.Presentation.Shape masterShape in masterShapes)
- {
- if (masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type != null &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.HasValue &&
- masterShape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape.Type.Value ==
- placeholderFromShape.Type.Value)
- {
- fillPropertiesFromMasterShape(masterShape, isLayout, true);
- return;
- }
- }
- }
- }
- private void GetShapesPropFromMasterPartLayout(OpenXmlPart openXmlPart, DocumentFormat.OpenXml.Presentation.Shape shape, int slideNumber)
- {
- if (shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape == null) //If there is no placeholder
- return;
- if (openXmlPart is SlidePart slidePart)
- {
- IEnumerator<SlideLayoutPart> slPart = slidePart.SlideLayoutPart.
- SlideMasterPart.SlideLayoutParts.GetEnumerator();
- var masterShapes = slidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.CommonSlideData.ShapeTree.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
- FillFromInheritedShapes(masterShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, false);
- var layoutShapes = slidePart.SlideLayoutPart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
- FillFromInheritedShapes(layoutShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, true);
- }
- else if (openXmlPart is SlideLayoutPart layoutPart) {
- IEnumerator<SlideLayoutPart> slPart = layoutPart.
- SlideMasterPart.SlideLayoutParts.GetEnumerator();
- var masterShapes = layoutPart.SlideMasterPart.SlideMaster.CommonSlideData.ShapeTree.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
- FillFromInheritedShapes(masterShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, false);
- var layoutShapes = layoutPart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.Shape>();
- FillFromInheritedShapes(layoutShapes, shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape, true);
- }
- }
- private void fillPropertiesFromMasterShape(DocumentFormat.OpenXml.Presentation.Shape masterShape, bool isLayout, bool addListStyle)
- {
- if (null != masterShape.TextBody)
- {
- if (masterShape.TextBody.ListStyle != null && addListStyle)
- {
- if (isLayout)
- shapeListStyleLayout = masterShape.TextBody.ListStyle;
- else
- shapeListStyleMaster = masterShape.TextBody.ListStyle;
- }
- if (masterShape.TextBody.BodyProperties != null && masterShape.TextBody.BodyProperties.Anchor != null)
- VerticalAlign = masterShape.TextBody.BodyProperties.Anchor;
- if (masterShape.TextBody.BodyProperties.TopInset != null)
- {
- TopInset = System. Math.Round((double)masterShape.TextBody.BodyProperties.TopInset.Value *1.0/ 12700, Globals.degree);
- }
- if (masterShape.TextBody.BodyProperties.BottomInset != null)
- {
- BottomInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.BottomInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (masterShape.TextBody.BodyProperties.RightInset != null)
- {
- RightInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.RightInset.Value * 1.0 / 12700,Globals.degree);
- }
- if (masterShape.TextBody.BodyProperties.LeftInset != null)
- {
- LeftInset = System.Math.Round((double)masterShape.TextBody.BodyProperties.LeftInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (masterShape.TextBody.BodyProperties != null &&
- masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>() != null &&
- masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale != null)
- fontScale = masterShape.TextBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale.Value;
- }
- if (masterShape.ShapeProperties.Transform2D != null)
- {
- VisualShapeProp.Extents = masterShape.ShapeProperties.Transform2D.Extents;
- VisualShapeProp.Offset = masterShape.ShapeProperties.Transform2D.Offset;
- }
- }
- }
- }
|