123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- 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 PPTXConvertNew
- {
- public static void GetSlideTitles(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);
- }
- }
- }
- }
- }
- public static Object GetSlideElement(SlidePart slidePart , Theme theme , ColorMap colorMap)
- {
- double inchpixel = 96.0, inchpt = 72.0,pxBase=914400.0 , rotBase = 60000;
- double pxConvert = inchpixel / pxBase;
- if (slidePart == null)
- {
- throw new ArgumentNullException("presentationDocument");
- }
-
- // var aaa= from shape in presentationPart.SlideMasterParts.Descendants<ColorMap>() select shape;
- var shaps = from shape in slidePart.Slide.Descendants<Shape>() select shape;
- List<string> repeatElement = new List<string>();
- var elements = from shape in slidePart.Slide select shape;
- List<PPTElement> pptElements = new List<PPTElement>();
- var pictures = from shap in elements.FirstOrDefault().Descendants<Picture>() select shap;
- var textMaths = from shap in elements.FirstOrDefault().Descendants<TextMath>() select shap;
- foreach (var math in textMaths) {
- string type = "math";
- var mathShape = (Shape) math.Parent.Parent.Parent;
- string textColor = "",fillColor="";
- var rgbColor = from shape in mathShape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- if (rgbColor != null && rgbColor.Count() > 0) {
- textColor = rgbColor.FirstOrDefault().Val;
- }
- fillColor = ShapForColor(mathShape, "Fill", theme);
- if (string.IsNullOrEmpty(textColor)) {
- textColor = ShapForColor(mathShape, "Font", theme);
- }
- repeatElement.Add(mathShape.OuterXml);
- string elementData = ProcessOMath(math.InnerXml);
- ShapeProperties properties = mathShape.ShapeProperties;
- if (string.IsNullOrEmpty(fillColor)) {
- var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
- if (solidFills != null && solidFills.Count() > 0)
- {
- var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- if (propRgbColor != null && propRgbColor.Count() > 0)
- {
- fillColor = propRgbColor.FirstOrDefault().Val;
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
- if (colorScheme.Count() > 0) {
- fillColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
- }
- }
- }
- }
-
- 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 = elementData;
- element.fillColor = fillColor;
- element.textColor = textColor;
- pptElements.Add(element);
- }
- foreach (var picture in pictures) {
- 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;
- pptElements.Add(element);
- }
- List<Shape> shapes = shaps.ToList();
- for (int i= 0; i< shapes.Count;i++ ) {
- if (shapes[i].Parent.OuterXml.StartsWith("<mc:Fallback")) {
- shapes.RemoveAt(i);
- }
- }
- for (int i = 0; i < shapes.Count; i++)
- {
- foreach (string enm in repeatElement)
- {
- if (enm.Equals(shapes[i].OuterXml))
- {
- shapes.RemoveAt(i);
- }
- }
- }
- // 获取文本框 和 图形(形状)
- foreach (Shape shapeIn in shapes) {
- ShapeProperties properties = shapeIn.ShapeProperties;
- var presetGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>() select shap;
- string type = "text";
- bool complete = false;
- if (presetGeometrys.Count() > 0)
- {
- DocumentFormat.OpenXml.Drawing.PresetGeometry presetGeometry = presetGeometrys.FirstOrDefault();
- type = presetGeometry.Preset;
- }
- PPTElement element = new PPTElement() { type = type };
- if (properties != null && properties.Transform2D != null) {
- complete = true;
- 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;
- }
- // Console.WriteLine(colorMap.);
- string textColor = "", fillColor = "";
- var runs = from shap in shapeIn.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;
- var schemeColor = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
- if (string.IsNullOrEmpty(textColor)) {
- if (schemeColor != null && schemeColor.Count() > 0)
- {
- foreach (var sc in schemeColor)
- {
- DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues = sc.Val.Value;
- //颜色寻找
- textColor = ColorForThemeClr(SchemeColorForColorMap(schemeColorValues, colorMap), theme);
- }
- }
- }
- DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
- element.text = element.text + run.Text.InnerText;
- }
- var rgbColor = from shape in shapeIn.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- if (string.IsNullOrEmpty(textColor) && rgbColor != null && rgbColor.Count() > 0)
- {
- textColor = rgbColor.FirstOrDefault().Val;
- }
- if (string.IsNullOrEmpty(textColor)) {
- textColor = ShapForColor(shapeIn, "Font", theme);
- }
- //if (element.text.Equals("33333"))
- //{
- // fillColor = ShapForColor(shapeIn, "Fill", theme);
- //}
- fillColor= ShapForColor(shapeIn, "Fill", theme);
- if (string.IsNullOrEmpty(fillColor)) {
- var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
- if (solidFills != null && solidFills.Count() > 0)
- {
- var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
- if (propRgbColor != null && propRgbColor.Count() > 0)
- {
- fillColor = propRgbColor.FirstOrDefault().Val;
- }
- if (string.IsNullOrEmpty(fillColor))
- {
- var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
- if (colorScheme.Count() > 0)
- {
- fillColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
- }
- }
- }
- }
- 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>();
-
- }
- }
- if (complete) {
- pptElements.Add(element);
- }
- // element.text = shapeIn.InnerText;
- }
- return pptElements;
- }
- public static string ShapForColor(Shape shape , string colorType ,Theme theme) {
- if (shape.ShapeStyle == null)
- {
- return "";
- }
- DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle = shape.ShapeStyle;
- 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 "";
- }
- 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = 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 colorStr = sysColor.LastColor;
- }
- DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
- if (rgbColor != null)
- {
- return colorStr = rgbColor.Val;
- }
- return colorStr;
- }
- return "";
- }
- 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;
- }
- private static readonly Regex EmojiRegex = new Regex("&#x?[A-Fa-f0-9]+;");
- private static string ReplaceInvalidXmlCharacterReferences(string input)
- {
- if (input.IndexOf("&#") == -1)
- return input;
- return EmojiRegex.Replace(input, match =>
- {
- string ncr = match.Value;
- uint num;
- var frmt = NumberFormatInfo.InvariantInfo;
- bool isParsed =
- ncr[2] == 'x' ? // the x must be lowercase in XML documents
- uint.TryParse(ncr.Substring(3, ncr.Length - 4), NumberStyles.AllowHexSpecifier, frmt, out num) :
- uint.TryParse(ncr.Substring(2, ncr.Length - 3), NumberStyles.Integer, frmt, out num);
- return isParsed && !XmlConvert.IsXmlChar((char)num) ? "" : ncr;
- });
- }
- 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 = 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 = i + 1;
- string code = id +"";
- codeValues.TryAdd(str, code);
- }
- }
- return codeValues;
- }
- 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;
- }
- }
- }
|