PresentationConvert.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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.Drawing;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Xml;
  14. using System.Xml.Linq;
  15. using System.Xml.Xsl;
  16. using TEAMModelOS.SDK.Context.Configuration;
  17. using TEAMModelOS.SDK.Helper.Common.ColorHelper;
  18. using TEAMModelOS.SDK.Module.PowerPointX.Model;
  19. using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
  20. using Point = TEAMModelOS.SDK.Module.PowerPointX.Model.Point;
  21. using Theme = DocumentFormat.OpenXml.Drawing.Theme;
  22. namespace TEAMModelOS.SDK.Module.PowerPointX
  23. {
  24. public class PresentationConvert
  25. {
  26. const double inchpixel = 96.00, inchpt = 72.00, pxBase = 914400.00, ptBase = 12700;
  27. const double rotBase = 60000.00;
  28. /// <summary>
  29. /// 加载PPTX文件
  30. /// </summary>
  31. /// <param name="presentationFile"></param>
  32. /// <returns></returns>
  33. public PPTXInfo LoadPresentation(Stream presentationStream)
  34. {
  35. using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationStream, false))
  36. {
  37. if (presentationDocument == null)
  38. {
  39. throw new ArgumentNullException("presentationDocument");
  40. }
  41. // Get a PresentationPart object from the PresentationDocument object.
  42. var thumbnailPart = presentationDocument.ThumbnailPart;
  43. var contentType = thumbnailPart.ContentType;
  44. string base64 = "";
  45. using (var stream = thumbnailPart.GetStream())
  46. {
  47. byte[] buffer = new byte[stream.Length];
  48. stream.Read(buffer, 0, buffer.Length);
  49. stream.Close();
  50. base64 = System.Convert.ToBase64String(buffer);
  51. base64 = "data:" + contentType + ";base64," + base64;
  52. }
  53. PresentationPart presentationPart = presentationDocument.PresentationPart;
  54. if (presentationPart != null && presentationPart.Presentation != null)
  55. {
  56. // Get a Presentation object from the PresentationPart object.
  57. Presentation presentation = presentationPart.Presentation;
  58. int pageSize = 0;
  59. var slideMasterParts = presentationPart.SlideMasterParts;
  60. ColorMap colorMap = null;
  61. Theme theme = null;
  62. foreach (var slideMasterPart in slideMasterParts)
  63. {
  64. if (colorMap != null && theme != null)
  65. {
  66. break;
  67. }
  68. colorMap = slideMasterPart.SlideMaster.ColorMap;
  69. theme = slideMasterPart.ThemePart.Theme;
  70. }
  71. //获取PPT 一页大小
  72. double x = presentation.SlideSize.Cx* inchpixel/ pxBase;
  73. double y = presentation.SlideSize.Cy * inchpixel / pxBase;
  74. List<PPTXSlide> slides = new List<PPTXSlide>();
  75. if (presentation.SlideIdList != null)
  76. {
  77. // Get the title of each slide in the slide order.
  78. // 获取的是几页PPT数量
  79. foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
  80. {
  81. PPTXSlide slide = new PPTXSlide();
  82. // 获取这一页 PPT 的id
  83. string id = slideId.RelationshipId;
  84. SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
  85. //获取当前页 PPT 的所有元素
  86. slide.Elements=GetSlideElement(slidePart, theme, colorMap);
  87. slides.Add(slide);
  88. pageSize++;
  89. }
  90. }
  91. PPTXInfo info = new PPTXInfo {Slides=slides,Width=x,Height=y ,PageSize= pageSize ,Thumbnail=base64 };
  92. return info;
  93. }
  94. return null;
  95. }
  96. }
  97. /// <summary>
  98. /// 处理一页PPT的元素
  99. /// </summary>
  100. /// <param name="slidePart"></param>
  101. /// <param name="theme"></param>
  102. /// <param name="colorMap"></param>
  103. /// <returns></returns>
  104. public List<PPTXElement> GetSlideElement(SlidePart slidePart, Theme theme, ColorMap colorMap)
  105. {
  106. List<PPTXElement> elements = new List<PPTXElement>();
  107. var shapeTrees = from shap in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>() select shap;
  108. if (shapeTrees.Count() > 0 && shapeTrees.First().ChildElements.Count > 0)
  109. {
  110. OpenXmlElementList openXmlElements = shapeTrees.First().ChildElements;
  111. int index = 0;
  112. foreach (OpenXmlElement element in openXmlElements)
  113. {
  114. PPTXElement pptElement = null;
  115. if (element is DocumentFormat.OpenXml.Presentation.Shape shape)
  116. { //p:sp
  117. pptElement = ShapeConvert(shape, theme, colorMap, index);
  118. }
  119. if (element is DocumentFormat.OpenXml.Presentation.Picture picture)//p:pic
  120. {
  121. pptElement = PictureConvert(picture, theme, colorMap, slidePart, index);
  122. }
  123. if (element is DocumentFormat.OpenXml.AlternateContent content)//mc:alternatecontent
  124. {
  125. pptElement = AlternateContentConvert(content, theme, colorMap,slidePart, index);
  126. }
  127. if (element is DocumentFormat.OpenXml.Presentation.GraphicFrame graphicFrame)//p:graphicFrame
  128. {
  129. pptElement = GraphicFrameConvert(graphicFrame, theme, colorMap, index);
  130. }
  131. if (element is DocumentFormat.OpenXml.Presentation.GroupShape groupShape)//p:grpSp
  132. {
  133. pptElement = GroupShapeConvert(groupShape, theme, colorMap, index);
  134. }
  135. if (element is DocumentFormat.OpenXml.Presentation.ConnectionShape connectionShape) // p:cxnSp
  136. {
  137. pptElement = ConnectionShapeConvert(connectionShape, theme, colorMap, index);
  138. }
  139. if (pptElement != null)
  140. {
  141. index++;
  142. elements.Add(pptElement);
  143. }
  144. }
  145. }
  146. return elements;
  147. }
  148. public PPTXElement GroupShapeConvert(GroupShape groupShape, Theme theme, ColorMap colorMap, int index)
  149. {
  150. return null;
  151. }
  152. public PPTXElement ConnectionShapeConvert(ConnectionShape connectionShape, Theme theme, ColorMap colorMap, int index)
  153. {
  154. string type = "";
  155. string fillColor = "";
  156. ShapeProperties properties = connectionShape.ShapeProperties;
  157. IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
  158. if (presetGeometries.Count() > 0)
  159. {
  160. type = presetGeometries.FirstOrDefault().Preset;
  161. }
  162. PPTXConnector element = new PPTXConnector() { Type = type };
  163. if (properties != null)
  164. {
  165. if (properties.Transform2D != null)
  166. {
  167. element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
  168. element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
  169. element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
  170. element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
  171. element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  172. element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  173. element.Rot = (properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0) / rotBase;
  174. }
  175. var headEnd = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.HeadEnd>() select shap;
  176. var tailEnd = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.TailEnd>() select shap;
  177. if (headEnd != null && headEnd.Count() > 0)
  178. {
  179. element.HeadEnd = headEnd.First().Type;
  180. }
  181. if (tailEnd != null && tailEnd.Count() > 0)
  182. {
  183. element.TailEnd = tailEnd.First().Type;
  184. }
  185. fillColor = GetPropertieColor(properties, theme, colorMap, "Line");
  186. }
  187. if (string.IsNullOrEmpty(fillColor))
  188. {
  189. fillColor = GetShapeStyleColor(connectionShape.ShapeStyle, "Line", theme);
  190. }
  191. element.FillColor = fillColor;
  192. return element;
  193. }
  194. public PPTXElement GraphicFrameConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap, int index)
  195. {
  196. DocumentFormat.OpenXml.Drawing.GraphicData graphicData = graphicFrame.ChildElements.First<DocumentFormat.OpenXml.Drawing.GraphicData>();
  197. if (graphicData != null)
  198. {
  199. OpenXmlElement element = graphicData.ChildElements.First();
  200. if (element != null)
  201. {
  202. if (element is DocumentFormat.OpenXml.Drawing.Table table)
  203. {
  204. return TableConvert(graphicFrame, theme, colorMap);
  205. }
  206. if (element is DocumentFormat.OpenXml.Drawing.Chart chart)
  207. {
  208. //return ChartConvert(graphicFrame, theme, colorMap);
  209. }
  210. if (element is DocumentFormat.OpenXml.Drawing.Diagram diagram)
  211. {
  212. //return DiagramConvert(graphicFrame, theme, colorMap);
  213. }
  214. }
  215. }
  216. return null;
  217. }
  218. public PPTXElement TableConvert(GraphicFrame graphicFrame, Theme theme, ColorMap colorMap)
  219. {
  220. return null;
  221. }
  222. /// <summary>
  223. /// 处理普通形状的元素
  224. /// </summary>
  225. /// <param name="shape"></param>
  226. /// <returns></returns>
  227. public PPTXElement ShapeConvert(Shape shape, Theme theme, ColorMap colorMap, int index)
  228. {
  229. // shape.base
  230. //文字颜色
  231. string textColor = "";
  232. //填充颜色
  233. string fillColor = "";
  234. //填充颜色
  235. string lineColor = "";
  236. // 形状类型
  237. string type = "rect";
  238. //字体大小
  239. int fontSize = 1800;
  240. //是否闭合形状
  241. bool complete = true;
  242. PPTXContainer element = new PPTXContainer() ;
  243. ShapeProperties properties = shape.ShapeProperties;
  244. IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
  245. IEnumerable<DocumentFormat.OpenXml.Drawing.CustomGeometry> customGeometries = GetCustomGeometry(properties);
  246. if (presetGeometries.Count() > 0)
  247. {
  248. type = presetGeometries.FirstOrDefault().Preset;
  249. var adjusts = from shap in presetGeometries.First().Descendants<DocumentFormat.OpenXml.Drawing.AdjustValueList>() select shap;
  250. if (adjusts.Count() > 0)
  251. {
  252. var elements = adjusts.First().ChildElements;
  253. foreach (var xmlElement in elements)
  254. {
  255. if (xmlElement is DocumentFormat.OpenXml.Drawing.ShapeGuide shapeGuide)
  256. {
  257. PPTXShapeGuide PPTXShapeGuide = new PPTXShapeGuide { Name = shapeGuide.Name, Fmla = shapeGuide.Formula };
  258. element.ShapeGuides.Add(PPTXShapeGuide);
  259. }
  260. }
  261. }
  262. }
  263. if (customGeometries.Count() > 0)
  264. {
  265. type = "custom";
  266. }
  267. element.Type = type;
  268. DocumentFormat.OpenXml.Drawing.Transform2D transform2D = GetTransform2D(properties);
  269. if (transform2D != null)
  270. {
  271. element.Close = complete;
  272. element.OffX = transform2D.Offset.X * inchpixel / pxBase;
  273. element.OffY = transform2D.Offset.Y * inchpixel / pxBase;
  274. element.ExtX = transform2D.Extents.Cx * inchpixel / pxBase;
  275. element.ExtY = transform2D.Extents.Cy * inchpixel / pxBase;
  276. element.FlipH = transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(transform2D.HorizontalFlip) : false;
  277. element.FlipV = transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(transform2D.VerticalFlip) : false;
  278. element.Rot = transform2D.Rotation != null ? transform2D.Rotation.Value : 0;
  279. }
  280. var nofill = GetNoFill(properties);
  281. if (nofill.Count() > 0)
  282. {
  283. element.Fill = false;
  284. }
  285. element.Index = index;
  286. ///获取runs的文本及颜色信息
  287. var bodyProp = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.BodyProperties>() select shap;
  288. if (bodyProp.Count() > 0)
  289. {
  290. DocumentFormat.OpenXml.Drawing.BodyProperties bdpros = bodyProp.First<DocumentFormat.OpenXml.Drawing.BodyProperties>();
  291. if (bdpros.Vertical != null && !bdpros.Vertical.Value.Equals("horz"))
  292. {
  293. element.TextLayout = "vert";
  294. }
  295. //0顶 1中 2底
  296. if (bdpros.Anchor != null)
  297. {
  298. element.Align = bdpros.Anchor.Value.ToString();
  299. }
  300. //bodyAlign = element.Align == null || string.IsNullOrEmpty(bdpros.Ancestors) ? "" : element.Align;
  301. //element.Wrap == null || string.IsNullOrEmpty(element.Wrap) ? "none" : element.Wrap;
  302. }
  303. var paragraphs = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>() select shap;
  304. foreach (var paragrap in paragraphs)
  305. {
  306. string textAling = "l";
  307. if (paragrap.ParagraphProperties != null)
  308. {
  309. textAling = paragrap.ParagraphProperties.Alignment;
  310. }
  311. //if (string.IsNullOrEmpty(textAling))
  312. //{
  313. //if (string.IsNullOrEmpty(bodyAlign))
  314. //{
  315. // textAling = "l";
  316. //}
  317. //else
  318. //{
  319. // textAling = bodyAlign;
  320. //}
  321. //}
  322. List<PPTXText> texts = new List<PPTXText>();
  323. var runs = from shap in paragrap.Descendants<DocumentFormat.OpenXml.Drawing.Run>() select shap;
  324. foreach (var run in runs)
  325. {
  326. PPTXText text = new PPTXText();
  327. var runprps = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
  328. DocumentFormat.OpenXml.Drawing.RunProperties runProperties = runprps.FirstOrDefault();
  329. if (runProperties != null)
  330. {
  331. if (runProperties.FontSize != null)
  332. {
  333. text.FontSize = runProperties.FontSize;
  334. }
  335. if (runProperties.Italic != null)
  336. {
  337. text.FontItalic = runProperties.Italic;
  338. }
  339. if (runProperties.Bold != null)
  340. {
  341. text.FontBold = runProperties.Bold;
  342. }
  343. //if (runProperties.Italic != null)
  344. //{
  345. // text.FontBold = runProperties.Bold;
  346. //}
  347. if (runProperties.Underline != null)
  348. {
  349. text.FontLine = (int)runProperties.Underline.Value;
  350. }
  351. }
  352. var schemeColor = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
  353. var rgbColors = from shap in run.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
  354. textColor = GetSchemeColor(schemeColor, theme, colorMap);
  355. if (string.IsNullOrEmpty(textColor))
  356. {
  357. text.TextColor = textColor = GetRgbColor(rgbColors);
  358. }
  359. if (string.IsNullOrEmpty(textColor))
  360. {
  361. textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
  362. }
  363. if (string.IsNullOrEmpty(textColor))
  364. {
  365. textColor = "FFFFFF";
  366. }
  367. text.FontSize = fontSize;
  368. text.Text = run.InnerText;
  369. text.TextAlign = textAling;
  370. texts.Add(text);
  371. }
  372. if (texts.Count() > 0)
  373. {
  374. PPTXParagraph paragraph = new PPTXParagraph();
  375. paragraph.Texts = texts;
  376. element.Paragraphs.Add(paragraph);
  377. }
  378. }
  379. if (string.IsNullOrEmpty(textColor))
  380. {
  381. textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
  382. }
  383. if (string.IsNullOrEmpty(fillColor))
  384. {
  385. fillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
  386. }
  387. if (string.IsNullOrEmpty(lineColor))
  388. {
  389. lineColor = GetPropertieColor(properties, theme, colorMap, "Line");
  390. }
  391. if (string.IsNullOrEmpty(lineColor))
  392. {
  393. lineColor = GetShapeStyleColor(shape.ShapeStyle, "Line", theme);
  394. }
  395. if (string.IsNullOrEmpty(fillColor))
  396. {
  397. fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
  398. }
  399. element.LineColor = lineColor;
  400. element.FillColor = fillColor;
  401. var custGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
  402. if (custGeometrys.Count() > 0)
  403. {
  404. var pathLists = from shap in custGeometrys.First().Descendants<DocumentFormat.OpenXml.Drawing.Path>() select shap;
  405. if (pathLists.Count() > 0)
  406. {
  407. element.Close = false;
  408. OpenXmlElementList elements = pathLists.First().ChildElements;
  409. foreach (OpenXmlElement xmlElement in elements)
  410. {
  411. //起点
  412. if (xmlElement is DocumentFormat.OpenXml.Drawing.MoveTo moveTo)
  413. {
  414. DocumentFormat.OpenXml.Drawing.Point point = moveTo.Point;
  415. PPTXMoveToPath moveToPath = new PPTXMoveToPath { Type = "MoveTo" };
  416. moveToPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
  417. element.Paths.Add(moveToPath);
  418. }
  419. //连线
  420. if (xmlElement is DocumentFormat.OpenXml.Drawing.LineTo linTo)
  421. {
  422. DocumentFormat.OpenXml.Drawing.Point point = linTo.Point;
  423. PPTXLineToPath lineToPath = new PPTXLineToPath { Type = "LineTo" };
  424. lineToPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
  425. element.Paths.Add(lineToPath);
  426. }
  427. //三次贝塞尔曲线
  428. if (xmlElement is DocumentFormat.OpenXml.Drawing.CubicBezierCurveTo cubicBezierCurveTo)
  429. {
  430. OpenXmlElementList list = cubicBezierCurveTo.ChildElements;
  431. PPTXCubicBezPath cubicBezPath = new PPTXCubicBezPath { Type = "CubicBez" };
  432. foreach (var ls in list)
  433. {
  434. if (ls is DocumentFormat.OpenXml.Drawing.Point point)
  435. {
  436. cubicBezPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
  437. }
  438. }
  439. element.Paths.Add(cubicBezPath);
  440. }
  441. //二次贝塞尔曲线
  442. if (xmlElement is DocumentFormat.OpenXml.Drawing.QuadraticBezierCurveTo quadraticBezierCurveTo)
  443. {
  444. OpenXmlElementList list = quadraticBezierCurveTo.ChildElements;
  445. PPTXQuadBezPath quadBezPath = new PPTXQuadBezPath { Type = "QuadBez" };
  446. foreach (var ls in list)
  447. {
  448. if (ls is DocumentFormat.OpenXml.Drawing.Point point)
  449. {
  450. quadBezPath.Pts.Add(new Point { X = double.Parse(point.X) * inchpixel / pxBase, Y = double.Parse(point.Y) * inchpixel / pxBase });
  451. }
  452. }
  453. element.Paths.Add(quadBezPath);
  454. }
  455. //处理曲线
  456. if (xmlElement is DocumentFormat.OpenXml.Drawing.ArcTo arcTO)
  457. {
  458. PPTXArcToPath arcToPath = new PPTXArcToPath() { WidthRadius = arcTO.WidthRadius, HeightRadius = arcTO.HeightRadius, StartAngle = arcTO.StartAngle, SwingAngle = arcTO.SwingAngle };
  459. element.Paths.Add(arcToPath);
  460. }
  461. ///判断路径是否闭合
  462. if (xmlElement is DocumentFormat.OpenXml.Drawing.CloseShapePath close)
  463. {
  464. if (close != null)
  465. {
  466. element.Close = true;
  467. }
  468. }
  469. }
  470. }
  471. }
  472. return element;
  473. }
  474. /// <summary>
  475. /// 处理图片的元素
  476. /// </summary>
  477. /// <param name="picture"></param>
  478. /// <returns></returns>
  479. public PPTXElement PictureConvert(Picture picture, Theme theme, ColorMap colorMap, SlidePart slidePart, int index)
  480. {
  481. BlipFill blipFill = picture.BlipFill;
  482. var imageRid = blipFill.Blip.Embed.Value;
  483. IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
  484. ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
  485. var contentType = imagePart.ContentType;
  486. string base64 = "";
  487. using (var stream = imagePart.GetStream())
  488. {
  489. byte[] buffer = new byte[stream.Length];
  490. stream.Read(buffer, 0, buffer.Length);
  491. stream.Close();
  492. base64 = System.Convert.ToBase64String(buffer);
  493. base64 = "data:" + contentType + ";base64," + base64;
  494. }
  495. ShapeProperties properties = picture.ShapeProperties;
  496. PPTXPicture element = new PPTXPicture();
  497. if (properties != null && properties.Transform2D != null)
  498. {
  499. element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
  500. element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
  501. element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
  502. element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
  503. element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  504. element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  505. element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  506. }
  507. element.FillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
  508. element.LineColor = GetPropertieColor(properties, theme, colorMap, "Line");
  509. element.Data = base64;
  510. element.Index = index;
  511. return element;
  512. }
  513. /// <summary>
  514. /// 处理兼容元素
  515. /// </summary>
  516. /// <param name="picture"></param>
  517. /// <returns></returns>
  518. public PPTXElement AlternateContentConvert(AlternateContent content, Theme theme, ColorMap colorMap, SlidePart slidePart, int index)
  519. {
  520. var choices = from contentChoice in content.Descendants<AlternateContentChoice>() select contentChoice;
  521. if (choices.Count() > 0) {
  522. OpenXmlElement element = choices.First().ChildElements.First();
  523. if (element != null)
  524. {
  525. if (element is DocumentFormat.OpenXml.Presentation.Shape shape)
  526. {
  527. var textMaths = from shap in shape.Descendants<TextMath>() select shap;
  528. if (textMaths.Count() > 0)
  529. {
  530. // return MathConvertMathML(shape, theme, colorMap, index);
  531. return MathConvertImage(content, theme, colorMap,slidePart, index);
  532. }
  533. }
  534. }
  535. }
  536. //AlternateContentChoice choice = content.ChildElements.First<AlternateContentChoice>();
  537. return null;
  538. }
  539. public PPTXElement MathConvertImage(AlternateContent content, Theme theme, ColorMap colorMap, SlidePart slidePart, int index) {
  540. var fallbacks = from contentChoice in content.Descendants<AlternateContentFallback>() select contentChoice;
  541. if (fallbacks.Count() > 0)
  542. {
  543. OpenXmlElement xmlElement = fallbacks.First().ChildElements.First();
  544. if (xmlElement != null)
  545. {
  546. if (xmlElement is DocumentFormat.OpenXml.Presentation.Shape shape)
  547. {
  548. var blipFills = from pic in shape.Descendants<DocumentFormat.OpenXml.Drawing.BlipFill>() select pic;
  549. if (blipFills.Count() > 0) {
  550. var imageRid= blipFills.First().Blip.Embed.Value;
  551. IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
  552. ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
  553. var contentType = imagePart.ContentType;
  554. string base64 = "";
  555. using (var stream = imagePart.GetStream())
  556. {
  557. byte[] buffer = new byte[stream.Length];
  558. stream.Read(buffer, 0, buffer.Length);
  559. stream.Close();
  560. base64 = System.Convert.ToBase64String(buffer);
  561. base64 = "data:" + contentType + ";base64," + base64;
  562. }
  563. ShapeProperties properties = shape.ShapeProperties;
  564. PPTXPicture element = new PPTXPicture();
  565. if (properties != null && properties.Transform2D != null)
  566. {
  567. element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
  568. element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
  569. element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
  570. element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
  571. element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  572. element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  573. element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  574. }
  575. element.FillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
  576. element.LineColor = GetPropertieColor(properties, theme, colorMap, "Line");
  577. element.Data = base64;
  578. element.Index = index;
  579. return element;
  580. }
  581. }
  582. }
  583. }
  584. return null;
  585. }
  586. /// <summary>
  587. /// 处理公式
  588. /// </summary>
  589. /// <param name="shape"></param>
  590. /// <param name="theme"></param>
  591. /// <param name="colorMap"></param>
  592. /// <returns></returns>
  593. public PPTXElement MathConvertMathML(DocumentFormat.OpenXml.Presentation.Shape shape, Theme theme, ColorMap colorMap, int index)
  594. {
  595. //文字颜色
  596. string textColor = "";
  597. //填充颜色
  598. string fillColor = "";
  599. // 形状类型
  600. string type = "math";
  601. //字体大小
  602. int fontSize = 1800;
  603. //是否闭合形状
  604. bool complete = true;
  605. ShapeProperties properties = shape.ShapeProperties;
  606. //IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> presetGeometries = GetPresetGeometry(properties);
  607. //if (presetGeometries.Count() > 0)
  608. //{
  609. // type = presetGeometries.FirstOrDefault().Preset;
  610. //}
  611. DocumentFormat.OpenXml.Drawing.Transform2D transform2D = GetTransform2D(properties);
  612. PPTXContainer element = new PPTXContainer() { Type = type };
  613. if (transform2D != null)
  614. {
  615. element.Close = complete;
  616. element.OffX = properties.Transform2D.Offset.X * inchpixel / pxBase;
  617. element.OffY = properties.Transform2D.Offset.Y * inchpixel / pxBase;
  618. element.ExtX = properties.Transform2D.Extents.Cx * inchpixel / pxBase;
  619. element.ExtY = properties.Transform2D.Extents.Cy * inchpixel / pxBase;
  620. element.FlipH = properties.Transform2D.HorizontalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.HorizontalFlip) : false;
  621. element.FlipV = properties.Transform2D.VerticalFlip != null ? BooleanValue.ToBoolean(properties.Transform2D.VerticalFlip) : false;
  622. element.Rot = properties.Transform2D.Rotation != null ? properties.Transform2D.Rotation.Value : 0;
  623. }
  624. textColor = GetPropertieColor(properties, theme, colorMap, "Font");
  625. var nofill = GetNoFill(properties);
  626. if (nofill.Count() > 0)
  627. {
  628. element.Fill = false;
  629. }
  630. //获取行内样式
  631. if (string.IsNullOrEmpty(textColor))
  632. {
  633. var rgbColor = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shap;
  634. textColor = GetRgbColor(rgbColor);
  635. if (string.IsNullOrEmpty(textColor))
  636. {
  637. var schemes = from shap in shape.TextBody.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shap;
  638. textColor = GetSchemeColor(schemes, theme, colorMap);
  639. }
  640. }
  641. if (string.IsNullOrEmpty(textColor))
  642. {
  643. textColor = GetShapeStyleColor(shape.ShapeStyle, "Font", theme);
  644. }
  645. {
  646. textColor = "FFFFFF";
  647. }
  648. if (string.IsNullOrEmpty(fillColor))
  649. {
  650. fillColor = GetPropertieColor(properties, theme, colorMap, "Fill");
  651. }
  652. if (string.IsNullOrEmpty(fillColor))
  653. {
  654. fillColor = GetShapeStyleColor(shape.ShapeStyle, "Fill", theme);
  655. }
  656. string lineColor = GetPropertieColor(properties, theme, colorMap, "Line");
  657. if (string.IsNullOrEmpty(lineColor))
  658. {
  659. lineColor = GetShapeStyleColor(shape.ShapeStyle, "Line", theme);
  660. }
  661. element.LineColor = lineColor;
  662. element.FillColor = fillColor;
  663. var textMaths = from shap in shape.Descendants<TextMath>() select shap;
  664. var math = textMaths.First<TextMath>();
  665. /// TODO 文字大小
  666. //XElement xElement= XElement.Load(math.InnerXml);
  667. // var aa = xElement.Element("a:rPr");
  668. //var runProperties = from shap in shape.Descendants<DocumentFormat.OpenXml.Drawing.RunProperties>() select shap;
  669. //if (runProperties.Count() > 0) {
  670. // DocumentFormat.OpenXml.Drawing.RunProperties propert= runProperties.First<DocumentFormat.OpenXml.Drawing.RunProperties>();
  671. // fontSize = propert.FontSize!=null&& propert.FontSize.Value > 0 ? propert.FontSize.Value : fontSize;
  672. //}
  673. string elementData = ProcessOMath(math.InnerXml);
  674. PPTXParagraph paragraph = new PPTXParagraph();
  675. paragraph.Texts.Add(new PPTXText { TextColor = textColor, Text = elementData, FontSize = fontSize });
  676. element.Paragraphs.Add(paragraph);
  677. element.Index = index;
  678. return element;
  679. }
  680. /// <summary>
  681. /// 处理元素的Transform2D属性
  682. /// </summary>
  683. /// <param name="properties"></param>
  684. /// <returns></returns>
  685. public DocumentFormat.OpenXml.Drawing.Transform2D GetTransform2D(ShapeProperties properties)
  686. {
  687. if (properties != null)
  688. {
  689. return properties.Transform2D;
  690. }
  691. return null;
  692. }
  693. /// <summary>
  694. /// 处理元素的Transform2D 系统默认的元素
  695. /// </summary>
  696. /// <param name="properties"></param>
  697. /// <returns></returns>
  698. public IEnumerable<DocumentFormat.OpenXml.Drawing.PresetGeometry> GetPresetGeometry(ShapeProperties properties)
  699. {
  700. if (properties != null)
  701. {
  702. var presetGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>() select shap;
  703. return presetGeometrys;
  704. }
  705. return null;
  706. }
  707. public IEnumerable<DocumentFormat.OpenXml.Drawing.CustomGeometry> GetCustomGeometry(ShapeProperties properties)
  708. {
  709. if (properties != null)
  710. {
  711. var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.CustomGeometry>() select shap;
  712. return customGeometrys;
  713. }
  714. return null;
  715. }
  716. /// <summary>
  717. /// 处理是否填充
  718. /// </summary>
  719. /// <param name="properties"></param>
  720. /// <returns></returns>
  721. public IEnumerable<DocumentFormat.OpenXml.Drawing.NoFill> GetNoFill(ShapeProperties properties)
  722. {
  723. if (properties != null)
  724. {
  725. var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.NoFill>() select shap;
  726. return customGeometrys;
  727. }
  728. return null;
  729. }
  730. ///// <summary>
  731. ///// 获取属性填充色
  732. ///// </summary>
  733. ///// <param name="properties"></param>
  734. ///// <returns></returns>
  735. //public IEnumerable<DocumentFormat.OpenXml.Drawing.SolidFill> GetPropFill(ShapeProperties properties)
  736. //{
  737. // if (properties != null)
  738. // {
  739. // var solidFills = properties.ChildElements
  740. // .Where(x => x is DocumentFormat.OpenXml.Drawing.SolidFill);
  741. // if (solidFills.Count() > 0) {
  742. // DocumentFormat.OpenXml.Drawing.SolidFill solidFills.ToList().First();
  743. // }
  744. // }
  745. // return null;
  746. //}
  747. /// <summary>
  748. /// 获取轮廓颜色填充
  749. /// </summary>
  750. /// <param name="properties"></param>
  751. /// <returns></returns>
  752. public IEnumerable<DocumentFormat.OpenXml.Drawing.NoFill> GetPropLineFill(ShapeProperties properties)
  753. {
  754. if (properties != null)
  755. {
  756. var customGeometrys = from shap in properties.Descendants<DocumentFormat.OpenXml.Drawing.NoFill>() select shap;
  757. return customGeometrys;
  758. }
  759. return null;
  760. }
  761. /// <summary>
  762. /// 处理元素属性的填充色
  763. /// </summary>
  764. /// <param name="properties"></param>
  765. /// <returns></returns>
  766. public string GetPropertieColor(ShapeProperties properties, Theme theme, ColorMap colorMap, string type)
  767. {
  768. if (properties != null)
  769. {
  770. var cidElement = properties.ChildElements;
  771. foreach (var element in cidElement)
  772. {
  773. if (type.Equals("Fill") && element is DocumentFormat.OpenXml.Drawing.SolidFill solidFill)
  774. {
  775. string fillColor = "";
  776. var propRgbColor = from shape in solidFill.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  777. fillColor = GetRgbColor(propRgbColor);
  778. if (string.IsNullOrEmpty(fillColor))
  779. {
  780. var colorScheme = from shape in solidFill.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
  781. fillColor = GetSchemeColor(colorScheme, theme, colorMap);
  782. }
  783. return fillColor;
  784. }
  785. if (type.Equals("Line") && element is DocumentFormat.OpenXml.Drawing.Outline outline)
  786. {
  787. string lineColor = "";
  788. var propRgbColor = from shape in outline.Descendants<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>() select shape;
  789. lineColor = GetRgbColor(propRgbColor);
  790. if (string.IsNullOrEmpty(lineColor))
  791. {
  792. var colorScheme = from shape in outline.Descendants<DocumentFormat.OpenXml.Drawing.SchemeColor>() select shape;
  793. lineColor = GetSchemeColor(colorScheme, theme, colorMap);
  794. }
  795. return lineColor;
  796. }
  797. }
  798. }
  799. return "";
  800. }
  801. /// <summary>
  802. /// 获取ShapeStyle样式表 各种属性 的颜色
  803. /// </summary>
  804. /// <param name="shapeStyle"></param>
  805. /// <param name="colorType"></param>
  806. /// <param name="theme"></param>
  807. /// <returns></returns>
  808. public string GetShapeStyleColor(DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle, string colorType, Theme theme)
  809. {
  810. if (shapeStyle == null)
  811. {
  812. return "";
  813. }
  814. // 效果颜色
  815. if (colorType.Equals("Effect"))
  816. {
  817. DocumentFormat.OpenXml.Drawing.SchemeColor EffectcolorScheme = shapeStyle.EffectReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  818. //return ColorForThemeClr(EffectcolorScheme.Val, theme);
  819. return GetColorSchemeExtInfo(EffectcolorScheme, ColorForThemeClr(EffectcolorScheme.Val, theme));
  820. }
  821. // 轮廓颜色
  822. if (colorType.Equals("Line"))
  823. {
  824. DocumentFormat.OpenXml.Drawing.SchemeColor LinecolorScheme = shapeStyle.LineReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  825. //return ColorForThemeClr(LinecolorScheme.Val, theme);
  826. return GetColorSchemeExtInfo(LinecolorScheme, ColorForThemeClr(LinecolorScheme.Val, theme));
  827. }
  828. // 填充色
  829. if (colorType.Equals("Fill"))
  830. {
  831. DocumentFormat.OpenXml.Drawing.SchemeColor FillcolorScheme = shapeStyle.FillReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  832. // return ColorForThemeClr(FillcolorScheme.Val, theme);
  833. return GetColorSchemeExtInfo(FillcolorScheme, ColorForThemeClr(FillcolorScheme.Val, theme));
  834. }
  835. // 字体颜色
  836. if (colorType.Equals("Font"))
  837. {
  838. DocumentFormat.OpenXml.Drawing.SchemeColor FontcolorScheme = shapeStyle.FontReference.ChildElements.First<DocumentFormat.OpenXml.Drawing.SchemeColor>();
  839. //return ColorForThemeClr(FontcolorScheme.Val, theme);
  840. return GetColorSchemeExtInfo(FontcolorScheme, ColorForThemeClr(FontcolorScheme.Val, theme));
  841. }
  842. return "";
  843. }
  844. /// <summary>
  845. /// 获取 声明颜色的色调扩展
  846. /// </summary>
  847. /// <param name="scheme"></param>
  848. /// <param name="schemeColor"></param>
  849. /// <returns></returns>
  850. public string GetColorSchemeExtInfo(DocumentFormat.OpenXml.Drawing.SchemeColor scheme, string schemeColor)
  851. {
  852. var childeElemet = scheme.ChildElements;
  853. int lumModVal = 0;
  854. int lumOffVal = 0;
  855. int tintVal = 0;
  856. int shadeVal = 0;
  857. if (childeElemet.Count() > 0)
  858. {
  859. foreach (var element in childeElemet)
  860. {
  861. if (element is DocumentFormat.OpenXml.Drawing.LuminanceModulation lumMod)
  862. {
  863. lumModVal = lumMod.Val;
  864. }
  865. if (element is DocumentFormat.OpenXml.Drawing.LuminanceOffset lumOff)
  866. {
  867. lumOffVal = lumOff.Val;
  868. }
  869. if (element is DocumentFormat.OpenXml.Drawing.Luminance lum)
  870. {
  871. }
  872. //与白色中和
  873. if (element is DocumentFormat.OpenXml.Drawing.Tint tint)
  874. {
  875. tintVal = tint.Val;
  876. }
  877. //与黑色中和
  878. if (element is DocumentFormat.OpenXml.Drawing.Shade shade)
  879. {
  880. shadeVal = shade.Val;
  881. }
  882. }
  883. }
  884. if (lumModVal > 0 || lumOffVal > 0)
  885. {
  886. Color color = ColorTranslator.FromHtml("#" + schemeColor);
  887. color = ColorHelper.GetColorLumModAndLumOff(color, lumModVal, lumOffVal);
  888. schemeColor = ColorTranslator.ToHtml(color).Replace("#", "");
  889. }
  890. if (tintVal > 0)
  891. {
  892. schemeColor = ColorTranslator.ToHtml(ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), tintVal / 100000.0000, "Tint")).Replace("#", "");
  893. }
  894. if (shadeVal > 0)
  895. {
  896. schemeColor = ColorTranslator.ToHtml(ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), shadeVal / 100000.0000, "Shade")).Replace("#", "");
  897. }
  898. return schemeColor;
  899. }
  900. /// <summary>
  901. /// 获取声明式颜色
  902. /// </summary>
  903. /// <param name="colorScheme"></param>
  904. /// <returns></returns>
  905. public string GetSchemeColor(IEnumerable<DocumentFormat.OpenXml.Drawing.SchemeColor> colorScheme, Theme theme, ColorMap colorMap)
  906. {
  907. string schemeColor = "";
  908. if (colorScheme.Count() > 0)
  909. {
  910. schemeColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
  911. var scheme = colorScheme.First();
  912. schemeColor = GetColorSchemeExtInfo(scheme, schemeColor);
  913. }
  914. return schemeColor;
  915. }
  916. /// <summary>
  917. /// 获取RGBColor
  918. /// </summary>
  919. /// <param name="rgbColors"></param>
  920. /// <returns></returns>
  921. public string GetRgbColor(IEnumerable<DocumentFormat.OpenXml.Drawing.RgbColorModelHex> rgbColors)
  922. {
  923. string rgbColor = "";
  924. if (rgbColors.Count() > 0)
  925. {
  926. rgbColor = rgbColors.FirstOrDefault().Val;
  927. }
  928. return rgbColor;
  929. }
  930. /// <summary>
  931. /// 在主题Theme中获取颜色
  932. /// </summary>
  933. /// <param name="scv"></param>
  934. /// <param name="theme"></param>
  935. /// <returns></returns>
  936. public static string ColorForThemeClr(string scv, Theme theme)
  937. {
  938. DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
  939. string colorStr = "";
  940. if (scv.Equals("dk1"))
  941. {
  942. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  943. if (sysColor != null)
  944. {
  945. return sysColor.LastColor;
  946. }
  947. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  948. if (rgbColor != null)
  949. {
  950. return rgbColor.Val;
  951. }
  952. return colorStr;
  953. }
  954. if (scv.Equals("dk2"))
  955. {
  956. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  957. if (sysColor != null)
  958. {
  959. return sysColor.LastColor;
  960. }
  961. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  962. if (rgbColor != null)
  963. {
  964. return rgbColor.Val;
  965. }
  966. return colorStr;
  967. }
  968. else if (scv.Equals("accent1"))
  969. {
  970. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  971. if (sysColor != null)
  972. {
  973. return sysColor.LastColor;
  974. }
  975. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  976. if (rgbColor != null)
  977. {
  978. return rgbColor.Val;
  979. }
  980. return colorStr;
  981. }
  982. else if (scv.Equals("accent2"))
  983. {
  984. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  985. if (sysColor != null)
  986. {
  987. return sysColor.LastColor;
  988. }
  989. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  990. if (rgbColor != null)
  991. {
  992. return rgbColor.Val;
  993. }
  994. return colorStr;
  995. }
  996. else if (scv.Equals("accent3"))
  997. {
  998. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  999. if (sysColor != null)
  1000. {
  1001. return sysColor.LastColor;
  1002. }
  1003. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1004. if (rgbColor != null)
  1005. {
  1006. return rgbColor.Val;
  1007. }
  1008. return colorStr;
  1009. }
  1010. else if (scv.Equals("accent4"))
  1011. {
  1012. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1013. if (sysColor != null)
  1014. {
  1015. return sysColor.LastColor;
  1016. }
  1017. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1018. if (rgbColor != null)
  1019. {
  1020. return rgbColor.Val;
  1021. }
  1022. return colorStr;
  1023. }
  1024. else if (scv.Equals("accent5"))
  1025. {
  1026. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1027. if (sysColor != null)
  1028. {
  1029. return sysColor.LastColor;
  1030. }
  1031. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1032. if (rgbColor != null)
  1033. {
  1034. return rgbColor.Val;
  1035. }
  1036. return colorStr;
  1037. }
  1038. else if (scv.Equals("accent6"))
  1039. {
  1040. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1041. if (sysColor != null)
  1042. {
  1043. return sysColor.LastColor;
  1044. }
  1045. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1046. if (rgbColor != null)
  1047. {
  1048. return rgbColor.Val;
  1049. }
  1050. return colorStr;
  1051. }
  1052. else if (scv.Equals("lt1"))
  1053. {
  1054. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1055. if (sysColor != null)
  1056. {
  1057. return sysColor.LastColor;
  1058. }
  1059. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1060. if (rgbColor != null)
  1061. {
  1062. return rgbColor.Val;
  1063. }
  1064. return colorStr;
  1065. }
  1066. else if (scv.Equals("lt2"))
  1067. {
  1068. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1069. if (sysColor != null)
  1070. {
  1071. return sysColor.LastColor;
  1072. }
  1073. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1074. if (rgbColor != null)
  1075. {
  1076. return rgbColor.Val;
  1077. }
  1078. return colorStr;
  1079. }
  1080. else if (scv.Equals("hlink"))
  1081. {
  1082. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1083. if (sysColor != null)
  1084. {
  1085. return sysColor.LastColor;
  1086. }
  1087. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1088. if (rgbColor != null)
  1089. {
  1090. return rgbColor.Val;
  1091. }
  1092. return colorStr;
  1093. }
  1094. else if (scv.Equals("folHlink"))
  1095. {
  1096. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1097. if (sysColor != null)
  1098. {
  1099. return sysColor.LastColor;
  1100. }
  1101. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1102. if (rgbColor != null)
  1103. {
  1104. return rgbColor.Val;
  1105. }
  1106. return colorStr;
  1107. }
  1108. return "";
  1109. }
  1110. /// <summary>
  1111. /// 按照ColorMap 去寻找对应的颜色
  1112. /// </summary>
  1113. /// <param name="schemeColorValues"></param>
  1114. /// <param name="colorMap"></param>
  1115. /// <returns></returns>
  1116. public static string SchemeColorForColorMap(DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues, ColorMap colorMap)
  1117. {
  1118. string scv = schemeColorValues.ToString();
  1119. if (scv.Equals("Text1"))
  1120. {
  1121. return colorMap.Text1.ToString();
  1122. }
  1123. if (scv.Equals("Text2"))
  1124. {
  1125. return colorMap.Text2.ToString();
  1126. }
  1127. else if (scv.Equals("Accent1"))
  1128. {
  1129. return colorMap.Accent1.ToString();
  1130. }
  1131. else if (scv.Equals("Accent2"))
  1132. {
  1133. return colorMap.Accent2.ToString();
  1134. }
  1135. else if (scv.Equals("Accent3"))
  1136. {
  1137. return colorMap.Accent3.ToString();
  1138. }
  1139. else if (scv.Equals("Accent4"))
  1140. {
  1141. return colorMap.Accent4.ToString();
  1142. }
  1143. else if (scv.Equals("Accent5"))
  1144. {
  1145. return colorMap.Accent5.ToString();
  1146. }
  1147. else if (scv.Equals("Accent6"))
  1148. {
  1149. return colorMap.Accent6.ToString();
  1150. }
  1151. else if (scv.Equals("Background1"))
  1152. {
  1153. return colorMap.Background1.ToString();
  1154. }
  1155. else if (scv.Equals("Background2"))
  1156. {
  1157. return colorMap.Background2.ToString();
  1158. }
  1159. else if (scv.Equals("Hyperlink"))
  1160. {
  1161. return colorMap.Hyperlink.ToString();
  1162. }
  1163. else if (scv.Equals("FollowedHyperlink"))
  1164. {
  1165. return colorMap.FollowedHyperlink.ToString();
  1166. }
  1167. else if (scv.Equals("PhColor"))
  1168. {
  1169. return "PhColor";
  1170. }
  1171. else if (scv.Equals("Dark1"))
  1172. {
  1173. return "dk1";
  1174. }
  1175. else if (scv.Equals("Dark2"))
  1176. {
  1177. return "dk2";
  1178. }
  1179. else if (scv.Equals("Light1"))
  1180. {
  1181. return "lt1";
  1182. }
  1183. else if (scv.Equals("Light2"))
  1184. {
  1185. return "lt2";
  1186. }
  1187. return null;
  1188. }
  1189. /// <summary>
  1190. /// 转换Surrogate 类型的编码的字符串
  1191. /// </summary>
  1192. /// <param name="xml"></param>
  1193. /// <returns></returns>
  1194. public static Dictionary<string, string> StringEncode(string xml)
  1195. {
  1196. Dictionary<string, string> codeValues = new Dictionary<string, string>();
  1197. Char[] charstr = xml.ToCharArray();
  1198. int len = charstr.Length;
  1199. for (int i = 0; i < len; i++)
  1200. {
  1201. var stringBuilder = "";
  1202. string str = Char.ToString(charstr[i]);
  1203. if (Char.GetUnicodeCategory(charstr[i]) == UnicodeCategory.Surrogate)
  1204. {
  1205. string str1 = Char.ToString(charstr[i + 1]);
  1206. str += str1;
  1207. var bytes = Encoding.UTF32.GetBytes(str);
  1208. for (var j = 0; j < bytes.Length; j += 2)
  1209. {
  1210. stringBuilder = bytes[j + 1].ToString("x").PadLeft(2, '0') + bytes[j].ToString("x").PadLeft(2, '0') + stringBuilder;
  1211. }
  1212. var id = Convert.ToInt64(stringBuilder, 16);
  1213. i += 1;
  1214. string code = id + "";
  1215. codeValues.TryAdd(str, code);
  1216. }
  1217. }
  1218. return codeValues;
  1219. }
  1220. /// <summary>
  1221. /// 根据OfficeML转换为MathML
  1222. /// </summary>
  1223. /// <param name="innerXml"></param>
  1224. /// <returns></returns>
  1225. public static string ProcessOMath(string innerXml)
  1226. {
  1227. Dictionary<string, string> codeValues = StringEncode(innerXml);
  1228. if (codeValues != null)
  1229. {
  1230. foreach (string codeValue in codeValues.Keys)
  1231. {
  1232. innerXml = Regex.Replace(innerXml, codeValue, codeValues[codeValue]);
  1233. }
  1234. }
  1235. XElement element = XElement.Load(new StringReader(innerXml));
  1236. List<XElement> elements = element.Elements().ToList();
  1237. element = elements.Where(x => x.Name.LocalName.Equals("oMath")).FirstOrDefault();
  1238. XslCompiledTransform xslTransform = new XslCompiledTransform();
  1239. xslTransform.Load(BaseConfigModel.ContentRootPath + "/XslFiles/OMML2MML.XSL");
  1240. string mathXml = element.ToString();
  1241. string officeML = string.Empty;
  1242. using (TextReader tr = new StringReader(mathXml))
  1243. {
  1244. using (XmlReader reader = XmlReader.Create(tr))
  1245. {
  1246. using (MemoryStream ms = new MemoryStream())
  1247. {
  1248. XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
  1249. settings.ConformanceLevel = ConformanceLevel.Fragment;
  1250. settings.OmitXmlDeclaration = true;
  1251. settings.Encoding = Encoding.Unicode;
  1252. XmlWriter xw = XmlWriter.Create(ms, settings);
  1253. xslTransform.Transform(reader, xw);
  1254. ms.Seek(0, SeekOrigin.Begin);
  1255. using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  1256. {
  1257. officeML = sr.ReadToEnd();
  1258. }
  1259. }
  1260. }
  1261. }
  1262. officeML = officeML.Replace("mml:", "");
  1263. if (codeValues != null)
  1264. {
  1265. foreach (string codeValue in codeValues.Keys)
  1266. {
  1267. officeML = Regex.Replace(officeML, codeValues[codeValue], codeValue);
  1268. }
  1269. }
  1270. return officeML;
  1271. }
  1272. }
  1273. }