PPTXConvertNew.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 PPTXConvertNew
  21. {
  22. public static void GetSlideTitles(string presentationFile) {
  23. using (PresentationDocument presentationDocument =
  24. PresentationDocument.Open(presentationFile, false))
  25. {
  26. if (presentationDocument == null)
  27. {
  28. throw new ArgumentNullException("presentationDocument");
  29. }
  30. // Get a PresentationPart object from the PresentationDocument object.
  31. PresentationPart presentationPart = presentationDocument.PresentationPart;
  32. if (presentationPart != null &&
  33. presentationPart.Presentation != null)
  34. {
  35. var slideMasterParts = presentationPart.SlideMasterParts;
  36. ColorMap colorMap = null;
  37. Theme theme = null;
  38. foreach (var slideMasterPart in slideMasterParts)
  39. {
  40. if (colorMap != null && theme != null)
  41. {
  42. break;
  43. }
  44. colorMap = slideMasterPart.SlideMaster.ColorMap;
  45. theme = slideMasterPart.ThemePart.Theme;
  46. }
  47. // Get a Presentation object from the PresentationPart object.
  48. Presentation presentation = presentationPart.Presentation;
  49. if (presentation.SlideIdList != null)
  50. {
  51. //获取PPT 一页大小
  52. int x= presentation.SlideSize.Cx;
  53. int y = presentation.SlideSize.Cy;
  54. // Get the title of each slide in the slide order.
  55. // 获取的是几页PPT数量
  56. foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
  57. {
  58. // 获取这一页 PPT 的id
  59. string id = slideId.RelationshipId;
  60. SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
  61. //获取当前页 PPT 的所有元素
  62. GetSlideElement(slidePart , theme , colorMap);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. public static Object GetSlideElement(SlidePart slidePart , Theme theme , ColorMap colorMap)
  69. {
  70. double inchpixel = 96.0, inchpt = 72.0,pxBase=914400.0 , rotBase = 60000;
  71. double pxConvert = inchpixel / pxBase;
  72. if (slidePart == null)
  73. {
  74. throw new ArgumentNullException("presentationDocument");
  75. }
  76. // var aaa= from shape in presentationPart.SlideMasterParts.Descendants<ColorMap>() select shape;
  77. var shaps = from shape in slidePart.Slide.Descendants<Shape>() select shape;
  78. List<string> repeatElement = new List<string>();
  79. var elements = from shape in slidePart.Slide select shape;
  80. List<PPTElement> pptElements = new List<PPTElement>();
  81. var pictures = from shap in elements.FirstOrDefault().Descendants<Picture>() select shap;
  82. var textMaths = from shap in elements.FirstOrDefault().Descendants<TextMath>() select shap;
  83. foreach (var math in textMaths) {
  84. string type = "math";
  85. var mathShape = (Shape) math.Parent.Parent.Parent;
  86. string textColor = "",fillColor="";
  87. var rgbColor = from shape in mathShape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  88. if (rgbColor != null && rgbColor.Count() > 0) {
  89. textColor = rgbColor.FirstOrDefault().Val;
  90. }
  91. fillColor = ShapForColor(mathShape, "Fill", theme);
  92. if (string.IsNullOrEmpty(textColor)) {
  93. textColor = ShapForColor(mathShape, "Font", theme);
  94. }
  95. repeatElement.Add(mathShape.OuterXml);
  96. string elementData = ProcessOMath(math.InnerXml);
  97. ShapeProperties properties = mathShape.ShapeProperties;
  98. if (string.IsNullOrEmpty(fillColor)) {
  99. var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
  100. if (solidFills != null && solidFills.Count() > 0)
  101. {
  102. var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  103. if (propRgbColor != null && propRgbColor.Count() > 0)
  104. {
  105. fillColor = propRgbColor.FirstOrDefault().Val;
  106. }
  107. if (string.IsNullOrEmpty(fillColor))
  108. {
  109. var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
  110. if (colorScheme.Count() > 0) {
  111. fillColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
  112. }
  113. }
  114. }
  115. }
  116. PPTElement element = new PPTElement() { type = type };
  117. if (properties != null && properties.Transform2D != null)
  118. {
  119. element.offx = properties.Transform2D.Offset.X;
  120. element.offy = properties.Transform2D.Offset.Y;
  121. element.extx = properties.Transform2D.Extents.Cx;
  122. element.exty = properties.Transform2D.Extents.Cy;
  123. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  124. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  125. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  126. }
  127. element.text = elementData;
  128. element.fillColor = fillColor;
  129. element.textColor = textColor;
  130. pptElements.Add(element);
  131. }
  132. foreach (var picture in pictures) {
  133. BlipFill blipFill = picture.BlipFill;
  134. var imageRid = blipFill.Blip.Embed.Value;
  135. string type = "picture";
  136. IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
  137. ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
  138. var contentType = imagePart.ContentType;
  139. string base64 = "";
  140. using (var stream = imagePart.GetStream())
  141. {
  142. byte[] buffer = new byte[stream.Length];
  143. stream.Read(buffer, 0, buffer.Length);
  144. stream.Close();
  145. base64 = System.Convert.ToBase64String(buffer);
  146. base64 = "data:" + contentType + ";base64," + base64;
  147. }
  148. ShapeProperties properties = picture.ShapeProperties;
  149. PPTElement element = new PPTElement() { type = type };
  150. if (properties != null && properties.Transform2D != null)
  151. {
  152. element.offx = properties.Transform2D.Offset.X;
  153. element.offy = properties.Transform2D.Offset.Y;
  154. element.extx = properties.Transform2D.Extents.Cx;
  155. element.exty = properties.Transform2D.Extents.Cy;
  156. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  157. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  158. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  159. }
  160. element.text = base64;
  161. pptElements.Add(element);
  162. }
  163. List<Shape> shapes = shaps.ToList();
  164. for (int i= 0; i< shapes.Count;i++ ) {
  165. if (shapes[i].Parent.OuterXml.StartsWith("<mc:Fallback")) {
  166. shapes.RemoveAt(i);
  167. }
  168. }
  169. for (int i = 0; i < shapes.Count; i++)
  170. {
  171. foreach (string enm in repeatElement)
  172. {
  173. if (enm.Equals(shapes[i].OuterXml))
  174. {
  175. shapes.RemoveAt(i);
  176. }
  177. }
  178. }
  179. // 获取文本框 和 图形(形状)
  180. foreach (Shape shapeIn in shapes) {
  181. ShapeProperties properties = shapeIn.ShapeProperties;
  182. var presetGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>() select shap;
  183. string type = "text";
  184. bool complete = false;
  185. if (presetGeometrys.Count() > 0)
  186. {
  187. DocumentFormat.OpenXml.Drawing.PresetGeometry presetGeometry = presetGeometrys.FirstOrDefault();
  188. type = presetGeometry.Preset;
  189. }
  190. PPTElement element = new PPTElement() { type = type };
  191. if (properties != null && properties.Transform2D != null) {
  192. complete = true;
  193. element.offx = properties.Transform2D.Offset.X;
  194. element.offy = properties.Transform2D.Offset.Y;
  195. element.extx = properties.Transform2D.Extents.Cx;
  196. element.exty = properties.Transform2D.Extents.Cy;
  197. element.flipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  198. element.flipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  199. element.rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  200. }
  201. // Console.WriteLine(colorMap.);
  202. string textColor = "", fillColor = "";
  203. var runs = from shap in shapeIn.Descendants<DocumentFormat.OpenXml.Drawing.Run>() select shap;
  204. foreach (var run in runs) {
  205. var runprps = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
  206. var schemeColor = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
  207. if (string.IsNullOrEmpty(textColor)) {
  208. if (schemeColor != null && schemeColor.Count() > 0)
  209. {
  210. foreach (var sc in schemeColor)
  211. {
  212. DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues = sc.Val.Value;
  213. //颜色寻找
  214. textColor = ColorForThemeClr(SchemeColorForColorMap(schemeColorValues, colorMap), theme);
  215. }
  216. }
  217. }
  218. DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
  219. element.text = element.text + run.Text.InnerText;
  220. }
  221. var rgbColor = from shape in shapeIn.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  222. if (string.IsNullOrEmpty(textColor) && rgbColor != null && rgbColor.Count() > 0)
  223. {
  224. textColor = rgbColor.FirstOrDefault().Val;
  225. }
  226. if (string.IsNullOrEmpty(textColor)) {
  227. textColor = ShapForColor(shapeIn, "Font", theme);
  228. }
  229. //if (element.text.Equals("33333"))
  230. //{
  231. // fillColor = ShapForColor(shapeIn, "Fill", theme);
  232. //}
  233. fillColor= ShapForColor(shapeIn, "Fill", theme);
  234. if (string.IsNullOrEmpty(fillColor)) {
  235. var solidFills = from shape in properties.Descendants<DocumentFormat.OpenXml.Drawing.SolidFill>() select shape;
  236. if (solidFills != null && solidFills.Count() > 0)
  237. {
  238. var propRgbColor = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  239. if (propRgbColor != null && propRgbColor.Count() > 0)
  240. {
  241. fillColor = propRgbColor.FirstOrDefault().Val;
  242. }
  243. if (string.IsNullOrEmpty(fillColor))
  244. {
  245. var colorScheme = from shape in solidFills.First().Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
  246. if (colorScheme.Count() > 0)
  247. {
  248. fillColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
  249. }
  250. }
  251. }
  252. }
  253. element.textColor = textColor;
  254. element.fillColor = fillColor;
  255. var custGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
  256. if (custGeometrys.Count() > 0) {
  257. var pathLists = from shap in custGeometrys.First().Descendants<DocumentFormat.OpenXml.Drawing.Path>() select shap;
  258. if (pathLists.Count() > 0) {
  259. var moveTos= pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.MoveTo>();
  260. var lineTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.LineTo>();
  261. var BezierTos = pathLists.First().ChildElements.OfType<DocumentFormat.OpenXml.Drawing.CubicBezierCurveTo>();
  262. }
  263. }
  264. if (complete) {
  265. pptElements.Add(element);
  266. }
  267. // element.text = shapeIn.InnerText;
  268. }
  269. return pptElements;
  270. }
  271. public static string ShapForColor(Shape shape , string colorType ,Theme theme) {
  272. if (shape.ShapeStyle == null)
  273. {
  274. return "";
  275. }
  276. DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle = shape.ShapeStyle;
  277. if (shapeStyle == null)
  278. {
  279. return "";
  280. }
  281. if (colorType.Equals("Effect")) {
  282. DocumentFormat.OpenXml.Drawing.SchemeColor EffectcolorScheme = shapeStyle.EffectReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  283. return ColorForThemeClr(EffectcolorScheme.Val, theme);
  284. }
  285. if (colorType.Equals("Line"))
  286. {
  287. DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  288. return ColorForThemeClr(LinecolorScheme.Val, theme);
  289. }
  290. if (colorType.Equals("Fill"))
  291. {
  292. DocumentFormat.OpenXml.Drawing.SchemeColor FillcolorScheme = shapeStyle.FillReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  293. return ColorForThemeClr(FillcolorScheme.Val, theme);
  294. }
  295. if (colorType.Equals("Font"))
  296. {
  297. DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  298. return ColorForThemeClr(FontcolorScheme.Val, theme);
  299. }
  300. return "";
  301. }
  302. public static string ColorForThemeClr(string scv, Theme theme) {
  303. DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
  304. string colorStr = "";
  305. if (scv.Equals("dk1"))
  306. {
  307. DocumentFormat.OpenXml.Drawing.SystemColor sysColor= colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  308. if (sysColor != null) {
  309. return colorStr = sysColor.LastColor;
  310. }
  311. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  312. if (rgbColor != null)
  313. {
  314. return colorStr = rgbColor.Val;
  315. }
  316. return colorStr;
  317. }
  318. if (scv.Equals("dk2"))
  319. {
  320. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  321. if (sysColor != null)
  322. {
  323. return colorStr = sysColor.LastColor;
  324. }
  325. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  326. if (rgbColor != null)
  327. {
  328. return colorStr = rgbColor.Val;
  329. }
  330. return colorStr;
  331. }
  332. else if (scv.Equals("accent1"))
  333. {
  334. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  335. if (sysColor != null)
  336. {
  337. return colorStr = sysColor.LastColor;
  338. }
  339. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  340. if (rgbColor != null)
  341. {
  342. return colorStr = rgbColor.Val;
  343. }
  344. return colorStr;
  345. }
  346. else if (scv.Equals("accent2"))
  347. {
  348. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  349. if (sysColor != null)
  350. {
  351. return colorStr = sysColor.LastColor;
  352. }
  353. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  354. if (rgbColor != null)
  355. {
  356. return colorStr = rgbColor.Val;
  357. }
  358. return colorStr;
  359. }
  360. else if (scv.Equals("accent3"))
  361. {
  362. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  363. if (sysColor != null)
  364. {
  365. return colorStr = sysColor.LastColor;
  366. }
  367. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  368. if (rgbColor != null)
  369. {
  370. return colorStr = rgbColor.Val;
  371. }
  372. return colorStr;
  373. }
  374. else if (scv.Equals("accent4"))
  375. {
  376. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  377. if (sysColor != null)
  378. {
  379. return colorStr = sysColor.LastColor;
  380. }
  381. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  382. if (rgbColor != null)
  383. {
  384. return colorStr = rgbColor.Val;
  385. }
  386. return colorStr;
  387. }
  388. else if (scv.Equals("accent5"))
  389. {
  390. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  391. if (sysColor != null)
  392. {
  393. return colorStr = sysColor.LastColor;
  394. }
  395. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  396. if (rgbColor != null)
  397. {
  398. return colorStr = rgbColor.Val;
  399. }
  400. return colorStr;
  401. }
  402. else if (scv.Equals("accent6"))
  403. {
  404. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  405. if (sysColor != null)
  406. {
  407. return colorStr = sysColor.LastColor;
  408. }
  409. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  410. if (rgbColor != null)
  411. {
  412. return colorStr = rgbColor.Val;
  413. }
  414. return colorStr;
  415. }
  416. else if (scv.Equals("lt1"))
  417. {
  418. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  419. if (sysColor != null)
  420. {
  421. return colorStr = sysColor.LastColor;
  422. }
  423. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  424. if (rgbColor != null)
  425. {
  426. return colorStr = rgbColor.Val;
  427. }
  428. return colorStr;
  429. }
  430. else if (scv.Equals("lt2"))
  431. {
  432. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  433. if (sysColor != null)
  434. {
  435. return colorStr = sysColor.LastColor;
  436. }
  437. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  438. if (rgbColor != null)
  439. {
  440. return colorStr = rgbColor.Val;
  441. }
  442. return colorStr;
  443. }
  444. else if (scv.Equals("hlink"))
  445. {
  446. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  447. if (sysColor != null)
  448. {
  449. return colorStr = sysColor.LastColor;
  450. }
  451. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  452. if (rgbColor != null)
  453. {
  454. return colorStr = rgbColor.Val;
  455. }
  456. return colorStr;
  457. }
  458. else if (scv.Equals("folHlink"))
  459. {
  460. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  461. if (sysColor != null)
  462. {
  463. return colorStr = sysColor.LastColor;
  464. }
  465. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  466. if (rgbColor != null)
  467. {
  468. return colorStr = rgbColor.Val;
  469. }
  470. return colorStr;
  471. }
  472. return "";
  473. }
  474. public static string SchemeColorForColorMap(DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues , ColorMap colorMap ) {
  475. string scv = schemeColorValues.ToString();
  476. if (scv.Equals("Text1"))
  477. {
  478. return colorMap.Text1.ToString();
  479. }
  480. if (scv.Equals("Text2"))
  481. {
  482. return colorMap.Text2.ToString();
  483. }
  484. else if (scv.Equals("Accent1")) {
  485. return colorMap.Accent1.ToString();
  486. }
  487. else if (scv.Equals("Accent2"))
  488. {
  489. return colorMap.Accent2.ToString();
  490. }
  491. else if (scv.Equals("Accent3"))
  492. {
  493. return colorMap.Accent3.ToString();
  494. }
  495. else if (scv.Equals("Accent4"))
  496. {
  497. return colorMap.Accent4.ToString();
  498. }
  499. else if (scv.Equals("Accent5"))
  500. {
  501. return colorMap.Accent5.ToString();
  502. }
  503. else if (scv.Equals("Accent6"))
  504. {
  505. return colorMap.Accent6.ToString();
  506. }
  507. else if (scv.Equals("Background1"))
  508. {
  509. return colorMap.Background1.ToString();
  510. }
  511. else if (scv.Equals("Background2"))
  512. {
  513. return colorMap.Background2.ToString();
  514. }
  515. else if (scv.Equals("Hyperlink"))
  516. {
  517. return colorMap.Hyperlink.ToString();
  518. }
  519. else if (scv.Equals("FollowedHyperlink"))
  520. {
  521. return colorMap.FollowedHyperlink.ToString();
  522. }
  523. else if (scv.Equals("PhColor"))
  524. {
  525. return "PhColor";
  526. }
  527. else if (scv.Equals("Dark1"))
  528. {
  529. return "dk1";
  530. }
  531. else if (scv.Equals("Dark2"))
  532. {
  533. return "dk2";
  534. }
  535. else if (scv.Equals("Light1"))
  536. {
  537. return "lt1";
  538. }
  539. else if (scv.Equals("Light2"))
  540. {
  541. return "lt2";
  542. }
  543. return null;
  544. }
  545. private static readonly Regex EmojiRegex = new Regex("&#x?[A-Fa-f0-9]+;");
  546. private static string ReplaceInvalidXmlCharacterReferences(string input)
  547. {
  548. if (input.IndexOf("&#") == -1)
  549. return input;
  550. return EmojiRegex.Replace(input, match =>
  551. {
  552. string ncr = match.Value;
  553. uint num;
  554. var frmt = NumberFormatInfo.InvariantInfo;
  555. bool isParsed =
  556. ncr[2] == 'x' ? // the x must be lowercase in XML documents
  557. uint.TryParse(ncr.Substring(3, ncr.Length - 4), NumberStyles.AllowHexSpecifier, frmt, out num) :
  558. uint.TryParse(ncr.Substring(2, ncr.Length - 3), NumberStyles.Integer, frmt, out num);
  559. return isParsed && !XmlConvert.IsXmlChar((char)num) ? "" : ncr;
  560. });
  561. }
  562. public static Dictionary<string,string> stringEncode(string xml) {
  563. Dictionary<string, string> codeValues = new Dictionary<string, string>() ;
  564. Char[] charstr = xml.ToCharArray();
  565. int len = charstr.Length;
  566. for (int i = 0; i< len;i++) {
  567. var stringBuilder = "";
  568. string str = Char.ToString(charstr[i]);
  569. if ( Char.GetUnicodeCategory(charstr[i]) == UnicodeCategory.Surrogate) {
  570. string str1 = Char.ToString(charstr[i+1]);
  571. str = str + str1;
  572. var bytes = Encoding.UTF32.GetBytes(str);
  573. for (var j = 0;j < bytes.Length; j += 2)
  574. {
  575. stringBuilder = bytes[j + 1].ToString("x").PadLeft(2, '0') + bytes[j].ToString("x").PadLeft(2, '0') + stringBuilder;
  576. }
  577. var id = Convert.ToInt64(stringBuilder, 16);
  578. i = i + 1;
  579. string code = id +"";
  580. codeValues.TryAdd(str, code);
  581. }
  582. }
  583. return codeValues;
  584. }
  585. public static string ProcessOMath(string innerXml )
  586. {
  587. Dictionary<string ,string> codeValues = stringEncode(innerXml);
  588. if (codeValues != null) {
  589. foreach (string codeValue in codeValues.Keys) {
  590. innerXml= Regex.Replace(innerXml, codeValue, codeValues[codeValue] );
  591. }
  592. }
  593. XElement element = XElement.Load(new StringReader(innerXml));
  594. List<XElement> elements = element.Elements().ToList();
  595. element =elements.Where(x => x.Name.LocalName.Equals("oMath")).FirstOrDefault();
  596. XslCompiledTransform xslTransform = new XslCompiledTransform();
  597. xslTransform.Load("E:/document/OMML2MML.XSL");
  598. string mathXml = element.ToString();
  599. string officeML = string.Empty;
  600. using (TextReader tr = new StringReader(mathXml))
  601. {
  602. using (XmlReader reader = XmlReader.Create(tr))
  603. {
  604. using (MemoryStream ms = new MemoryStream())
  605. {
  606. XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
  607. settings.ConformanceLevel = ConformanceLevel.Fragment;
  608. settings.OmitXmlDeclaration = true;
  609. settings.Encoding= Encoding.Unicode;
  610. XmlWriter xw = XmlWriter.Create(ms, settings);
  611. xslTransform.Transform(reader, xw);
  612. ms.Seek(0, SeekOrigin.Begin);
  613. using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  614. {
  615. officeML = sr.ReadToEnd();
  616. }
  617. }
  618. }
  619. }
  620. officeML = officeML.Replace("mml:", "");
  621. if (codeValues != null)
  622. {
  623. foreach (string codeValue in codeValues.Keys)
  624. {
  625. officeML = Regex.Replace(officeML, codeValues[codeValue],codeValue);
  626. }
  627. }
  628. return officeML;
  629. }
  630. }
  631. }