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()) { // 获取这一页 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() select shape; var shaps = from shape in slidePart.Slide.Descendants() select shape; List repeatElement = new List(); var elements = from shape in slidePart.Slide select shape; List pptElements = new List(); var pictures = from shap in elements.FirstOrDefault().Descendants() select shap; var textMaths = from shap in elements.FirstOrDefault().Descendants() 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() 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() select shape; if (solidFills != null && solidFills.Count() > 0) { var propRgbColor = from shape in solidFills.First().Descendants() select shape; if (propRgbColor != null && propRgbColor.Count() > 0) { fillColor = propRgbColor.FirstOrDefault().Val; } if (string.IsNullOrEmpty(fillColor)) { var colorScheme = from shape in solidFills.First().Descendants() 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 shapes = shaps.ToList(); for (int i= 0; i< shapes.Count;i++ ) { if (shapes[i].Parent.OuterXml.StartsWith("() 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() select shap; foreach (var run in runs) { var runprps = from shap in run.Descendants() select shap; var schemeColor = from shap in run.Descendants() 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() 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() select shape; if (solidFills != null && solidFills.Count() > 0) { var propRgbColor = from shape in solidFills.First().Descendants() select shape; if (propRgbColor != null && propRgbColor.Count() > 0) { fillColor = propRgbColor.FirstOrDefault().Val; } if (string.IsNullOrEmpty(fillColor)) { var colorScheme = from shape in solidFills.First().Descendants() 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() select shap; if (custGeometrys.Count() > 0) { var pathLists = from shap in custGeometrys.First().Descendants() select shap; if (pathLists.Count() > 0) { var moveTos= pathLists.First().ChildElements.OfType(); var lineTos = pathLists.First().ChildElements.OfType(); var BezierTos = pathLists.First().ChildElements.OfType(); } } 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(); return ColorForThemeClr(EffectcolorScheme.Val, theme); } if (colorType.Equals("Line")) { DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First(); return ColorForThemeClr(LinecolorScheme.Val, theme); } if (colorType.Equals("Fill")) { DocumentFormat.OpenXml.Drawing.SchemeColor FillcolorScheme = shapeStyle.FillReference.ChildElements.First(); return ColorForThemeClr(FillcolorScheme.Val, theme); } if (colorType.Equals("Font")) { DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First(); 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(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } if (scv.Equals("dk2")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent1")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent2")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent3")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent4")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent5")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("accent6")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("lt1")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("lt2")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("hlink")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First(); if (rgbColor != null) { return colorStr = rgbColor.Val; } return colorStr; } else if (scv.Equals("folHlink")) { DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First(); if (sysColor != null) { return colorStr = sysColor.LastColor; } DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First(); 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 stringEncode(string xml) { Dictionary codeValues = new Dictionary() ; 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 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 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; } } }