123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 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();
- 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
- shape =new PPTGraphicFrame(slidePart, (GraphicFrame)obj, 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.GetTextByPath("//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 objs = slidePart.Slide.Descendants();
-
- 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
- shape =new PPTGraphicFrame(slidePart, (GraphicFrame)obj, 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.GetTextByPath("//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;
- }
- /// <summary>
- /// 处理xml 中的非ASCII编码的特殊公式字符
- /// </summary>
- /// <param name="element"></param>
- public void DoXml(XElement element) {
- foreach (var elm in element.Elements()) {
- var child = elm.Elements().ToList();
- if (child != null && child.Count>0) {
- DoXml(elm);
- }
- else{
- if (!string.IsNullOrEmpty(elm.Value)) {
- string em = element.Value;
- var keys = CharHelper.MathChar.Keys;
- foreach (string key in keys) {
- em= em.Replace(key, CharHelper.MathChar[key]);
- }
- elm.Value = em;
- elm.ReplaceWith(elm);
- }
- }
- }
- }
- }
- }
|