PresentationConvert.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. schemeColor = ColorHelper.GetColorLumModAndLumOff(color, lumModVal, lumOffVal);
  888. }
  889. if (tintVal > 0)
  890. {
  891. schemeColor = ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), tintVal, "Tint");
  892. }
  893. if (shadeVal > 0)
  894. {
  895. schemeColor = ColorHelper.GetShadeOrTintColor(ColorTranslator.FromHtml("#" + schemeColor), shadeVal, "Shade");
  896. }
  897. return schemeColor;
  898. }
  899. /// <summary>
  900. /// 获取声明式颜色
  901. /// </summary>
  902. /// <param name="colorScheme"></param>
  903. /// <returns></returns>
  904. public string GetSchemeColor(IEnumerable<DocumentFormat.OpenXml.Drawing.SchemeColor> colorScheme, Theme theme, ColorMap colorMap)
  905. {
  906. string schemeColor = "";
  907. if (colorScheme.Count() > 0)
  908. {
  909. schemeColor = ColorForThemeClr(SchemeColorForColorMap(colorScheme.FirstOrDefault().Val.Value, colorMap), theme);
  910. var scheme = colorScheme.First();
  911. schemeColor = GetColorSchemeExtInfo(scheme, schemeColor);
  912. }
  913. return schemeColor;
  914. }
  915. /// <summary>
  916. /// 获取RGBColor
  917. /// </summary>
  918. /// <param name="rgbColors"></param>
  919. /// <returns></returns>
  920. public string GetRgbColor(IEnumerable<DocumentFormat.OpenXml.Drawing.RgbColorModelHex> rgbColors)
  921. {
  922. string rgbColor = "";
  923. if (rgbColors.Count() > 0)
  924. {
  925. rgbColor = rgbColors.FirstOrDefault().Val;
  926. }
  927. return rgbColor;
  928. }
  929. /// <summary>
  930. /// 在主题Theme中获取颜色
  931. /// </summary>
  932. /// <param name="scv"></param>
  933. /// <param name="theme"></param>
  934. /// <returns></returns>
  935. public static string ColorForThemeClr(string scv, Theme theme)
  936. {
  937. DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
  938. string colorStr = "";
  939. if (scv.Equals("dk1"))
  940. {
  941. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  942. if (sysColor != null)
  943. {
  944. return sysColor.LastColor;
  945. }
  946. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  947. if (rgbColor != null)
  948. {
  949. return rgbColor.Val;
  950. }
  951. return colorStr;
  952. }
  953. if (scv.Equals("dk2"))
  954. {
  955. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  956. if (sysColor != null)
  957. {
  958. return sysColor.LastColor;
  959. }
  960. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Dark2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  961. if (rgbColor != null)
  962. {
  963. return rgbColor.Val;
  964. }
  965. return colorStr;
  966. }
  967. else if (scv.Equals("accent1"))
  968. {
  969. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  970. if (sysColor != null)
  971. {
  972. return sysColor.LastColor;
  973. }
  974. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  975. if (rgbColor != null)
  976. {
  977. return rgbColor.Val;
  978. }
  979. return colorStr;
  980. }
  981. else if (scv.Equals("accent2"))
  982. {
  983. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  984. if (sysColor != null)
  985. {
  986. return sysColor.LastColor;
  987. }
  988. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  989. if (rgbColor != null)
  990. {
  991. return rgbColor.Val;
  992. }
  993. return colorStr;
  994. }
  995. else if (scv.Equals("accent3"))
  996. {
  997. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  998. if (sysColor != null)
  999. {
  1000. return sysColor.LastColor;
  1001. }
  1002. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent3Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1003. if (rgbColor != null)
  1004. {
  1005. return rgbColor.Val;
  1006. }
  1007. return colorStr;
  1008. }
  1009. else if (scv.Equals("accent4"))
  1010. {
  1011. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1012. if (sysColor != null)
  1013. {
  1014. return sysColor.LastColor;
  1015. }
  1016. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent4Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1017. if (rgbColor != null)
  1018. {
  1019. return rgbColor.Val;
  1020. }
  1021. return colorStr;
  1022. }
  1023. else if (scv.Equals("accent5"))
  1024. {
  1025. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1026. if (sysColor != null)
  1027. {
  1028. return sysColor.LastColor;
  1029. }
  1030. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent5Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1031. if (rgbColor != null)
  1032. {
  1033. return rgbColor.Val;
  1034. }
  1035. return colorStr;
  1036. }
  1037. else if (scv.Equals("accent6"))
  1038. {
  1039. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1040. if (sysColor != null)
  1041. {
  1042. return sysColor.LastColor;
  1043. }
  1044. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Accent6Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1045. if (rgbColor != null)
  1046. {
  1047. return rgbColor.Val;
  1048. }
  1049. return colorStr;
  1050. }
  1051. else if (scv.Equals("lt1"))
  1052. {
  1053. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1054. if (sysColor != null)
  1055. {
  1056. return sysColor.LastColor;
  1057. }
  1058. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light1Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1059. if (rgbColor != null)
  1060. {
  1061. return rgbColor.Val;
  1062. }
  1063. return colorStr;
  1064. }
  1065. else if (scv.Equals("lt2"))
  1066. {
  1067. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1068. if (sysColor != null)
  1069. {
  1070. return sysColor.LastColor;
  1071. }
  1072. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Light2Color.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1073. if (rgbColor != null)
  1074. {
  1075. return rgbColor.Val;
  1076. }
  1077. return colorStr;
  1078. }
  1079. else if (scv.Equals("hlink"))
  1080. {
  1081. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1082. if (sysColor != null)
  1083. {
  1084. return sysColor.LastColor;
  1085. }
  1086. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.Hyperlink.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1087. if (rgbColor != null)
  1088. {
  1089. return rgbColor.Val;
  1090. }
  1091. return colorStr;
  1092. }
  1093. else if (scv.Equals("folHlink"))
  1094. {
  1095. DocumentFormat.OpenXml.Drawing.SystemColor sysColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.SystemColor>();
  1096. if (sysColor != null)
  1097. {
  1098. return sysColor.LastColor;
  1099. }
  1100. DocumentFormat.OpenXml.Drawing.RgbColorModelHex rgbColor = colorScheme.FollowedHyperlinkColor.ChildElements.First<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>();
  1101. if (rgbColor != null)
  1102. {
  1103. return rgbColor.Val;
  1104. }
  1105. return colorStr;
  1106. }
  1107. return "";
  1108. }
  1109. /// <summary>
  1110. /// 按照ColorMap 去寻找对应的颜色
  1111. /// </summary>
  1112. /// <param name="schemeColorValues"></param>
  1113. /// <param name="colorMap"></param>
  1114. /// <returns></returns>
  1115. public static string SchemeColorForColorMap(DocumentFormat.OpenXml.Drawing.SchemeColorValues schemeColorValues, ColorMap colorMap)
  1116. {
  1117. string scv = schemeColorValues.ToString();
  1118. if (scv.Equals("Text1"))
  1119. {
  1120. return colorMap.Text1.ToString();
  1121. }
  1122. if (scv.Equals("Text2"))
  1123. {
  1124. return colorMap.Text2.ToString();
  1125. }
  1126. else if (scv.Equals("Accent1"))
  1127. {
  1128. return colorMap.Accent1.ToString();
  1129. }
  1130. else if (scv.Equals("Accent2"))
  1131. {
  1132. return colorMap.Accent2.ToString();
  1133. }
  1134. else if (scv.Equals("Accent3"))
  1135. {
  1136. return colorMap.Accent3.ToString();
  1137. }
  1138. else if (scv.Equals("Accent4"))
  1139. {
  1140. return colorMap.Accent4.ToString();
  1141. }
  1142. else if (scv.Equals("Accent5"))
  1143. {
  1144. return colorMap.Accent5.ToString();
  1145. }
  1146. else if (scv.Equals("Accent6"))
  1147. {
  1148. return colorMap.Accent6.ToString();
  1149. }
  1150. else if (scv.Equals("Background1"))
  1151. {
  1152. return colorMap.Background1.ToString();
  1153. }
  1154. else if (scv.Equals("Background2"))
  1155. {
  1156. return colorMap.Background2.ToString();
  1157. }
  1158. else if (scv.Equals("Hyperlink"))
  1159. {
  1160. return colorMap.Hyperlink.ToString();
  1161. }
  1162. else if (scv.Equals("FollowedHyperlink"))
  1163. {
  1164. return colorMap.FollowedHyperlink.ToString();
  1165. }
  1166. else if (scv.Equals("PhColor"))
  1167. {
  1168. return "PhColor";
  1169. }
  1170. else if (scv.Equals("Dark1"))
  1171. {
  1172. return "dk1";
  1173. }
  1174. else if (scv.Equals("Dark2"))
  1175. {
  1176. return "dk2";
  1177. }
  1178. else if (scv.Equals("Light1"))
  1179. {
  1180. return "lt1";
  1181. }
  1182. else if (scv.Equals("Light2"))
  1183. {
  1184. return "lt2";
  1185. }
  1186. return null;
  1187. }
  1188. /// <summary>
  1189. /// 转换Surrogate 类型的编码的字符串
  1190. /// </summary>
  1191. /// <param name="xml"></param>
  1192. /// <returns></returns>
  1193. public static Dictionary<string, string> StringEncode(string xml)
  1194. {
  1195. Dictionary<string, string> codeValues = new Dictionary<string, string>();
  1196. Char[] charstr = xml.ToCharArray();
  1197. int len = charstr.Length;
  1198. for (int i = 0; i < len; i++)
  1199. {
  1200. var stringBuilder = "";
  1201. string str = Char.ToString(charstr[i]);
  1202. if (Char.GetUnicodeCategory(charstr[i]) == UnicodeCategory.Surrogate)
  1203. {
  1204. string str1 = Char.ToString(charstr[i + 1]);
  1205. str += str1;
  1206. var bytes = Encoding.UTF32.GetBytes(str);
  1207. for (var j = 0; j < bytes.Length; j += 2)
  1208. {
  1209. stringBuilder = bytes[j + 1].ToString("x").PadLeft(2, '0') + bytes[j].ToString("x").PadLeft(2, '0') + stringBuilder;
  1210. }
  1211. var id = Convert.ToInt64(stringBuilder, 16);
  1212. i += 1;
  1213. string code = id + "";
  1214. codeValues.TryAdd(str, code);
  1215. }
  1216. }
  1217. return codeValues;
  1218. }
  1219. /// <summary>
  1220. /// 根据OfficeML转换为MathML
  1221. /// </summary>
  1222. /// <param name="innerXml"></param>
  1223. /// <returns></returns>
  1224. public static string ProcessOMath(string innerXml)
  1225. {
  1226. Dictionary<string, string> codeValues = StringEncode(innerXml);
  1227. if (codeValues != null)
  1228. {
  1229. foreach (string codeValue in codeValues.Keys)
  1230. {
  1231. innerXml = Regex.Replace(innerXml, codeValue, codeValues[codeValue]);
  1232. }
  1233. }
  1234. XElement element = XElement.Load(new StringReader(innerXml));
  1235. List<XElement> elements = element.Elements().ToList();
  1236. element = elements.Where(x => x.Name.LocalName.Equals("oMath")).FirstOrDefault();
  1237. XslCompiledTransform xslTransform = new XslCompiledTransform();
  1238. xslTransform.Load(BaseConfigModel.ContentRootPath + "/XslFiles/OMML2MML.XSL");
  1239. string mathXml = element.ToString();
  1240. string officeML = string.Empty;
  1241. using (TextReader tr = new StringReader(mathXml))
  1242. {
  1243. using (XmlReader reader = XmlReader.Create(tr))
  1244. {
  1245. using (MemoryStream ms = new MemoryStream())
  1246. {
  1247. XmlWriterSettings settings = xslTransform.OutputSettings.Clone();
  1248. settings.ConformanceLevel = ConformanceLevel.Fragment;
  1249. settings.OmitXmlDeclaration = true;
  1250. settings.Encoding = Encoding.Unicode;
  1251. XmlWriter xw = XmlWriter.Create(ms, settings);
  1252. xslTransform.Transform(reader, xw);
  1253. ms.Seek(0, SeekOrigin.Begin);
  1254. using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  1255. {
  1256. officeML = sr.ReadToEnd();
  1257. }
  1258. }
  1259. }
  1260. }
  1261. officeML = officeML.Replace("mml:", "");
  1262. if (codeValues != null)
  1263. {
  1264. foreach (string codeValue in codeValues.Keys)
  1265. {
  1266. officeML = Regex.Replace(officeML, codeValues[codeValue], codeValue);
  1267. }
  1268. }
  1269. return officeML;
  1270. }
  1271. }
  1272. }