123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Math;
- using DocumentFormat.OpenXml.Office2010.Drawing;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using HTEXLib.Helpers.ShapeHelpers;
- using HTEXLib.Models;
- using HTEXLib.Models.Inner;
- using HTEXLib.Models.PPTX;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Xsl;
- namespace HTEXLib.Builders
- {
- internal class PPTContainerShapeBuilder
- {
- public PPTContainerShape GetPPTContainerShape(SlideLayoutPart slidePart, PPTSlide slide)
- {
- var pptContainerShape = new PPTContainerShape();
- pptContainerShape.Elements = new List<PPTShapeBase>();
- // var objs = slidePart.SlideLayout.Descendants();
- var ShapeTree = slidePart.SlideLayout.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
- if (ShapeTree == null)
- {
- return null;
- }
- var objs = ShapeTree.ChildElements;
- foreach (object obj in objs)
- {
- PPTShapeBase shape = null;
- if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
- {
- DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
- if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback")
- {
- continue;
- }
- //p:sp
- PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
- if (shapeppt.IsText)
- {
- if (shapeppt.placeholder == null)
- {
- shape = shapeppt;
- }
- }
- }
- else if (typeof(Picture).Equals(obj.GetType()))
- {
- //p:pic
- shape =new PPTImage(slidePart, (Picture)obj, slide);
- }
- else if (typeof(GraphicFrame).Equals(obj.GetType()))
- {
- //p:graphicFrame Chart, Diagram, Table
- var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
- var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
- foreach (var clild in GraphicDataChildren)
- {
- if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
- {
- shape = new PPTTable(slidePart, graphicFrame, Table, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
- {
- shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
- {
- shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
- }
- }
- }
- else if (typeof(GroupShape).Equals(obj.GetType()))
- {
- //p:grpSp
- shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
- }
- else if (typeof(ConnectionShape).Equals(obj.GetType()))
- {
- //p:cxnSp
- shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
- }
- else if (typeof(AlternateContent).Equals(obj.GetType()))
- {
- ///Equations and formulas as Image 处理公式 方程等
- AlternateContent alternateContent = (AlternateContent)obj;
- // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
- var AlternateContentChoice = alternateContent.GetFirstChild<AlternateContentChoice>();
- var shp = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
- if (shp != null)
- {
- var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
- foreach (var pa in Paragraphs)
- {
- // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
- // OfficeMath
- ///公式插入 线性 专用,普通文本区别
- var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
- if (oMath != null)
- {
- var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
- AlternateContentFallback alternateContentFallback = null;
- if (element != null)
- {
- alternateContentFallback = (AlternateContentFallback)element;
- }
- shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
- }
- }
- }
- //MathML
- // shape =new PPTAlternateContent(slidePart, (AlternateContent)obj, slide);
- /// 处理公式 方程等
- }
- if (shape != null)
- {
- bool exist = false;
- foreach (var elm in pptContainerShape.Elements)
- {
- if (elm.suid == shape.suid)
- {
- exist = true;
- }
- }
- if (!exist)
- {
- pptContainerShape.Elements.Add(shape);
- }
- }
- }
- return pptContainerShape;
- }
-
- public PPTContainerShape GetPPTContainerShape(SlidePart slidePart, PPTSlide slide)
- {
- var pptContainerShape = new PPTContainerShape();
- pptContainerShape.Elements = new List<PPTShapeBase>();
- var ShapeTree = slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
- if (ShapeTree == null) {
- return null;
- }
- var objs = ShapeTree.ChildElements;
- foreach (object obj in objs)
- {
- PPTShapeBase shape = null;
- if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
- {
- DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
- if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback") {
- continue;
- }
- //p:sp
- PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
- if (shapeppt.IsText) {
- shape = shapeppt;
- }
-
- }
- else if (typeof(Picture).Equals(obj.GetType()))
- {
- //p:pic
- shape =new PPTImage(slidePart, (Picture)obj,slide);
- }
- else if (typeof(GraphicFrame).Equals(obj.GetType()))
- {
- //p:graphicFrame Chart, Diagram, Table
- var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
- var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
- foreach (var clild in GraphicDataChildren)
- {
- if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
- {
- shape = new PPTTable(slidePart, graphicFrame, Table, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
- {
- shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
- {
- shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
- }
- }
- }
- else if (typeof(GroupShape).Equals(obj.GetType()))
- {
- //p:grpSp
- shape =new PPTGroupShape(slidePart, (GroupShape)obj,slide);
- }
- else if (typeof(ConnectionShape).Equals(obj.GetType()))
- {
- //p:cxnSp
- shape =new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
- }
- else if (typeof(AlternateContent).Equals(obj.GetType())) {
- ///Equations and formulas as Image 处理公式 方程等
- AlternateContent alternateContent = (AlternateContent)obj;
- // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
- var AlternateContentChoice= alternateContent.GetFirstChild<AlternateContentChoice>();
- var shp= AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
- if (shp != null) {
- var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
- foreach (var pa in Paragraphs)
- {
- // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
- // OfficeMath
- ///公式插入 线性 专用,普通文本区别
- var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
- if (oMath != null)
- {
- var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
- AlternateContentFallback alternateContentFallback = null;
- if (element != null)
- {
- alternateContentFallback = (AlternateContentFallback)element;
- }
- shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
- }
- }
- }
-
- }
- if (shape != null) {
- bool exist = false;
- foreach (var elm in pptContainerShape.Elements)
- {
- if (elm.suid == shape.suid)
- {
- exist = true;
- }
- }
- if (!exist)
- {
- pptContainerShape.Elements.Add(shape);
- }
- }
- }
- return pptContainerShape;
- }
- public PPTContainerShape GetPPTContainerShape(SlideMasterPart slidePart, PPTSlide slide)
- {
- var pptContainerShape = new PPTContainerShape();
- pptContainerShape.Elements = new List<PPTShapeBase>();
- var ShapeTree = slidePart.SlideMaster.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>().FirstOrDefault();
- if (ShapeTree == null)
- {
- return null;
- }
- var objs = ShapeTree.ChildElements;
- foreach (object obj in objs)
- {
- PPTShapeBase shape = null;
- if (typeof(DocumentFormat.OpenXml.Presentation.Shape).Equals(obj.GetType()))
- {
- DocumentFormat.OpenXml.Presentation.Shape pshape = (DocumentFormat.OpenXml.Presentation.Shape)obj;
- if (pshape.Parent.LocalName == "Choice" || pshape.Parent.LocalName == "Fallback")
- {
- continue;
- }
- //p:sp
- PPTShape shapeppt = new PPTShape(slidePart, pshape, slide);
- if (shapeppt.IsText)
- {
- shape = shapeppt;
- }
- }
- else if (typeof(Picture).Equals(obj.GetType()))
- {
- //p:pic
- shape = new PPTImage(slidePart, (Picture)obj, slide);
- }
- else if (typeof(GraphicFrame).Equals(obj.GetType()))
- {
- //p:graphicFrame Chart, Diagram, Table
- var graphicFrame = new PPTGraphicFrame(slidePart, (GraphicFrame)obj, slide);
- var GraphicDataChildren = graphicFrame.element.Graphic.GraphicData.ChildElements;
- foreach (var clild in GraphicDataChildren)
- {
- if (clild is DocumentFormat.OpenXml.Drawing.Table Table)
- {
- shape = new PPTTable(slidePart, graphicFrame, Table, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart)
- {
- shape = new PPTChart(slidePart, graphicFrame, Chart, slide);
- }
- if (clild is DocumentFormat.OpenXml.Drawing.Diagrams.RelationshipIds Diagram)
- {
- shape = new PPTDiagram(slidePart, graphicFrame, Diagram, slide);
- }
- }
- }
- else if (typeof(GroupShape).Equals(obj.GetType()))
- {
- //p:grpSp
- shape = new PPTGroupShape(slidePart, (GroupShape)obj, slide);
- }
- else if (typeof(ConnectionShape).Equals(obj.GetType()))
- {
- //p:cxnSp
- shape = new PPTConnectionShape(slidePart, (ConnectionShape)obj, slide);
- }
- else if (typeof(AlternateContent).Equals(obj.GetType()))
- {
- ///Equations and formulas as Image 处理公式 方程等
- AlternateContent alternateContent = (AlternateContent)obj;
- // shape= new PPTAlternateContent(slidePart, (DocumentFormat.OpenXml.AlternateContent)obj, slide);
- var AlternateContentChoice = alternateContent.GetFirstChild<AlternateContentChoice>();
- var shp = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
- if (shp != null)
- {
- var Paragraphs = shp.TextBody.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
- foreach (var pa in Paragraphs)
- {
- // var a = pa.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.TextMath>();
- // OfficeMath
- ///公式插入 线性 专用,普通文本区别
- var oMath = shp.GetPPTXNodeByPath("//p:txBody/a:p/a14:m/m:oMathPara/m:oMath");
- if (oMath != null)
- {
- var element = alternateContent.ChildElements.Where(x => typeof(AlternateContentFallback).Equals(x.GetType())).FirstOrDefault();
- AlternateContentFallback alternateContentFallback = null;
- if (element != null)
- {
- alternateContentFallback = (AlternateContentFallback)element;
- }
- shape = new PPTMath(slidePart, AlternateContentChoice, alternateContentFallback, slide);
- }
- }
- }
- }
- if (shape != null)
- {
- bool exist = false;
- foreach (var elm in pptContainerShape.Elements)
- {
- if (elm.suid == shape.suid)
- {
- exist = true;
- }
- }
- if (!exist)
- {
- pptContainerShape.Elements.Add(shape);
- }
- }
- }
- return pptContainerShape;
- }
- }
- }
|