12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Office2010.Drawing;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Xsl;
- using TEAMModelOS.SDK.Context.Configuration;
- using TEAMModelOS.SDK.Helper.Common.ColorHelper;
- using TEAMModelOS.SDK.Module.PowerPointX.Model;
- using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
- using Point = TEAMModelOS.SDK.Module.PowerPointX.Model.Point;
- using Theme = DocumentFormat.OpenXml.Drawing.Theme;
- namespace TEAMModelOS.SDK.Module.PowerPointX
- {
- public class PresentationConvert
- {
- const double inchpixel = 96.00, inchpt = 72.00, pxBase = 914400.00, ptBase = 12700;
- const double rotBase = 60000.00;
- /// <summary>
- /// 加载PPTX文件
- /// </summary>
- /// <param name="presentationFile"></param>
- /// <returns></returns>
- public PPTXInfo LoadPresentation(Stream presentationStream)
- {
- using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationStream, false))
- {
- if (presentationDocument == null)
- {
- throw new ArgumentNullException("presentationDocument");
- }
- // Get a PresentationPart object from the PresentationDocument object.
-
- var thumbnailPart = presentationDocument.ThumbnailPart;
- var contentType = thumbnailPart.ContentType;
- string base64 = "";
- using (var stream = thumbnailPart.GetStream())
- {
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- stream.Close();
- base64 = System.Convert.ToBase64String(buffer);
- base64 = "data:" + contentType + ";base64," + base64;
- }
-
-
- PresentationPart presentationPart = presentationDocument.PresentationPart;
-
-
- if (presentationPart != null && presentationPart.Presentation != null)
- {
- // Get a Presentation object from the PresentationPart object.
- Presentation presentation = presentationPart.Presentation;
- int pageSize = 0;
- var slideMasterParts = presentationPart.SlideMasterParts;
- ColorMap colorMap = null;
- Theme theme = null;
- foreach (var slideMasterPart in slideMasterParts)
- {
- if (colorMap != null && theme != null)
- {
- break;
- }
- colorMap = slideMasterPart.SlideMaster.ColorMap;
- theme = slideMasterPart.ThemePart.Theme;
- }
-
- //获取PPT 一页大小
- double x = presentation.SlideSize.Cx* inchpixel/ pxBase;
- double y = presentation.SlideSize.Cy * inchpixel / pxBase;
- List<PPTXSlide> slides = new List<PPTXSlide>();
- if (presentation.SlideIdList != null)
- {
- // Get the title of each slide in the slide order.
- // 获取的是几页PPT数量
- foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
- {
- PPTXSlide slide = new PPTXSlide();
- // 获取这一页 PPT 的id
- string id = slideId.RelationshipId;
- SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
- //获取当前页 PPT 的所有元素
- slide.Elements=GetSlideElement(slidePart, theme, colorMap);
- slides.Add(slide);
- pageSize++;
- }
- }
- PPTXInfo info = new PPTXInfo {Slides=slides,Width=x,Height=y ,PageSize= pageSize ,Thumbnail=base64 };
- return info;
- }
- return null;
- }
- }
- /// <summary>
- /// 处理一页PPT的元素
- /// </summary>
- /// <param name="slidePart"></param>
- /// <param name="theme"></param>
- /// <param name="colorMap"></param>
- /// <returns></returns>
- public List<PPTXElement> GetSlideElement(SlidePart slidePart, Theme theme, ColorMap colorMap)
- {
- List<PPTXElement> elements = new List<PPTXElement>();
- var shapeTrees = from shap in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>() select shap;
- if (shapeTrees.Count() > 0 && shapeTrees.First().ChildElements.Count > 0)
- {
- OpenXmlElementList openXmlElements = shapeTrees.First().ChildElements;
- int index = 0;
- foreach (OpenXmlElement element in openXmlElements)
- {
- PPTXElement pptElement = null;
- if (element is DocumentFormat.OpenXml.Presentation.Shape shape)
- { //p:sp
- pptElement = ShapeConvert(shape, theme, colorMap, index);
- }
- if (element is DocumentFormat.OpenXml.Presentation.Picture picture)//p:pic
- {
- pptElement = PictureConvert(picture, theme, colorMap, slidePart, index);
- }
- if (element is DocumentFormat.OpenXml.AlternateContent content)//mc:alternatecontent
- {
- pptElement = AlternateContentConvert(content, theme, colorMap,slidePart, index);
- }
- if (element is DocumentFormat.OpenXml.Presentation.GraphicFrame graphicFrame)//p:graphicFrame
- {
- pptElement = GraphicFrameConvert(graphicFrame, theme, colorMap, index);
- }
- if (element is DocumentFormat.OpenXml.Presentation.GroupShape groupShape)//p:grpSp
- {
- pptElement = GroupShapeConvert(groupShape, theme, colorMap, index);
- }
- if (element is DocumentFormat.OpenXml.Presentation.ConnectionShape connectionShape) // p:cxnSp
- {
- pptElement = ConnectionShapeConvert(connectionShape, theme, colorMap, index);
- }
- if (pptElement != null)
- {
- index++;
- elements.Add(pptElement);
- }
- }
- }
- return elements;
- }
- public PPTXElement GroupShapeConvert(GroupShape groupShape, Theme theme, ColorMap colorMap, int index)
- {
- return null;
- }
- public PPTXElement ConnectionShapeConvert(ConnectionShape connectionShape, Theme theme, ColorMap colorMap, int index)
- {
- string type = "";
- string fillColor = "";
- ShapeProperties properties = connectionShape.ShapeProperties;
- IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
- if (presetGeometries.Count() > 0)
- {
- type = presetGeometries.FirstOrDefault().Preset;
- }
- PPTXConnector element = new PPTXConnector() { Type = type };
- if (properties != null)
- {
- if (properties.Transform2D != null)
- {
- element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
- element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
- element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
- element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
- element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
- element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
- element.Rot = (properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0) / rotBase;
- }
- var headEnd = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.HeadEnd>() select shap;
- var tailEnd = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.TailEnd>() select shap;
- if (headEnd != null && headEnd.Count() > 0)
- {
- element.HeadEnd = headEnd.First().Type;
- }
- if (tailEnd != null && tailEnd.Count() > 0)
- {
- element.TailEnd = tailEnd.First().Type;
- }
- fillColor = GetPropertieColor(properties, theme, colorMap, "Line");
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetShapeStyleColor(connectionShape.ShapeStyle, "Line", theme);
- }
- element.FillColor = fillColor;
- return element;
- }
- public PPTXElement GraphicFrameConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap, int index)
- {
- DocumentFormat.OpenXml.Drawing.GraphicData graphicData = graphicFrame.ChildElements.First<DocumentFormat.OpenXml.Drawing.GraphicData>();
- if (graphicData != null)
- {
- OpenXmlElement element = graphicData.ChildElements.First();
- if (element != null)
- {
- if (element is DocumentFormat.OpenXml.Drawing.Table table)
- {
- return TableConvert(graphicFrame, theme, colorMap);
- }
- if (element is DocumentFormat.OpenXml.Drawing.Chart chart)
- {
- //return ChartConvert(graphicFrame, theme, colorMap);
- }
- if (element is DocumentFormat.OpenXml.Drawing.Diagram diagram)
- {
- //return DiagramConvert(graphicFrame, theme, colorMap);
- }
- }
- }
- return null;
- }
- public PPTXElement TableConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap)
- {
- return null;
- }
- /// <summary>
- /// 处理普通形状的元素
- /// </summary>
- /// <param name="shape"></param>
- /// <returns></returns>
- public PPTXElement ShapeConvert(Shape shape, Theme theme, ColorMap colorMap, int index)
- {
- // shape.base
- //文字颜色
- string textColor = "";
- //填充颜色
- string fillColor = "";
- //填充颜色
- string lineColor = "";
- // 形状类型
- string type = "rect";
- //字体大小
- int fontSize = 1800;
- //是否闭合形状
- bool complete = true;
- PPTXContainer element = new PPTXContainer() ;
- ShapeProperties properties = shape.ShapeProperties;
- IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
- IEnumerable<DocumentFormat.OpenXml.Drawing.CustomGeometry> customGeometries = GetCustomGeometry(properties);
- if (presetGeometries.Count() > 0)
- {
- type = presetGeometries.FirstOrDefault().Preset;
- var adjusts = from shap in presetGeometries.First().Descendants<DocumentFormat.OpenXml.Drawing.AdjustValueList>() select shap;
- if (adjusts.Count() > 0)
- {
- var elements = adjusts.First().ChildElements;
- foreach (var xmlElement in elements)
- {
- if (xmlElement is DocumentFormat.OpenXml.Drawing.ShapeGuide shapeGuide)
- {
- PPTXShapeGuide PPTXShapeGuide = new PPTXShapeGuide { Name = shapeGuide.Name, Fmla = shapeGuide.Formula };
- element.ShapeGuides.Add(PPTXShapeGuide);
- }
- }
- }
- }
- if (customGeometries.Count() > 0)
- {
- type = "custom";
- }
- element.Type = type;
- DocumentFormat.OpenXml.Drawing.Transform2D transform2D = GetTransform2D(properties);
- if (transform2D != null)
- {
- element.Close = complete;
- element.OffX = transform2D.Offset.X * inchpixel / pxBase;
- element.OffY = transform2D.Offset.Y * inchpixel / pxBase;
- element.ExtX = transform2D.Extents.Cx * inchpixel / pxBase;
- element.ExtY = transform2D.Extents.Cy * inchpixel / pxBase;
- element.FlipH = transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(transform2D.HorizontalFlip) : false;
- element.FlipV = transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(transform2D.VerticalFlip) : false;
- element.Rot = transform2D.Rotation != null ? transform2D.Rotation.Value : 0;
- }
- var nofill = GetNoFill(properties);
- if (nofill.Count() > 0)
- {
- element.Fill = false;
- }
- element.Index = index;
- ///获取runs的文本及颜色信息
- var bodyProp = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.BodyProperties>() select shap;
- if (bodyProp.Count() > 0)
- {
- DocumentFormat.OpenXml.Drawing.BodyProperties bdpros = bodyProp.First<DocumentFormat.OpenXml.Drawing.BodyProperties>();
- if (bdpros.Vertical != null && !bdpros.Vertical.Value.Equals("horz"))
- {
- element.TextLayout = "vert";
- }
- //0顶 1中 2底
- if (bdpros.Anchor != null)
- {
- element.Align = bdpros.Anchor.Value.ToString();
- }
- //bodyAlign = element.Align == null || string.IsNullOrEmpty(bdpros.Ancestors) ? "" : element.Align;
- //element.Wrap == null || string.IsNullOrEmpty(element.Wrap) ? "none" : element.Wrap;
- }
- var paragraphs = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>() select shap;
- foreach (var paragrap in paragraphs)
- {
- string textAling = "l";
- if (paragrap.ParagraphProperties != null)
- {
- textAling = paragrap.ParagraphProperties.Alignment;
- }
- //if (string.IsNullOrEmpty(textAling))
- //{
- //if (string.IsNullOrEmpty(bodyAlign))
- //{
- // textAling = "l";
- //}
- //else
- //{
- // textAling = bodyAlign;
- //}
- //}
- List<PPTXText> texts = new List<PPTXText>();
- var runs = from shap in paragrap.Descendants<DocumentFormat.OpenXml.Drawing.Run>() select shap;
- foreach (var run in runs)
- {
- PPTXText text = new PPTXText();
- var runprps = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
- DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
- if (runProperties != null)
- {
- if (runProperties.FontSize != null)
- {
- text.FontSize = runProperties.FontSize;
- }
- if (runProperties.Italic != null)
- {
- text.FontItalic = runProperties.Italic;
- }
- if (runProperties.Bold != null)
- {
- text.FontBold = runProperties.Bold;
- }
- //if (runProperties.Italic != null)
- //{
- // text.FontBold = runProperties.Bold;
- //}
- if (runProperties.Underline != null)
- {
- text.FontLine = (int)runProperties.Underline.Value;
- }
- }
- var schemeColor = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
- var rgbColors = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
- textColor = GetSchemeColor(schemeColor, theme, colorMap);
- if (string.IsNullOrEmpty(textColor))
- {
- text.TextColor = textColor = GetRgbColor(rgbColors);
- }
- if (string.IsNullOrEmpty(textColor))
- {
- textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
- }
- if (string.IsNullOrEmpty(textColor))
- {
- textColor = "FFFFFF";
- }
- text.FontSize = fontSize;
- text.Text = run.InnerText;
- text.TextAlign = textAling;
- texts.Add(text);
- }
- if (texts.Count() > 0)
- {
- PPTXParagraph paragraph = new PPTXParagraph();
- paragraph.Texts = texts;
- element.Paragraphs.Add(paragraph);
- }
- }
- if (string.IsNullOrEmpty(textColor))
- {
- textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
- }
- if (string.IsNullOrEmpty(lineColor))
- {
- lineColor = GetPropertieColor(properties, theme, colorMap, "Line");
- }
- if (string.IsNullOrEmpty(lineColor))
- {
- lineColor = GetShapeStyleColor(shape.ShapeStyle, "Line", theme);
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
- }
- element.LineColor = lineColor;
- element.FillColor = fillColor;
- var custGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
- if (custGeometrys.Count() > 0)
- {
- var pathLists = from shap in custGeometrys.First().Descendants<DocumentFormat.OpenXml.Drawing.Path>() select shap;
- if (pathLists.Count() > 0)
- {
- element.Close = false;
- OpenXmlElementList elements = pathLists.First().ChildElements;
- foreach (OpenXmlElement xmlElement in elements)
- {
- //起点
- if (xmlElement is DocumentFormat.OpenXml.Drawing.MoveTo moveTo)
- {
- DocumentFormat.OpenXml.Drawing.Point point = moveTo.Point;
- PPTXMoveToPath moveToPath = new PPTXMoveToPath { Type = "MoveTo" };
- moveToPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
- element.Paths.Add(moveToPath);
- }
- //连线
- if (xmlElement is DocumentFormat.OpenXml.Drawing.LineTo linTo)
- {
- DocumentFormat.OpenXml.Drawing.Point point = linTo.Point;
- PPTXLineToPath lineToPath = new PPTXLineToPath { Type = "LineTo" };
- lineToPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
- element.Paths.Add(lineToPath);
- }
- //三次贝塞尔曲线
- if (xmlElement is DocumentFormat.OpenXml.Drawing.CubicBezierCurveTo cubicBezierCurveTo)
- {
- OpenXmlElementList list = cubicBezierCurveTo.ChildElements;
- PPTXCubicBezPath cubicBezPath = new PPTXCubicBezPath { Type = "CubicBez" };
- foreach (var ls in list)
- {
- if (ls is DocumentFormat.OpenXml.Drawing.Point point)
- {
- cubicBezPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
- }
- }
- element.Paths.Add(cubicBezPath);
- }
- //二次贝塞尔曲线
- if (xmlElement is DocumentFormat.OpenXml.Drawing.QuadraticBezierCurveTo quadraticBezierCurveTo)
- {
- OpenXmlElementList list = quadraticBezierCurveTo.ChildElements;
- PPTXQuadBezPath quadBezPath = new PPTXQuadBezPath { Type = "QuadBez" };
- foreach (var ls in list)
- {
- if (ls is DocumentFormat.OpenXml.Drawing.Point point)
- {
- quadBezPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
- }
- }
- element.Paths.Add(quadBezPath);
- }
- //处理曲线
- if (xmlElement is DocumentFormat.OpenXml.Drawing.ArcTo arcTO)
- {
- PPTXArcToPath arcToPath = new PPTXArcToPath() { WidthRadius = arcTO.WidthRadius, HeightRadius = arcTO.HeightRadius, StartAngle = arcTO.StartAngle, SwingAngle = arcTO.SwingAngle };
- element.Paths.Add(arcToPath);
- }
- ///判断路径是否闭合
- if (xmlElement is DocumentFormat.OpenXml.Drawing.CloseShapePath close)
- {
- if (close != null)
- {
- element.Close = true;
- }
- }
- }
- }
- }
- return element;
- }
- /// <summary>
- /// 处理图片的元素
- /// </summary>
- /// <param name="picture"></param>
- /// <returns></returns>
- public PPTXElement PictureConvert(Picture picture, Theme theme, ColorMap colorMap, SlidePart slidePart, int index)
- {
- BlipFill blipFill = picture.BlipFill;
- var imageRid = blipFill.Blip.Embed.Value;
- IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
- ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
- var contentType = imagePart.ContentType;
- string base64 = "";
- using (var stream = imagePart.GetStream())
- {
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- stream.Close();
- base64 = System.Convert.ToBase64String(buffer);
- base64 = "data:" + contentType + ";base64," + base64;
- }
- ShapeProperties properties = picture.ShapeProperties;
- PPTXPicture element = new PPTXPicture();
- if (properties != null && properties.Transform2D != null)
- {
- element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
- element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
- element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
- element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
- element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
- element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
- element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
- }
- element.FillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
- element.LineColor = GetPropertieColor(properties, theme, colorMap, "Line");
- element.Data = base64;
- element.Index = index;
- return element;
- }
- /// <summary>
- /// 处理兼容元素
- /// </summary>
- /// <param name="picture"></param>
- /// <returns></returns>
- public PPTXElement AlternateContentConvert(AlternateContent content, Theme theme, ColorMap colorMap, SlidePart slidePart, int index)
- {
- var choices = from contentChoice in content.Descendants<AlternateContentChoice>() select contentChoice;
- if (choices.Count() > 0) {
- OpenXmlElement element = choices.First().ChildElements.First();
- if (element != null)
- {
- if (element is DocumentFormat.OpenXml.Presentation.Shape shape)
- {
- var textMaths = from shap in shape.Descendants<TextMath>() select shap;
- if (textMaths.Count() > 0)
- {
- // return MathConvertMathML(shape, theme, colorMap, index);
- return MathConvertImage(content, theme, colorMap,slidePart, index);
- }
- }
- }
- }
- //AlternateContentChoice choice = content.ChildElements.First<AlternateContentChoice>();
-
- return null;
- }
- public PPTXElement MathConvertImage(AlternateContent content, Theme theme, ColorMap colorMap, SlidePart slidePart, int index) {
- var fallbacks = from contentChoice in content.Descendants<AlternateContentFallback>() select contentChoice;
- if (fallbacks.Count() > 0)
- {
- OpenXmlElement xmlElement = fallbacks.First().ChildElements.First();
- if (xmlElement != null)
- {
- if (xmlElement is DocumentFormat.OpenXml.Presentation.Shape shape)
- {
- var blipFills = from pic in shape.Descendants<DocumentFormat.OpenXml.Drawing.BlipFill>() select pic;
- if (blipFills.Count() > 0) {
- var imageRid= blipFills.First().Blip.Embed.Value;
- IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
- ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
- var contentType = imagePart.ContentType;
- string base64 = "";
- using (var stream = imagePart.GetStream())
- {
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- stream.Close();
- base64 = System.Convert.ToBase64String(buffer);
- base64 = "data:" + contentType + ";base64," + base64;
- }
- ShapeProperties properties = shape.ShapeProperties;
- PPTXPicture element = new PPTXPicture();
- if (properties != null && properties.Transform2D != null)
- {
- element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
- element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
- element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
- element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
- element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
- element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
- element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
- }
- element.FillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
- element.LineColor = GetPropertieColor(properties, theme, colorMap, "Line");
- element.Data = base64;
- element.Index = index;
- return element;
- }
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 处理公式
- /// </summary>
- /// <param name="shape"></param>
- /// <param name="theme"></param>
- /// <param name="colorMap"></param>
- /// <returns></returns>
- public PPTXElement MathConvertMathML(DocumentFormat.OpenXml.Presentation.Shape shape, Theme theme, ColorMap colorMap, int index)
- {
- //文字颜色
- string textColor = "";
- //填充颜色
- string fillColor = "";
- // 形状类型
- string type = "math";
- //字体大小
- int fontSize = 1800;
- //是否闭合形状
- bool complete = true;
- ShapeProperties properties = shape.ShapeProperties;
- //IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
- //if (presetGeometries.Count() > 0)
- //{
- // type = presetGeometries.FirstOrDefault().Preset;
- //}
- DocumentFormat.OpenXml.Drawing.Transform2D transform2D = GetTransform2D(properties);
- PPTXContainer element = new PPTXContainer() { Type = type };
- if (transform2D != null)
- {
- element.Close = complete;
- element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
- element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
- element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
- element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
- element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
- element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
- element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
- }
- textColor = GetPropertieColor(properties, theme, colorMap, "Font");
- var nofill = GetNoFill(properties);
- if (nofill.Count() > 0)
- {
- element.Fill = false;
- }
- //获取行内样式
- if (string.IsNullOrEmpty(textColor))
- {
- var rgbColor = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
- textColor = GetRgbColor(rgbColor);
- if (string.IsNullOrEmpty(textColor))
- {
- var schemes = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
- textColor = GetSchemeColor(schemes, theme, colorMap);
- }
- }
- if (string.IsNullOrEmpty(textColor))
- {
- textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
- }
- {
- textColor = "FFFFFF";
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
- }
- string lineColor = GetPropertieColor(properties, theme, colorMap, "Line");
- if (string.IsNullOrEmpty(lineColor))
- {
- lineColor = GetShapeStyleColor(shape.ShapeStyle, "Line", theme);
- }
- element.LineColor = lineColor;
- element.FillColor = fillColor;
- var textMaths = from shap in shape.Descendants<TextMath>() select shap;
- var math = textMaths.First<TextMath>();
- /// TODO 文字大小
- //XElement xElement= XElement.Load(math.InnerXml);
- // var aa = xElement.Element("a:rPr");
- //var runProperties = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
- //if (runProperties.Count() > 0) {
- // DocumentFormat.OpenXml.Drawing.RunProperties propert= runProperties.First<DocumentFormat.OpenXml.Drawing.RunProperties>();
- // fontSize = propert.FontSize!=null&& propert.FontSize.Value > 0 ? propert.FontSize.Value : fontSize;
- //}
- string elementData = ProcessOMath(math.InnerXml);
- PPTXParagraph paragraph = new PPTXParagraph();
- paragraph.Texts.Add(new PPTXText { TextColor = textColor, Text = elementData, FontSize = fontSize });
- element.Paragraphs.Add(paragraph);
- element.Index = index;
- return element;
- }
- /// <summary>
- /// 处理元素的Transform2D属性
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public DocumentFormat.OpenXml.Drawing.Transform2D GetTransform2D(ShapeProperties properties)
- {
- if (properties != null)
- {
- return properties.Transform2D;
- }
- return null;
- }
- /// <summary>
- /// 处理元素的Transform2D 系统默认的元素
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> GetPresetGeometry(ShapeProperties properties)
- {
- if (properties != null)
- {
- var presetGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>() select shap;
- return presetGeometrys;
- }
- return null;
- }
- public IEnumerable<DocumentFormat.OpenXml.Drawing.CustomGeometry> GetCustomGeometry(ShapeProperties properties)
- {
- if (properties != null)
- {
- var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
- return customGeometrys;
- }
- return null;
- }
- /// <summary>
- /// 处理是否填充
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public IEnumerable<DocumentFormat.OpenXml.Drawing.NoFill> GetNoFill(ShapeProperties properties)
- {
- if (properties != null)
- {
- var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.NoFill>() select shap;
- return customGeometrys;
- }
- return null;
- }
- ///// <summary>
- ///// 获取属性填充色
- ///// </summary>
- ///// <param name="properties"></param>
- ///// <returns></returns>
- //public IEnumerable<DocumentFormat.OpenXml.Drawing.SolidFill> GetPropFill(ShapeProperties properties)
- //{
- // if (properties != null)
- // {
- // var solidFills = properties.ChildElements
- // .Where(x => x is DocumentFormat.OpenXml.Drawing.SolidFill);
- // if (solidFills.Count() > 0) {
- // DocumentFormat.OpenXml.Drawing.SolidFill solidFills.ToList().First();
- // }
- // }
- // return null;
- //}
- /// <summary>
- /// 获取轮廓颜色填充
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public IEnumerable<DocumentFormat.OpenXml.Drawing.NoFill> GetPropLineFill(ShapeProperties properties)
- {
- if (properties != null)
- {
- var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.NoFill>() select shap;
- return customGeometrys;
- }
- return null;
- }
- /// <summary>
- /// 处理元素属性的填充色
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public string GetPropertieColor(ShapeProperties properties, Theme theme, ColorMap colorMap, string type)
- {
- if (properties != null)
- {
- var cidElement = properties.ChildElements;
- foreach (var element in cidElement)
- {
- if (type.Equals("Fill") && element is DocumentFormat.OpenXml.Drawing.SolidFill solidFill)
- {
- string fillColor = "";
- var propRgbColor = from shape in solidFill.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- fillColor = GetRgbColor(propRgbColor);
- if (string.IsNullOrEmpty(fillColor))
- {
- var colorScheme = from shape in solidFill.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
- fillColor = GetSchemeColor(colorScheme, theme, colorMap);
- }
- return fillColor;
- }
- if (type.Equals("Line") && element is DocumentFormat.OpenXml.Drawing.Outline outline)
- {
- string lineColor = "";
- var propRgbColor = from shape in outline.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- lineColor = GetRgbColor(propRgbColor);
- if (string.IsNullOrEmpty(lineColor))
- {
- var colorScheme = from shape in outline.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
- lineColor = GetSchemeColor(colorScheme, theme, colorMap);
- }
- return lineColor;
- }
- }
- }
- return "";
- }
- /// <summary>
- /// 获取ShapeStyle样式表 各种属性 的颜色
- /// </summary>
- /// <param name="shapeStyle"></param>
- /// <param name="colorType"></param>
- /// <param name="theme"></param>
- /// <returns></returns>
- public string GetShapeStyleColor(DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle, string colorType, Theme theme)
- {
- if (shapeStyle == null)
- {
- return "";
- }
- // 效果颜色
- if (colorType.Equals("Effect"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor EffectcolorScheme = shapeStyle.EffectReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- //return ColorForThemeClr(EffectcolorScheme.Val, theme);
- return GetColorSchemeExtInfo(EffectcolorScheme, ColorForThemeClr(EffectcolorScheme.Val, theme));
- }
- // 轮廓颜色
- if (colorType.Equals("Line"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- //return ColorForThemeClr(LinecolorScheme.Val, theme);
- return GetColorSchemeExtInfo(LinecolorScheme, ColorForThemeClr(LinecolorScheme.Val, theme));
- }
- // 填充色
- if (colorType.Equals("Fill"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor FillcolorScheme = shapeStyle.FillReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- // return ColorForThemeClr(FillcolorScheme.Val, theme);
- return GetColorSchemeExtInfo(FillcolorScheme, ColorForThemeClr(FillcolorScheme.Val, theme));
- }
- // 字体颜色
- if (colorType.Equals("Font"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- //return ColorForThemeClr(FontcolorScheme.Val, theme);
- return GetColorSchemeExtInfo(FontcolorScheme, ColorForThemeClr(FontcolorScheme.Val, theme));
- }
- return "";
- }
- /// <summary>
- /// 获取 声明颜色的色调扩展
- /// </summary>
- /// <param name="scheme"></param>
- /// <param name="schemeColor"></param>
- /// <returns></returns>
- public string GetColorSchemeExtInfo(DocumentFormat.OpenXml.Drawing.SchemeColor scheme, string schemeColor)
- {
- var childeElemet = scheme.ChildElements;
- int lumModVal = 0;
- int lumOffVal = 0;
- int tintVal = 0;
- int shadeVal = 0;
- if (childeElemet.Count() > 0)
- {
- foreach (var element in childeElemet)
- {
- if (element is DocumentFormat.OpenXml.Drawing.LuminanceModulation lumMod)
- {
- lumModVal = lumMod.Val;
- }
- if (element is DocumentFormat.OpenXml.Drawing.LuminanceOffset lumOff)
- {
- lumOffVal = lumOff.Val;
- }
- if (element is DocumentFormat.OpenXml.Drawing.Luminance lum)
- {
- }
- //与白色中和
- if (element is DocumentFormat.OpenXml.Drawing.Tint tint)
- {
- tintVal = tint.Val;
- }
- //与黑色中和
- if (element is DocumentFormat.OpenXml.Drawing.Shade shade)
- {
- shadeVal = shade.Val;
- }
- }
- }
- if (lumModVal > 0 || lumOffVal > 0)
- {
- Color color = ColorTranslator.FromHtml("#" + schemeColor);
- schemeColor = ColorHelper.GetColorLumModAndLumOff(color, lumModVal, lumOffVal);
- }
- if (tintVal > 0)
- {
- schemeColor = ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), tintVal, "Tint");
- }
- if (shadeVal > 0)
- {
- schemeColor = ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), shadeVal, "Shade");
- }
- return schemeColor;
- }
- /// <summary>
- /// 获取声明式颜色
- /// </summary>
- /// <param name="colorScheme"></param>
- /// <returns></returns>
- public string GetSchemeColor(IEnumerable<DocumentFormat.OpenXml.Drawing.SchemeColor> colorScheme, Theme theme, ColorMap colorMap)
- {
- string schemeColor = "";
- if (colorScheme.Count() > 0)
- {
- schemeColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
- var scheme = colorScheme.First();
- schemeColor = GetColorSchemeExtInfo(scheme, schemeColor);
- }
- return schemeColor;
- }
- /// <summary>
- /// 获取RGBColor
- /// </summary>
- /// <param name="rgbColors"></param>
- /// <returns></returns>
- public string GetRgbColor(IEnumerable<DocumentFormat.OpenXml.Drawing.RgbColorModelHex> rgbColors)
- {
- string rgbColor = "";
- if (rgbColors.Count() > 0)
- {
- rgbColor = rgbColors.FirstOrDefault().Val;
- }
- return rgbColor;
- }
-
- /// <summary>
- /// 在主题Theme中获取颜色
- /// </summary>
- /// <param name="scv"></param>
- /// <param name="theme"></param>
- /// <returns></returns>
- public static string ColorForThemeClr(string scv, Theme theme)
- {
- DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
- string colorStr = "";
- if (scv.Equals("dk1"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- if (scv.Equals("dk2"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent1"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent2"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent3"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent4"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent5"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("accent6"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("lt1"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("lt2"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("hlink"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- else if (scv.Equals("folHlink"))
- {
- DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
- if (sysColor != null)
- {
- return sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return rgbColor.Val;
- }
- return colorStr;
- }
- return "";
- }
- /// <summary>
- /// 按照ColorMap 去寻找对应的颜色
- /// </summary>
- /// <param name="schemeColorValues"></param>
- /// <param name="colorMap"></param>
- /// <returns></returns>
- public static string SchemeColorForColorMap(DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues, ColorMap colorMap)
- {
- string scv = schemeColorValues.ToString();
- if (scv.Equals("Text1"))
- {
- return colorMap.Text1.ToString();
- }
- if (scv.Equals("Text2"))
- {
- return colorMap.Text2.ToString();
- }
- else if (scv.Equals("Accent1"))
- {
- return colorMap.Accent1.ToString();
- }
- else if (scv.Equals("Accent2"))
- {
- return colorMap.Accent2.ToString();
- }
- else if (scv.Equals("Accent3"))
- {
- return colorMap.Accent3.ToString();
- }
- else if (scv.Equals("Accent4"))
- {
- return colorMap.Accent4.ToString();
- }
- else if (scv.Equals("Accent5"))
- {
- return colorMap.Accent5.ToString();
- }
- else if (scv.Equals("Accent6"))
- {
- return colorMap.Accent6.ToString();
- }
- else if (scv.Equals("Background1"))
- {
- return colorMap.Background1.ToString();
- }
- else if (scv.Equals("Background2"))
- {
- return colorMap.Background2.ToString();
- }
- else if (scv.Equals("Hyperlink"))
- {
- return colorMap.Hyperlink.ToString();
- }
- else if (scv.Equals("FollowedHyperlink"))
- {
- return colorMap.FollowedHyperlink.ToString();
- }
- else if (scv.Equals("PhColor"))
- {
- return "PhColor";
- }
- else if (scv.Equals("Dark1"))
- {
- return "dk1";
- }
- else if (scv.Equals("Dark2"))
- {
- return "dk2";
- }
- else if (scv.Equals("Light1"))
- {
- return "lt1";
- }
- else if (scv.Equals("Light2"))
- {
- return "lt2";
- }
- return null;
- }
- /// <summary>
- /// 转换Surrogate 类型的编码的字符串
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static Dictionary<string, string> StringEncode(string xml)
- {
- Dictionary<string, string> codeValues = new Dictionary<string, string>();
- Char[] charstr = xml.ToCharArray();
- int len = charstr.Length;
- for (int i = 0; i < len; i++)
- {
- var stringBuilder = "";
- string str = Char.ToString(charstr[i]);
- if (Char.GetUnicodeCategory(charstr[i]) == UnicodeCategory.Surrogate)
- {
- string str1 = Char.ToString(charstr[i + 1]);
- str += str1;
- var bytes = Encoding.UTF32.GetBytes(str);
- for (var j = 0; j < bytes.Length; j += 2)
- {
- stringBuilder = bytes[j + 1].ToString("x").PadLeft(2, '0') + bytes[j].ToString("x").PadLeft(2, '0') + stringBuilder;
- }
- var id = Convert.ToInt64(stringBuilder, 16);
- i += 1;
- string code = id + "";
- codeValues.TryAdd(str, code);
- }
- }
- return codeValues;
- }
- /// <summary>
- /// 根据OfficeML转换为MathML
- /// </summary>
- /// <param name="innerXml"></param>
- /// <returns></returns>
- public static string ProcessOMath(string innerXml)
- {
- Dictionary<string, string> codeValues = StringEncode(innerXml);
- if (codeValues != null)
- {
- foreach (string codeValue in codeValues.Keys)
- {
- innerXml = Regex.Replace(innerXml, codeValue, codeValues[codeValue]);
- }
- }
- XElement element = XElement.Load(new StringReader(innerXml));
- List<XElement> elements = element.Elements().ToList();
- element = elements.Where(x => x.Name.LocalName.Equals("oMath")).FirstOrDefault();
- XslCompiledTransform xslTransform = new XslCompiledTransform();
- xslTransform.Load(BaseConfigModel.ContentRootPath + "/XslFiles/OMML2MML.XSL");
- string mathXml = element.ToString();
- string officeML = string.Empty;
- using (TextReader tr = new StringReader(mathXml))
- {
- using (XmlReader reader = XmlReader.Create(tr))
- {
- using (MemoryStream ms = new MemoryStream())
- {
- XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
- settings.ConformanceLevel = ConformanceLevel.Fragment;
- settings.OmitXmlDeclaration = true;
- settings.Encoding = Encoding.Unicode;
- XmlWriter xw = XmlWriter.Create(ms, settings);
- xslTransform.Transform(reader, xw);
- ms.Seek(0, SeekOrigin.Begin);
- using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
- {
- officeML = sr.ReadToEnd();
- }
- }
- }
- }
- officeML = officeML.Replace("mml:", "");
- if (codeValues != null)
- {
- foreach (string codeValue in codeValues.Keys)
- {
- officeML = Regex.Replace(officeML, codeValues[codeValue], codeValue);
- }
- }
- return officeML;
- }
- }
- }
|