PresentationConvert.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Office2010.Drawing;
  3. using DocumentFormat.OpenXml.Packaging;
  4. using DocumentFormat.OpenXml.Presentation;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Xml;
  13. using System.Xml.Linq;
  14. using System.Xml.Xsl;
  15. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  16. using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
  17. using Theme = DocumentFormat.OpenXml.Drawing.Theme;
  18. namespace TEAMModelOS.Test.PPTX
  19. {
  20. public class PresentationConvert
  21. {
  22. const double inchpixel = 96.0, inchpt = 72.0, pxBase = 914400.0, rotBase = 60000;
  23. /// <summary>
  24. /// 处理一页PPT的元素
  25. /// </summary>
  26. /// <param name="slidePart"></param>
  27. /// <param name="theme"></param>
  28. /// <param name="colorMap"></param>
  29. /// <returns></returns>
  30. public object GetSlideElement(SlidePart slidePart, Theme theme, ColorMap colorMap) {
  31. List<PPTElement> elements = new List<PPTElement>();
  32. var shapeTrees = from shap in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>() select shap;
  33. if (shapeTrees.Count() > 0 && shapeTrees.First().ChildElements.Count > 0) {
  34. OpenXmlElementList openXmlElements = shapeTrees.First().ChildElements;
  35. int index = 0;
  36. foreach (OpenXmlElement element in openXmlElements) {
  37. PPTElement pptElement = null;
  38. if (element is DocumentFormat.OpenXml.Presentation.Shape shape) {
  39. pptElement = ShapeConvert(shape, theme, colorMap);
  40. }
  41. if (element is DocumentFormat.OpenXml.Presentation.Picture picture)
  42. {
  43. pptElement = PictureConvert(picture, slidePart);
  44. }
  45. if (element is DocumentFormat.OpenXml.AlternateContent content)
  46. {
  47. pptElement = AlternateContentConvert(content, theme, colorMap);
  48. }
  49. if (element is DocumentFormat.OpenXml.Presentation.GraphicFrame graphicFrame)
  50. {
  51. pptElement = GraphicFrameConvert(graphicFrame, theme, colorMap);
  52. }
  53. index++;
  54. elements.Add(pptElement);
  55. }
  56. }
  57. return elements;
  58. }
  59. public PPTElement GraphicFrameConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap) {
  60. DocumentFormat.OpenXml.Drawing.GraphicData graphicData = graphicFrame.ChildElements.First<DocumentFormat.OpenXml.Drawing.GraphicData>();
  61. if (graphicData != null) {
  62. OpenXmlElement element = graphicData.ChildElements.First();
  63. if (element != null)
  64. {
  65. if (element is DocumentFormat.OpenXml.Drawing.Table table)
  66. {
  67. return TableConvert(graphicFrame, theme, colorMap);
  68. }
  69. }
  70. }
  71. return null;
  72. }
  73. public PPTElement TableConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap) {
  74. return null;
  75. }
  76. /// <summary>
  77. /// 处理普通形状的元素
  78. /// </summary>
  79. /// <param name="shape"></param>
  80. /// <returns></returns>
  81. public PPTElement ShapeConvert(Shape shape ,Theme theme ,ColorMap colorMap) {
  82. // shape.base
  83. //文字颜色
  84. string textColor = "";
  85. //填充颜色
  86. string fillColor = "";
  87. // 形状类型
  88. string type = "text";
  89. //字体大小
  90. int fontSize = 1800;
  91. //是否闭合形状
  92. bool complete = false;
  93. ShapeProperties properties = shape.ShapeProperties;
  94. IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries= GetPresetGeometry(properties);
  95. if (presetGeometries.Count() > 0) {
  96. type = presetGeometries.FirstOrDefault().Preset;
  97. }
  98. DocumentFormat.OpenXml.Drawing.Transform2D transform2D= GetTransform2D(properties);
  99. PPTElement element = new PPTElement() { type = type ,fontSize=fontSize};
  100. if (transform2D != null) {
  101. element.complete = complete;
  102. element.offx = properties.Transform2D.Offset.X;
  103. element.offy = properties.Transform2D.Offset.Y;
  104. element.extx = properties.Transform2D.Extents.Cx;
  105. element.exty = properties.Transform2D.Extents.Cy;
  106. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  107. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  108. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  109. }
  110. ///获取runs的文本及颜色信息
  111. var runs = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.Run>() select shap;
  112. foreach (var run in runs)
  113. {
  114. var runprps = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
  115. DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
  116. if (runProperties != null && fontSize == 1800) {
  117. fontSize = runProperties.FontSize!=null && runProperties.FontSize>0 ?runProperties.FontSize.Value: fontSize;
  118. }
  119. var schemeColor = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
  120. var rgbColors = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
  121. textColor = GetSchemeColor(schemeColor ,theme ,colorMap);
  122. if (string.IsNullOrEmpty(textColor)) {
  123. textColor = GetRgbColor(rgbColors);
  124. }
  125. element.text = element.text + run.Text.InnerText;
  126. }
  127. if (string.IsNullOrEmpty(textColor)) {
  128. textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
  129. }
  130. if (string.IsNullOrEmpty(fillColor)) {
  131. fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
  132. }
  133. if (string.IsNullOrEmpty(fillColor)) {
  134. fillColor = GetPropertieFillColor(properties, theme, colorMap);
  135. }
  136. element.textColor = textColor;
  137. element.fillColor = fillColor;
  138. var custGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
  139. if (custGeometrys.Count() > 0)
  140. {
  141. var pathLists = from shap in custGeometrys.First().Descendants<DocumentFormat.OpenXml.Drawing.Path>() select shap;
  142. if (pathLists.Count() > 0)
  143. {
  144. var moveTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.MoveTo>();
  145. var lineTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.LineTo>();
  146. var BezierTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.CubicBezierCurveTo>();
  147. }
  148. }
  149. return element;
  150. }
  151. /// <summary>
  152. /// 处理图片的元素
  153. /// </summary>
  154. /// <param name="picture"></param>
  155. /// <returns></returns>
  156. public PPTElement PictureConvert(Picture picture , SlidePart slidePart)
  157. {
  158. BlipFill blipFill = picture.BlipFill;
  159. var imageRid = blipFill.Blip.Embed.Value;
  160. string type = "picture";
  161. IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
  162. ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
  163. var contentType = imagePart.ContentType;
  164. string base64 = "";
  165. using (var stream = imagePart.GetStream())
  166. {
  167. byte[] buffer = new byte[stream.Length];
  168. stream.Read(buffer, 0, buffer.Length);
  169. stream.Close();
  170. base64 = System.Convert.ToBase64String(buffer);
  171. base64 = "data:" + contentType + ";base64," + base64;
  172. }
  173. ShapeProperties properties = picture.ShapeProperties;
  174. PPTElement element = new PPTElement() { type = type };
  175. if (properties != null && properties.Transform2D != null)
  176. {
  177. element.offx = properties.Transform2D.Offset.X;
  178. element.offy = properties.Transform2D.Offset.Y;
  179. element.extx = properties.Transform2D.Extents.Cx;
  180. element.exty = properties.Transform2D.Extents.Cy;
  181. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  182. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  183. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  184. }
  185. element.text = base64;
  186. return element;
  187. }
  188. /// <summary>
  189. /// 处理兼容元素
  190. /// </summary>
  191. /// <param name="picture"></param>
  192. /// <returns></returns>
  193. public PPTElement AlternateContentConvert(AlternateContent content, Theme theme, ColorMap colorMap)
  194. {
  195. AlternateContentChoice choice= content.ChildElements.First<AlternateContentChoice>();
  196. OpenXmlElement element = choice.ChildElements.First();
  197. if (element != null) {
  198. if (element is DocumentFormat.OpenXml.Presentation.Shape shape)
  199. {
  200. var textMaths = from shap in shape.Descendants<TextMath>() select shap;
  201. if (textMaths.Count() > 0) {
  202. return MathConvert(shape ,theme ,colorMap);
  203. }
  204. }
  205. }
  206. return null;
  207. }
  208. /// <summary>
  209. /// 处理公式
  210. /// </summary>
  211. /// <param name="shape"></param>
  212. /// <param name="theme"></param>
  213. /// <param name="colorMap"></param>
  214. /// <returns></returns>
  215. public PPTElement MathConvert(DocumentFormat.OpenXml.Presentation.Shape shape, Theme theme, ColorMap colorMap) {
  216. //文字颜色
  217. string textColor = "";
  218. //填充颜色
  219. string fillColor = "";
  220. // 形状类型
  221. string type = "math";
  222. //字体大小
  223. int fontSize = 1800;
  224. //是否闭合形状
  225. bool complete = true;
  226. ShapeProperties properties = shape.ShapeProperties;
  227. //IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
  228. //if (presetGeometries.Count() > 0)
  229. //{
  230. // type = presetGeometries.FirstOrDefault().Preset;
  231. //}
  232. DocumentFormat.OpenXml.Drawing.Transform2D transform2D = GetTransform2D(properties);
  233. PPTElement element = new PPTElement() { type = type, fontSize = fontSize };
  234. if (transform2D != null)
  235. {
  236. element.complete = complete;
  237. element.offx = properties.Transform2D.Offset.X;
  238. element.offy = properties.Transform2D.Offset.Y;
  239. element.extx = properties.Transform2D.Extents.Cx;
  240. element.exty = properties.Transform2D.Extents.Cy;
  241. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  242. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  243. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  244. }
  245. //获取行内样式
  246. if (string.IsNullOrEmpty(textColor)) {
  247. var rgbColor = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
  248. textColor =GetRgbColor(rgbColor);
  249. if (string.IsNullOrEmpty(textColor))
  250. {
  251. var schemes = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
  252. textColor = GetSchemeColor(schemes ,theme ,colorMap);
  253. }
  254. }
  255. if (string.IsNullOrEmpty(textColor))
  256. {
  257. textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
  258. }
  259. if (string.IsNullOrEmpty(fillColor))
  260. {
  261. fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
  262. }
  263. if (string.IsNullOrEmpty(fillColor))
  264. {
  265. fillColor = GetPropertieFillColor(properties, theme, colorMap);
  266. }
  267. element.fillColor = fillColor;
  268. element.textColor = textColor;
  269. var textMaths = from shap in shape.Descendants<TextMath>() select shap;
  270. var math= textMaths.First<TextMath>();
  271. string elementData = ProcessOMath(math.InnerXml);
  272. element.text = elementData;
  273. return element;
  274. }
  275. /// <summary>
  276. /// 处理元素的Transform2D属性
  277. /// </summary>
  278. /// <param name="properties"></param>
  279. /// <returns></returns>
  280. public DocumentFormat.OpenXml.Drawing.Transform2D GetTransform2D(ShapeProperties properties) {
  281. if (properties != null) {
  282. return properties.Transform2D;
  283. }
  284. return null;
  285. }
  286. /// <summary>
  287. /// 处理元素的Transform2D属性
  288. /// </summary>
  289. /// <param name="properties"></param>
  290. /// <returns></returns>
  291. public IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> GetPresetGeometry(ShapeProperties properties)
  292. {
  293. if (properties != null)
  294. {
  295. var presetGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>() select shap;
  296. return presetGeometrys;
  297. }
  298. return null;
  299. }
  300. /// <summary>
  301. /// 处理元素属性的填充色
  302. /// </summary>
  303. /// <param name="properties"></param>
  304. /// <returns></returns>
  305. public string GetPropertieFillColor(ShapeProperties properties, Theme theme, ColorMap colorMap)
  306. {
  307. string fillColor = "";
  308. if (properties != null)
  309. {
  310. var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
  311. if ( solidFills.Count() > 0)
  312. {
  313. var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  314. fillColor = GetRgbColor(propRgbColor);
  315. if (string.IsNullOrEmpty(fillColor))
  316. {
  317. var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
  318. fillColor = GetSchemeColor(colorScheme, theme, colorMap);
  319. }
  320. }
  321. }
  322. return fillColor;
  323. }
  324. /// <summary>
  325. /// 获取ShapeStyle样式表 各种属性 的颜色
  326. /// </summary>
  327. /// <param name="shapeStyle"></param>
  328. /// <param name="colorType"></param>
  329. /// <param name="theme"></param>
  330. /// <returns></returns>
  331. public string GetShapeStyleColor(DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle, string colorType, Theme theme) {
  332. if (shapeStyle == null)
  333. {
  334. return "";
  335. }
  336. // 效果颜色
  337. if (colorType.Equals("Effect"))
  338. {
  339. DocumentFormat.OpenXml.Drawing.SchemeColor EffectcolorScheme = shapeStyle.EffectReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  340. return ColorForThemeClr(EffectcolorScheme.Val, theme);
  341. }
  342. // 线条颜色
  343. if (colorType.Equals("Line"))
  344. {
  345. DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  346. return ColorForThemeClr(LinecolorScheme.Val, theme);
  347. }
  348. // 填充色
  349. if (colorType.Equals("Fill"))
  350. {
  351. DocumentFormat.OpenXml.Drawing.SchemeColor FillcolorScheme = shapeStyle.FillReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  352. return ColorForThemeClr(FillcolorScheme.Val, theme);
  353. }
  354. // 字体颜色
  355. if (colorType.Equals("Font"))
  356. {
  357. DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  358. return ColorForThemeClr(FontcolorScheme.Val, theme);
  359. }
  360. return "";
  361. }
  362. /// <summary>
  363. /// 获取声明式颜色
  364. /// </summary>
  365. /// <param name="rgbColors"></param>
  366. /// <returns></returns>
  367. public string GetSchemeColor(IEnumerable<DocumentFormat.OpenXml.Drawing.SchemeColor> colorScheme, Theme theme, ColorMap colorMap)
  368. {
  369. string rgbColor = "";
  370. if (colorScheme.Count() > 0)
  371. {
  372. rgbColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
  373. }
  374. return rgbColor;
  375. }
  376. /// <summary>
  377. /// 获取RGBColor
  378. /// </summary>
  379. /// <param name="rgbColors"></param>
  380. /// <returns></returns>
  381. public string GetRgbColor(IEnumerable<DocumentFormat.OpenXml.Drawing.RgbColorModelHex> rgbColors) {
  382. string rgbColor = "";
  383. if ( rgbColors.Count() > 0)
  384. {
  385. rgbColor = rgbColors.FirstOrDefault().Val;
  386. }
  387. return rgbColor;
  388. }
  389. /// <summary>
  390. /// 加载PPTX文件
  391. /// </summary>
  392. /// <param name="presentationFile"></param>
  393. /// <returns></returns>
  394. public object LoadPresentation(string presentationFile) {
  395. using (PresentationDocument presentationDocument =PresentationDocument.Open(presentationFile, false))
  396. {
  397. if (presentationDocument == null)
  398. {
  399. throw new ArgumentNullException("presentationDocument");
  400. }
  401. // Get a PresentationPart object from the PresentationDocument object.
  402. PresentationPart presentationPart = presentationDocument.PresentationPart;
  403. if (presentationPart != null && presentationPart.Presentation != null)
  404. {
  405. var slideMasterParts = presentationPart.SlideMasterParts;
  406. ColorMap colorMap = null;
  407. Theme theme = null;
  408. foreach (var slideMasterPart in slideMasterParts)
  409. {
  410. if (colorMap != null && theme != null)
  411. {
  412. break;
  413. }
  414. colorMap = slideMasterPart.SlideMaster.ColorMap;
  415. theme = slideMasterPart.ThemePart.Theme;
  416. }
  417. // Get a Presentation object from the PresentationPart object.
  418. Presentation presentation = presentationPart.Presentation;
  419. if (presentation.SlideIdList != null)
  420. {
  421. //获取PPT 一页大小
  422. int x = presentation.SlideSize.Cx;
  423. int y = presentation.SlideSize.Cy;
  424. // Get the title of each slide in the slide order.
  425. // 获取的是几页PPT数量
  426. foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
  427. {
  428. // 获取这一页 PPT 的id
  429. string id = slideId.RelationshipId;
  430. SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
  431. //获取当前页 PPT 的所有元素
  432. GetSlideElement(slidePart, theme, colorMap);
  433. }
  434. }
  435. }
  436. return null;
  437. }
  438. }
  439. /// <summary>
  440. /// 在主题Theme中获取颜色
  441. /// </summary>
  442. /// <param name="scv"></param>
  443. /// <param name="theme"></param>
  444. /// <returns></returns>
  445. public static string ColorForThemeClr(string scv, Theme theme)
  446. {
  447. DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
  448. string colorStr = "";
  449. if (scv.Equals("dk1"))
  450. {
  451. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  452. if (sysColor != null)
  453. {
  454. return sysColor.LastColor;
  455. }
  456. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  457. if (rgbColor != null)
  458. {
  459. return rgbColor.Val;
  460. }
  461. return colorStr;
  462. }
  463. if (scv.Equals("dk2"))
  464. {
  465. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  466. if (sysColor != null)
  467. {
  468. return sysColor.LastColor;
  469. }
  470. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  471. if (rgbColor != null)
  472. {
  473. return rgbColor.Val;
  474. }
  475. return colorStr;
  476. }
  477. else if (scv.Equals("accent1"))
  478. {
  479. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  480. if (sysColor != null)
  481. {
  482. return sysColor.LastColor;
  483. }
  484. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  485. if (rgbColor != null)
  486. {
  487. return rgbColor.Val;
  488. }
  489. return colorStr;
  490. }
  491. else if (scv.Equals("accent2"))
  492. {
  493. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  494. if (sysColor != null)
  495. {
  496. return sysColor.LastColor;
  497. }
  498. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  499. if (rgbColor != null)
  500. {
  501. return rgbColor.Val;
  502. }
  503. return colorStr;
  504. }
  505. else if (scv.Equals("accent3"))
  506. {
  507. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  508. if (sysColor != null)
  509. {
  510. return sysColor.LastColor;
  511. }
  512. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  513. if (rgbColor != null)
  514. {
  515. return rgbColor.Val;
  516. }
  517. return colorStr;
  518. }
  519. else if (scv.Equals("accent4"))
  520. {
  521. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  522. if (sysColor != null)
  523. {
  524. return sysColor.LastColor;
  525. }
  526. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  527. if (rgbColor != null)
  528. {
  529. return rgbColor.Val;
  530. }
  531. return colorStr;
  532. }
  533. else if (scv.Equals("accent5"))
  534. {
  535. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  536. if (sysColor != null)
  537. {
  538. return sysColor.LastColor;
  539. }
  540. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  541. if (rgbColor != null)
  542. {
  543. return rgbColor.Val;
  544. }
  545. return colorStr;
  546. }
  547. else if (scv.Equals("accent6"))
  548. {
  549. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  550. if (sysColor != null)
  551. {
  552. return sysColor.LastColor;
  553. }
  554. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  555. if (rgbColor != null)
  556. {
  557. return rgbColor.Val;
  558. }
  559. return colorStr;
  560. }
  561. else if (scv.Equals("lt1"))
  562. {
  563. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  564. if (sysColor != null)
  565. {
  566. return sysColor.LastColor;
  567. }
  568. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  569. if (rgbColor != null)
  570. {
  571. return rgbColor.Val;
  572. }
  573. return colorStr;
  574. }
  575. else if (scv.Equals("lt2"))
  576. {
  577. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  578. if (sysColor != null)
  579. {
  580. return sysColor.LastColor;
  581. }
  582. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  583. if (rgbColor != null)
  584. {
  585. return rgbColor.Val;
  586. }
  587. return colorStr;
  588. }
  589. else if (scv.Equals("hlink"))
  590. {
  591. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  592. if (sysColor != null)
  593. {
  594. return sysColor.LastColor;
  595. }
  596. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  597. if (rgbColor != null)
  598. {
  599. return rgbColor.Val;
  600. }
  601. return colorStr;
  602. }
  603. else if (scv.Equals("folHlink"))
  604. {
  605. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  606. if (sysColor != null)
  607. {
  608. return sysColor.LastColor;
  609. }
  610. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  611. if (rgbColor != null)
  612. {
  613. return rgbColor.Val;
  614. }
  615. return colorStr;
  616. }
  617. return "";
  618. }
  619. /// <summary>
  620. /// 按照ColorMap 去寻找对应的颜色
  621. /// </summary>
  622. /// <param name="schemeColorValues"></param>
  623. /// <param name="colorMap"></param>
  624. /// <returns></returns>
  625. public static string SchemeColorForColorMap(DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues, ColorMap colorMap)
  626. {
  627. string scv = schemeColorValues.ToString();
  628. if (scv.Equals("Text1"))
  629. {
  630. return colorMap.Text1.ToString();
  631. }
  632. if (scv.Equals("Text2"))
  633. {
  634. return colorMap.Text2.ToString();
  635. }
  636. else if (scv.Equals("Accent1"))
  637. {
  638. return colorMap.Accent1.ToString();
  639. }
  640. else if (scv.Equals("Accent2"))
  641. {
  642. return colorMap.Accent2.ToString();
  643. }
  644. else if (scv.Equals("Accent3"))
  645. {
  646. return colorMap.Accent3.ToString();
  647. }
  648. else if (scv.Equals("Accent4"))
  649. {
  650. return colorMap.Accent4.ToString();
  651. }
  652. else if (scv.Equals("Accent5"))
  653. {
  654. return colorMap.Accent5.ToString();
  655. }
  656. else if (scv.Equals("Accent6"))
  657. {
  658. return colorMap.Accent6.ToString();
  659. }
  660. else if (scv.Equals("Background1"))
  661. {
  662. return colorMap.Background1.ToString();
  663. }
  664. else if (scv.Equals("Background2"))
  665. {
  666. return colorMap.Background2.ToString();
  667. }
  668. else if (scv.Equals("Hyperlink"))
  669. {
  670. return colorMap.Hyperlink.ToString();
  671. }
  672. else if (scv.Equals("FollowedHyperlink"))
  673. {
  674. return colorMap.FollowedHyperlink.ToString();
  675. }
  676. else if (scv.Equals("PhColor"))
  677. {
  678. return "PhColor";
  679. }
  680. else if (scv.Equals("Dark1"))
  681. {
  682. return "dk1";
  683. }
  684. else if (scv.Equals("Dark2"))
  685. {
  686. return "dk2";
  687. }
  688. else if (scv.Equals("Light1"))
  689. {
  690. return "lt1";
  691. }
  692. else if (scv.Equals("Light2"))
  693. {
  694. return "lt2";
  695. }
  696. return null;
  697. }
  698. /// <summary>
  699. /// 转换Surrogate 类型的编码的字符串
  700. /// </summary>
  701. /// <param name="xml"></param>
  702. /// <returns></returns>
  703. public static Dictionary<string, string> StringEncode(string xml)
  704. {
  705. Dictionary<string, string> codeValues = new Dictionary<string, string>();
  706. Char[] charstr = xml.ToCharArray();
  707. int len = charstr.Length;
  708. for (int i = 0; i < len; i++)
  709. {
  710. var stringBuilder = "";
  711. string str = Char.ToString(charstr[i]);
  712. if (Char.GetUnicodeCategory(charstr[i]) == UnicodeCategory.Surrogate)
  713. {
  714. string str1 = Char.ToString(charstr[i + 1]);
  715. str += str1;
  716. var bytes = Encoding.UTF32.GetBytes(str);
  717. for (var j = 0; j < bytes.Length; j += 2)
  718. {
  719. stringBuilder = bytes[j + 1].ToString("x").PadLeft(2, '0') + bytes[j].ToString("x").PadLeft(2, '0') + stringBuilder;
  720. }
  721. var id = Convert.ToInt64(stringBuilder, 16);
  722. i += 1;
  723. string code = id + "";
  724. codeValues.TryAdd(str, code);
  725. }
  726. }
  727. return codeValues;
  728. }
  729. /// <summary>
  730. /// 根据OfficeML转换为MathML
  731. /// </summary>
  732. /// <param name="innerXml"></param>
  733. /// <returns></returns>
  734. public static string ProcessOMath(string innerXml)
  735. {
  736. Dictionary<string, string> codeValues = StringEncode(innerXml);
  737. if (codeValues != null)
  738. {
  739. foreach (string codeValue in codeValues.Keys)
  740. {
  741. innerXml = Regex.Replace(innerXml, codeValue, codeValues[codeValue]);
  742. }
  743. }
  744. XElement element = XElement.Load(new StringReader(innerXml));
  745. List<XElement> elements = element.Elements().ToList();
  746. element = elements.Where(x => x.Name.LocalName.Equals("oMath")).FirstOrDefault();
  747. XslCompiledTransform xslTransform = new XslCompiledTransform();
  748. xslTransform.Load("E:/document/OMML2MML.XSL");
  749. string mathXml = element.ToString();
  750. string officeML = string.Empty;
  751. using (TextReader tr = new StringReader(mathXml))
  752. {
  753. using (XmlReader reader = XmlReader.Create(tr))
  754. {
  755. using (MemoryStream ms = new MemoryStream())
  756. {
  757. XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
  758. settings.ConformanceLevel = ConformanceLevel.Fragment;
  759. settings.OmitXmlDeclaration = true;
  760. settings.Encoding = Encoding.Unicode;
  761. XmlWriter xw = XmlWriter.Create(ms, settings);
  762. xslTransform.Transform(reader, xw);
  763. ms.Seek(0, SeekOrigin.Begin);
  764. using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  765. {
  766. officeML = sr.ReadToEnd();
  767. }
  768. }
  769. }
  770. }
  771. officeML = officeML.Replace("mml:", "");
  772. if (codeValues != null)
  773. {
  774. foreach (string codeValue in codeValues.Keys)
  775. {
  776. officeML = Regex.Replace(officeML, codeValues[codeValue], codeValue);
  777. }
  778. }
  779. return officeML;
  780. }
  781. }
  782. }