123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796 |
- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Office2010.Drawing;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using System;
- using System.Collections.Generic;
- 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.Helper.Common.JsonHelper;
- using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
- using Theme = DocumentFormat.OpenXml.Drawing.Theme;
- namespace TEAMModelOS.Test.PPTX
- {
- public class PresentationConvert
- {
- const double inchpixel = 96.0, inchpt = 72.0, pxBase = 914400.0, rotBase = 60000;
- /// <summary>
- /// 处理一页PPT的元素
- /// </summary>
- /// <param name="slidePart"></param>
- /// <param name="theme"></param>
- /// <param name="colorMap"></param>
- /// <returns></returns>
- public object GetSlideElement(SlidePart slidePart, Theme theme, ColorMap colorMap) {
- List<PPTElement> elements = new List<PPTElement>();
- 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) {
- PPTElement pptElement = null;
- if (element is DocumentFormat.OpenXml.Presentation.Shape shape) {
- pptElement = ShapeConvert(shape, theme, colorMap);
- }
- if (element is DocumentFormat.OpenXml.Presentation.Picture picture)
- {
- pptElement = PictureConvert(picture, slidePart);
- }
- if (element is DocumentFormat.OpenXml.AlternateContent content)
- {
- pptElement = AlternateContentConvert(content, theme, colorMap);
- }
- if (element is DocumentFormat.OpenXml.Presentation.GraphicFrame graphicFrame)
- {
- pptElement = GraphicFrameConvert(graphicFrame, theme, colorMap);
- }
- index++;
- elements.Add(pptElement);
- }
- }
- return elements;
- }
- public PPTElement GraphicFrameConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap) {
- 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);
- }
- }
- }
- return null;
- }
- public PPTElement TableConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap) {
- return null;
- }
- /// <summary>
- /// 处理普通形状的元素
- /// </summary>
- /// <param name="shape"></param>
- /// <returns></returns>
- public PPTElement ShapeConvert(Shape shape ,Theme theme ,ColorMap colorMap) {
- // shape.base
- //文字颜色
- string textColor = "";
- //填充颜色
- string fillColor = "";
- // 形状类型
- string type = "text";
- //字体大小
- int fontSize = 1800;
- //是否闭合形状
- bool complete = false;
- 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);
- PPTElement element = new PPTElement() { type = type ,fontSize=fontSize};
- if (transform2D != null) {
- element.complete = complete;
- element.offx = properties.Transform2D.Offset.X;
- element.offy = properties.Transform2D.Offset.Y;
- element.extx = properties.Transform2D.Extents.Cx;
- element.exty = properties.Transform2D.Extents.Cy;
- 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;
- }
- ///获取runs的文本及颜色信息
- var runs = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.Run>() select shap;
-
- foreach (var run in runs)
- {
- var runprps = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
- DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
- if (runProperties != null && fontSize == 1800) {
- fontSize = runProperties.FontSize!=null && runProperties.FontSize>0 ?runProperties.FontSize.Value: fontSize;
- }
- 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)) {
- textColor = GetRgbColor(rgbColors);
- }
- element.text = element.text + run.Text.InnerText;
- }
- if (string.IsNullOrEmpty(textColor)) {
- textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
- }
- if (string.IsNullOrEmpty(fillColor)) {
- fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
- }
- if (string.IsNullOrEmpty(fillColor)) {
- fillColor = GetPropertieFillColor(properties, theme, colorMap);
- }
- element.textColor = textColor;
- 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)
- {
- var moveTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.MoveTo>();
- var lineTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.LineTo>();
- var BezierTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.CubicBezierCurveTo>();
- }
- }
- return element;
- }
- /// <summary>
- /// 处理图片的元素
- /// </summary>
- /// <param name="picture"></param>
- /// <returns></returns>
- public PPTElement PictureConvert(Picture picture , SlidePart slidePart)
- {
- BlipFill blipFill = picture.BlipFill;
- var imageRid = blipFill.Blip.Embed.Value;
- string type = "picture";
- 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;
- PPTElement element = new PPTElement() { type = type };
- if (properties != null && properties.Transform2D != null)
- {
- element.offx = properties.Transform2D.Offset.X;
- element.offy = properties.Transform2D.Offset.Y;
- element.extx = properties.Transform2D.Extents.Cx;
- element.exty = properties.Transform2D.Extents.Cy;
- 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.text = base64;
- return element;
- }
- /// <summary>
- /// 处理兼容元素
- /// </summary>
- /// <param name="picture"></param>
- /// <returns></returns>
- public PPTElement AlternateContentConvert(AlternateContent content, Theme theme, ColorMap colorMap)
- {
- AlternateContentChoice choice= content.ChildElements.First<AlternateContentChoice>();
- OpenXmlElement element = choice.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 MathConvert(shape ,theme ,colorMap);
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 处理公式
- /// </summary>
- /// <param name="shape"></param>
- /// <param name="theme"></param>
- /// <param name="colorMap"></param>
- /// <returns></returns>
- public PPTElement MathConvert(DocumentFormat.OpenXml.Presentation.Shape shape, Theme theme, ColorMap colorMap) {
- //文字颜色
- 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);
- PPTElement element = new PPTElement() { type = type, fontSize = fontSize };
- if (transform2D != null)
- {
- element.complete = complete;
- element.offx = properties.Transform2D.Offset.X;
- element.offy = properties.Transform2D.Offset.Y;
- element.extx = properties.Transform2D.Extents.Cx;
- element.exty = properties.Transform2D.Extents.Cy;
- 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;
- }
- //获取行内样式
- 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);
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- fillColor = GetPropertieFillColor(properties, theme, colorMap);
- }
- element.fillColor = fillColor;
- element.textColor = textColor;
- var textMaths = from shap in shape.Descendants<TextMath>() select shap;
- var math= textMaths.First<TextMath>();
- string elementData = ProcessOMath(math.InnerXml);
- element.text = elementData;
- 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;
- }
- /// <summary>
- /// 处理元素属性的填充色
- /// </summary>
- /// <param name="properties"></param>
- /// <returns></returns>
- public string GetPropertieFillColor(ShapeProperties properties, Theme theme, ColorMap colorMap)
- {
- string fillColor = "";
- if (properties != null)
- {
- var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
- if ( solidFills.Count() > 0)
- {
- var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- fillColor = GetRgbColor(propRgbColor);
- if (string.IsNullOrEmpty(fillColor))
- {
- var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
- fillColor = GetSchemeColor(colorScheme, theme, colorMap);
- }
- }
- }
- return fillColor;
- }
- /// <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);
- }
- // 线条颜色
- if (colorType.Equals("Line"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- return 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);
- }
- // 字体颜色
- if (colorType.Equals("Font"))
- {
- DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
- return ColorForThemeClr(FontcolorScheme.Val, theme);
- }
- return "";
- }
- /// <summary>
- /// 获取声明式颜色
- /// </summary>
- /// <param name="rgbColors"></param>
- /// <returns></returns>
- public string GetSchemeColor(IEnumerable<DocumentFormat.OpenXml.Drawing.SchemeColor> colorScheme, Theme theme, ColorMap colorMap)
- {
- string rgbColor = "";
- if (colorScheme.Count() > 0)
- {
- rgbColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
- }
- return rgbColor;
- }
- /// <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>
- /// 加载PPTX文件
- /// </summary>
- /// <param name="presentationFile"></param>
- /// <returns></returns>
- public object LoadPresentation(string presentationFile) {
- using (PresentationDocument presentationDocument =PresentationDocument.Open(presentationFile, false))
- {
- if (presentationDocument == null)
- {
- throw new ArgumentNullException("presentationDocument");
- }
- // Get a PresentationPart object from the PresentationDocument object.
- PresentationPart presentationPart = presentationDocument.PresentationPart;
- if (presentationPart != null && presentationPart.Presentation != null)
- {
- 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;
- }
- // Get a Presentation object from the PresentationPart object.
- Presentation presentation = presentationPart.Presentation;
- if (presentation.SlideIdList != null)
- {
- //获取PPT 一页大小
- int x = presentation.SlideSize.Cx;
- int y = presentation.SlideSize.Cy;
- // Get the title of each slide in the slide order.
- // 获取的是几页PPT数量
- foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
- {
- // 获取这一页 PPT 的id
- string id = slideId.RelationshipId;
- SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
- //获取当前页 PPT 的所有元素
- GetSlideElement(slidePart, theme, colorMap);
- }
- }
- }
- return null;
- }
- }
- /// <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("E:/document/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;
- }
- }
- }
|