PPTXHelper.cs 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. using DocumentFormat.OpenXml;
  2. using HTEXLib.Models.Inner;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using DocumentFormat.OpenXml.Presentation;
  9. using DocumentFormat.OpenXml.Drawing;
  10. using HTEXLib.Models;
  11. using ColorType = DocumentFormat.OpenXml.Drawing.ColorType;
  12. using System.Linq;
  13. using DocumentFormat.OpenXml.Packaging;
  14. using DocumentFormat.OpenXml.Drawing.Charts;
  15. using System.Drawing;
  16. using HTEXLib.Helpers.ColorHelpers;
  17. using System.IO;
  18. namespace HTEXLib.Helpers.ShapeHelpers
  19. {
  20. public class SlideColor
  21. {
  22. public string LineColor { get; set; }
  23. public string FillColor { get; set; }
  24. public string EffectColor { get; set; }
  25. public string FontColor { get; set; }
  26. }
  27. public static class PPTXHelper
  28. {
  29. // StyleMatrixReferenceType
  30. ///处理边框
  31. //
  32. // 摘要:
  33. // LineReference.
  34. // Represents the following element tag in the schema: a:lnRef.
  35. //public LineReference LineReference { get; set; }
  36. //
  37. // 摘要:
  38. // FillReference.
  39. // Represents the following element tag in the schema: a:fillRef.
  40. // public FillReference FillReference { get; set; }
  41. //
  42. // 摘要:
  43. // EffectReference.
  44. // Represents the following element tag in the schema: a:effectRef.
  45. //
  46. // 摘要:
  47. // Font Reference.
  48. // Represents the following element tag in the schema: a:fontRef.
  49. // public FontReference FontReference { get; set; }
  50. /// <summary>
  51. /// 处理 LineReference 轮廓 a:lnRef
  52. /// 处理 FillReference 填充 a:fillRef
  53. /// 处理 EffectReference 效果 a:effectRef
  54. /// 处理 FontReference 字体 a:fontRef
  55. /// </summary>
  56. /// <param name="shapeStyle"></param>
  57. public static SlideColor DoShapeStyle(DocumentFormat.OpenXml.Presentation.ShapeStyle shapeStyle, PPTSlide slide, string type) {
  58. if (shapeStyle == null) {
  59. return null;
  60. }
  61. string LineColor = DoMatrixReferenceColors(shapeStyle.LineReference, slide);
  62. string FillColor = DoMatrixReferenceColors(shapeStyle.FillReference, slide);
  63. string EffectColor = DoMatrixReferenceColors(shapeStyle.EffectReference, slide);
  64. string FontColor = FontReferenceColors(shapeStyle.FontReference, slide);
  65. return new SlideColor { LineColor = LineColor, FillColor = FillColor, EffectColor = EffectColor, FontColor = FontColor };
  66. }
  67. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(DocumentFormat.OpenXml.Drawing.Charts.ShapeProperties shapeProperties, PPTSlide slide, string type, string partForm)
  68. {
  69. if (shapeProperties == null) {
  70. return null;
  71. }
  72. //Outline
  73. var lnNode = shapeProperties.GetFirstChild<Outline>();
  74. Border border = null;
  75. if (lnNode != null)
  76. {
  77. border = DoOutline(lnNode, slide, type);
  78. }
  79. Fill fill = new Fill() { type = -1 };
  80. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  81. if (gradFill != null)
  82. {
  83. fill.type = 2;
  84. fill.gradientFill = DoGradientFill(gradFill, slide);
  85. }
  86. var noFill = shapeProperties.GetFirstChild<NoFill>();
  87. if (noFill != null)
  88. {
  89. fill.type = 0;
  90. }
  91. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  92. if (pattFill != null)
  93. {
  94. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  95. fill.type = 4;
  96. fill.pattFill = htexPattFill;
  97. }
  98. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  99. if (solidFill != null)
  100. {
  101. fill.type = 1;
  102. fill.solidFill = DoSolidFill(solidFill, slide);
  103. }
  104. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  105. if (groupFill != null)
  106. {
  107. fill.type = 5;
  108. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  109. }
  110. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  111. if (blipFill != null)
  112. {
  113. fill.type = 3;
  114. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  115. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  116. }
  117. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  118. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  119. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  120. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  121. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  122. // CustomGeometry PresetGeometry TODO
  123. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border };
  124. }
  125. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(ChartShapeProperties shapeProperties, PPTSlide slide, string type, string partForm,ChartPart chartPart)
  126. {
  127. if (chartPart != null) {
  128. slide.chartPart = chartPart;
  129. }
  130. //Outline
  131. var lnNode = shapeProperties.GetFirstChild<Outline>();
  132. Border border = null;
  133. if (lnNode != null)
  134. {
  135. border = DoOutline(lnNode, slide, type);
  136. }
  137. Fill fill = new Fill() { type = -1 };
  138. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  139. if (gradFill != null)
  140. {
  141. fill.type = 2;
  142. fill.gradientFill = DoGradientFill(gradFill, slide);
  143. }
  144. var noFill = shapeProperties.GetFirstChild<NoFill>();
  145. if (noFill != null)
  146. {
  147. fill.type = 0;
  148. }
  149. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  150. if (pattFill != null)
  151. {
  152. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  153. fill.type = 4;
  154. fill.pattFill = htexPattFill;
  155. }
  156. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  157. if (solidFill != null)
  158. {
  159. fill.type = 1;
  160. fill.solidFill = DoSolidFill(solidFill, slide);
  161. }
  162. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  163. if (groupFill != null)
  164. {
  165. fill.type = 5;
  166. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  167. }
  168. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  169. if (blipFill != null)
  170. {
  171. fill.type = 3;
  172. partForm = "chartPart";
  173. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  174. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  175. }
  176. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  177. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  178. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  179. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  180. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  181. // CustomGeometry PresetGeometry TODO
  182. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border };
  183. }
  184. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(GroupShapeProperties shapeProperties, PPTSlide slide, string type, string partForm)
  185. {
  186. //Outline
  187. var lnNode = shapeProperties.GetFirstChild<Outline>();
  188. Border border = null;
  189. if (lnNode != null)
  190. {
  191. border = DoOutline(lnNode, slide, type);
  192. }
  193. Fill fill = new Fill() { type = -1 };
  194. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  195. if (gradFill != null)
  196. {
  197. fill.type = 2;
  198. fill.gradientFill = DoGradientFill(gradFill, slide);
  199. }
  200. var noFill = shapeProperties.GetFirstChild<NoFill>();
  201. if (noFill != null)
  202. {
  203. fill.type = 0;
  204. }
  205. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  206. if (pattFill != null)
  207. {
  208. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  209. fill.type = 4;
  210. fill.pattFill = htexPattFill;
  211. }
  212. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  213. if (solidFill != null)
  214. {
  215. fill.type = 1;
  216. fill.solidFill = DoSolidFill(solidFill, slide);
  217. }
  218. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  219. if (groupFill != null)
  220. {
  221. fill.type = 5;
  222. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  223. }
  224. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  225. if (blipFill != null)
  226. {
  227. fill.type = 3;
  228. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  229. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  230. }
  231. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  232. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  233. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  234. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  235. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  236. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border };
  237. }
  238. public static TbStyle DoTableProperties(TableProperties shapeProperties, PPTSlide slide, string type, string partForm)
  239. {
  240. Fill fill = new Fill() { type = -1 };
  241. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  242. if (gradFill != null)
  243. {
  244. fill.type = 2;
  245. fill.gradientFill = DoGradientFill(gradFill, slide);
  246. }
  247. var noFill = shapeProperties.GetFirstChild<NoFill>();
  248. if (noFill != null)
  249. {
  250. fill.type = 0;
  251. }
  252. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  253. if (pattFill != null)
  254. {
  255. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  256. fill.type = 4;
  257. fill.pattFill = htexPattFill;
  258. }
  259. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  260. if (solidFill != null)
  261. {
  262. fill.type = 1;
  263. fill.solidFill = DoSolidFill(solidFill, slide);
  264. }
  265. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  266. if (groupFill != null)
  267. {
  268. fill.type = 5;
  269. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  270. }
  271. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  272. if (blipFill != null)
  273. {
  274. fill.type = 3;
  275. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  276. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  277. }
  278. var TableStyleId = shapeProperties.GetFirstChild<TableStyleId>().InnerText;
  279. TableStyleType TableStyle = shapeProperties.GetFirstChild<TableStyle>();
  280. if (TableStyle == null)
  281. {
  282. var TableStyleEntrys = slide.tableStylesPart.TableStyleList.Elements<TableStyleEntry>();
  283. foreach (var TableStyleEntry in TableStyleEntrys) {
  284. if (TableStyleEntry.StyleId == TableStyleId) {
  285. TableStyle = TableStyleEntry;
  286. }
  287. }
  288. }
  289. TbStyle tbStyle =DoTableStyle(TableStyle,slide,type,partForm);
  290. if (fill != null )
  291. {
  292. tbStyle.fill = fill;
  293. }
  294. //TODO
  295. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  296. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  297. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.ExtensionList>();
  298. tbStyle.RightToLeft = shapeProperties.RightToLeft != null? shapeProperties.RightToLeft.Value:false;
  299. tbStyle.FirstRow = shapeProperties.FirstRow != null ? shapeProperties.FirstRow.Value : false;
  300. tbStyle.FirstColumn = shapeProperties.FirstColumn != null ? shapeProperties.FirstColumn.Value : false;
  301. tbStyle.LastRow = shapeProperties.LastRow != null ? shapeProperties.LastRow.Value : false;
  302. tbStyle.LastColumn = shapeProperties.LastColumn != null ? shapeProperties.LastColumn.Value : false;
  303. tbStyle.BandRow = shapeProperties.BandRow != null ? shapeProperties.BandRow.Value : false;
  304. tbStyle.BandColumn = shapeProperties.BandColumn != null ? shapeProperties.BandColumn.Value : false;
  305. return tbStyle;
  306. }
  307. public static TbStyle DoTableStyle(TableStyleType tableStyle, PPTSlide slide, string type, string partForm) {
  308. TbStyle tbStyle = new TbStyle();
  309. if (tableStyle == null) {
  310. return tbStyle;
  311. }
  312. var TableBackground = tableStyle.TableBackground;
  313. if (TableBackground != null)
  314. {
  315. var shapeProperties = TableBackground.GetFirstChild<FillProperties>();
  316. Fill fill = DoFillProperties(shapeProperties , partForm,slide );
  317. var FillReference = TableBackground.GetFirstChild<FillReference>();
  318. fill= DoFillReference(slide, fill, FillReference);
  319. tbStyle.fill = fill;
  320. //TODO
  321. TableBackground.GetFirstChild<EffectPropertiesType>();
  322. TableBackground.GetFirstChild<EffectReference>();
  323. }
  324. //TablePartStyleType
  325. List<CellStyle> cellStyles = new List<CellStyle>();
  326. var WholeTable = tableStyle.WholeTable;
  327. if (WholeTable != null) {
  328. var style= DoTablePartStyleType(WholeTable, slide, type, partForm);
  329. if (style != null) {
  330. cellStyles.Add(style);
  331. }
  332. }
  333. var Band1Horizontal = tableStyle.Band1Horizontal;
  334. if (Band1Horizontal != null)
  335. {
  336. var style= DoTablePartStyleType(Band1Horizontal, slide, type, partForm);
  337. if (style != null)
  338. {
  339. cellStyles.Add(style);
  340. }
  341. }
  342. var Band2Horizontal = tableStyle.Band2Horizontal;
  343. if (Band2Horizontal != null)
  344. {
  345. var style= DoTablePartStyleType(Band2Horizontal, slide, type, partForm);
  346. if (style != null)
  347. {
  348. cellStyles.Add(style);
  349. }
  350. }
  351. var Band1Vertical = tableStyle.Band1Vertical;
  352. if (Band1Vertical != null)
  353. {
  354. var style= DoTablePartStyleType(Band1Vertical, slide, type, partForm);
  355. if (style != null)
  356. {
  357. cellStyles.Add(style);
  358. }
  359. }
  360. var Band2Vertical = tableStyle.Band2Vertical;
  361. if (Band2Vertical != null)
  362. {
  363. var style= DoTablePartStyleType(Band2Vertical, slide, type, partForm);
  364. if (style != null)
  365. {
  366. cellStyles.Add(style);
  367. }
  368. }
  369. var LastColumn = tableStyle.LastColumn;
  370. if (LastColumn != null)
  371. {
  372. var style= DoTablePartStyleType(LastColumn, slide, type, partForm);
  373. if (style != null)
  374. {
  375. cellStyles.Add(style);
  376. }
  377. }
  378. var FirstColumn = tableStyle.FirstColumn;
  379. if (FirstColumn != null)
  380. {
  381. var style= DoTablePartStyleType(FirstColumn, slide, type, partForm);
  382. if (style != null)
  383. {
  384. cellStyles.Add(style);
  385. }
  386. }
  387. var FirstRow = tableStyle.FirstRow;
  388. if (FirstRow != null)
  389. {
  390. var style= DoTablePartStyleType(FirstRow, slide, type, partForm);
  391. if (style != null)
  392. {
  393. cellStyles.Add(style);
  394. }
  395. }
  396. var LastRow = tableStyle.LastRow;
  397. if (LastRow != null)
  398. {
  399. var style= DoTablePartStyleType(LastRow, slide, type, partForm);
  400. if (style != null)
  401. {
  402. cellStyles.Add(style);
  403. }
  404. }
  405. var SoutheastCell = tableStyle.SoutheastCell;
  406. if (SoutheastCell != null)
  407. {
  408. var style= DoTablePartStyleType(SoutheastCell, slide, type, partForm);
  409. if (style != null)
  410. {
  411. cellStyles.Add(style);
  412. }
  413. }
  414. var SouthwestCell = tableStyle.SouthwestCell;
  415. if (SouthwestCell != null)
  416. {
  417. var style= DoTablePartStyleType(SouthwestCell, slide, type, partForm);
  418. if (style != null)
  419. {
  420. cellStyles.Add(style);
  421. }
  422. }
  423. var NortheastCell = tableStyle.NortheastCell;
  424. if (NortheastCell != null)
  425. {
  426. var style= DoTablePartStyleType(NortheastCell, slide, type, partForm);
  427. if (style != null)
  428. {
  429. cellStyles.Add(style);
  430. }
  431. }
  432. var NorthwestCell = tableStyle.NorthwestCell;
  433. if (NorthwestCell != null)
  434. {
  435. var style= DoTablePartStyleType(NorthwestCell, slide, type, partForm);
  436. if (style != null)
  437. {
  438. cellStyles.Add(style);
  439. }
  440. }
  441. tbStyle.cellStyles = cellStyles;
  442. var ExtensionList = tableStyle.ExtensionList;
  443. return tbStyle;
  444. }
  445. private static Fill DoFillReference(PPTSlide slide, Fill fill, FillReference FillReference)
  446. {
  447. if (FillReference != null)
  448. {
  449. if (fill.type == -1)
  450. {
  451. string color = null;
  452. if (FillReference.RgbColorModelPercentage != null)
  453. {
  454. color = ReadColor(FillReference.RgbColorModelPercentage);
  455. }
  456. if (FillReference.RgbColorModelHex != null)
  457. {
  458. color = ReadColor(FillReference.RgbColorModelHex);
  459. }
  460. if (FillReference.HslColor != null)
  461. {
  462. color = ReadColor(FillReference.HslColor);
  463. }
  464. if (FillReference.SystemColor != null)
  465. {
  466. color = ReadColor(FillReference.SystemColor, slide);
  467. }
  468. if (FillReference.SchemeColor != null)
  469. {
  470. color = ReadColor(FillReference.SchemeColor, slide);
  471. }
  472. if (FillReference.PresetColor != null)
  473. {
  474. color = ReadColor(FillReference.PresetColor);
  475. }
  476. fill.solidFill = color;
  477. fill.type = 2;
  478. }
  479. }
  480. return fill;
  481. }
  482. private static Fill DoFillProperties(FillProperties shapeProperties , string partForm, PPTSlide slide)
  483. {
  484. Fill fill = new Fill() { type = -1 };
  485. if (shapeProperties != null)
  486. {
  487. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  488. if (gradFill != null)
  489. {
  490. fill.type = 2;
  491. fill.gradientFill = DoGradientFill(gradFill, slide);
  492. }
  493. var noFill = shapeProperties.GetFirstChild<NoFill>();
  494. if (noFill != null)
  495. {
  496. fill.type = 0;
  497. }
  498. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  499. if (pattFill != null)
  500. {
  501. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  502. fill.type = 4;
  503. fill.pattFill = htexPattFill;
  504. }
  505. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  506. if (solidFill != null)
  507. {
  508. fill.type = 1;
  509. fill.solidFill = DoSolidFill(solidFill, slide);
  510. }
  511. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  512. if (groupFill != null)
  513. {
  514. fill.type = 5;
  515. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  516. }
  517. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  518. if (blipFill != null)
  519. {
  520. fill.type = 3;
  521. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  522. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  523. }
  524. }
  525. return fill;
  526. }
  527. /// <summary>
  528. /// 单元格样式表
  529. /// </summary>
  530. /// <param name="tablePartStyleType"></param>
  531. /// <param name="slide"></param>
  532. /// <param name="type"></param>
  533. /// <param name="partForm"></param>
  534. public static CellStyle DoTablePartStyleType(TablePartStyleType tablePartStyleType, PPTSlide slide, string type, string partForm) {
  535. //表格单元格文本样式。
  536. CellStyle cellStyle = new CellStyle() { type= tablePartStyleType.LocalName};
  537. var TableCellTextStyle = tablePartStyleType.GetFirstChild<TableCellTextStyle>();
  538. if (TableCellTextStyle != null) {
  539. FontStyle fontStyle = new FontStyle { };
  540. //字体加粗以及斜体
  541. if (TableCellTextStyle.Bold != null && TableCellTextStyle.Bold.Value == BooleanStyleValues.On)
  542. {
  543. fontStyle.bold = true;
  544. }
  545. else {
  546. fontStyle.bold = false;
  547. }
  548. if (TableCellTextStyle.Italic != null && TableCellTextStyle.Italic.Value == BooleanStyleValues.On)
  549. {
  550. fontStyle.italic = true;
  551. }
  552. //字体颜色
  553. string color = null;
  554. var RgbColorModelPercentage = TableCellTextStyle.GetFirstChild<RgbColorModelPercentage>();
  555. if (RgbColorModelPercentage != null)
  556. {
  557. color = ReadColor(RgbColorModelPercentage);
  558. }
  559. var RgbColorModelHex = TableCellTextStyle.GetFirstChild<RgbColorModelHex>();
  560. if (RgbColorModelHex != null)
  561. {
  562. color = ReadColor(RgbColorModelHex);
  563. }
  564. var HslColor = TableCellTextStyle.GetFirstChild<DocumentFormat.OpenXml.Drawing.HslColor>();
  565. if (HslColor != null)
  566. {
  567. color = ReadColor(HslColor);
  568. }
  569. var SystemColor = TableCellTextStyle.GetFirstChild<SystemColor>();
  570. if (SystemColor != null)
  571. {
  572. color = ReadColor(SystemColor, slide);
  573. }
  574. var SchemeColor = TableCellTextStyle.GetFirstChild<SchemeColor>();
  575. if (SchemeColor != null)
  576. {
  577. color = ReadColor(SchemeColor, slide);
  578. }
  579. var PresetColor = TableCellTextStyle.GetFirstChild<PresetColor>();
  580. if (PresetColor != null)
  581. {
  582. color = ReadColor(PresetColor);
  583. }
  584. fontStyle.color = color;
  585. var FontReference = TableCellTextStyle.GetFirstChild<DocumentFormat.OpenXml.Drawing.FontReference>();
  586. if (color == null && FontReference != null)
  587. {
  588. if (FontReference.RgbColorModelPercentage != null)
  589. {
  590. color = ReadColor(FontReference.RgbColorModelPercentage);
  591. }
  592. if (FontReference.RgbColorModelHex != null)
  593. {
  594. color = ReadColor(FontReference.RgbColorModelHex);
  595. }
  596. if (FontReference.HslColor != null)
  597. {
  598. color = ReadColor(FontReference.HslColor);
  599. }
  600. if (FontReference.SystemColor != null)
  601. {
  602. color = ReadColor(FontReference.SystemColor, slide);
  603. }
  604. if (FontReference.SchemeColor != null)
  605. {
  606. color = ReadColor(FontReference.SchemeColor, slide);
  607. }
  608. if (FontReference.PresetColor != null)
  609. {
  610. color = ReadColor(FontReference.PresetColor);
  611. }
  612. fontStyle.color = color;
  613. }
  614. //字体属性
  615. var font = TableCellTextStyle.GetFirstChild<DocumentFormat.OpenXml.Drawing.Fonts>();
  616. if (font != null) {
  617. var LatinFont = font.LatinFont;
  618. var EastAsianFont = font.EastAsianFont;
  619. var ComplexScriptFont = font.ComplexScriptFont;
  620. if (EastAsianFont != null)
  621. {
  622. fontStyle.family = DoTextFontType(EastAsianFont, slide);
  623. }
  624. if (ComplexScriptFont != null)
  625. {
  626. fontStyle.family = DoTextFontType(ComplexScriptFont, slide);
  627. }
  628. if (LatinFont != null)
  629. {
  630. fontStyle.family = DoTextFontType(LatinFont, slide);
  631. }
  632. var SupplementalFont = font.GetFirstChild<SupplementalFont>();
  633. if (SupplementalFont != null)
  634. {
  635. fontStyle.family = SupplementalFont.Typeface;
  636. }
  637. cellStyle.fontStyle = fontStyle;
  638. }
  639. }
  640. //表格单元格样式。
  641. var TableCellStyle = tablePartStyleType.GetFirstChild<TableCellStyle>();
  642. if (TableCellStyle != null)
  643. {
  644. List<CellBorder> cellBorders = new List<CellBorder>();
  645. var TableCellBorders= TableCellStyle.TableCellBorders;
  646. if (TableCellBorders != null) {
  647. var LeftBorder = TableCellBorders.LeftBorder;
  648. if (LeftBorder != null) {
  649. Border border= DoOutline(LeftBorder.Outline, slide, type);
  650. if (LeftBorder.LineReference!= null && (border.color.type == null || border.color.type == -1))
  651. {
  652. border.color.solidFill=DoMatrixReferenceColors(LeftBorder.LineReference, slide);
  653. border.color.type = 2;
  654. }
  655. cellBorders.Add(new CellBorder() { border = border, type = "Left" });
  656. }
  657. var RightBorder = TableCellBorders.RightBorder;
  658. if (RightBorder != null)
  659. {
  660. Border border = DoOutline(RightBorder.Outline, slide, type);
  661. if (RightBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  662. {
  663. border.color.solidFill = DoMatrixReferenceColors(RightBorder.LineReference, slide);
  664. border.color.type = 2;
  665. }
  666. cellBorders.Add(new CellBorder() { border = border, type = "Right" });
  667. }
  668. var TopBorder = TableCellBorders.TopBorder;
  669. if (TopBorder != null)
  670. {
  671. Border border = DoOutline(TopBorder.Outline, slide, type);
  672. if (TopBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  673. {
  674. border.color.solidFill = DoMatrixReferenceColors(TopBorder.LineReference, slide);
  675. border.color.type = 2;
  676. }
  677. cellBorders.Add(new CellBorder() { border = border, type = "Top" });
  678. }
  679. var BottomBorder = TableCellBorders.BottomBorder;
  680. if (BottomBorder != null)
  681. {
  682. Border border = DoOutline(BottomBorder.Outline, slide, type);
  683. if (BottomBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  684. {
  685. border.color.solidFill = DoMatrixReferenceColors(BottomBorder.LineReference, slide);
  686. border.color.type = 2;
  687. }
  688. cellBorders.Add(new CellBorder() { border = border, type = "Bottom" });
  689. }
  690. var InsideHorizontalBorder = TableCellBorders.InsideHorizontalBorder;
  691. if (InsideHorizontalBorder != null)
  692. {
  693. Border border = DoOutline(InsideHorizontalBorder.Outline, slide, type);
  694. if (InsideHorizontalBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  695. {
  696. border.color.solidFill = DoMatrixReferenceColors(InsideHorizontalBorder.LineReference, slide);
  697. border.color.type = 2;
  698. }
  699. cellBorders.Add(new CellBorder() { border = border, type = "InHor" });
  700. }
  701. var InsideVerticalBorder = TableCellBorders.InsideVerticalBorder;
  702. if (InsideVerticalBorder != null)
  703. {
  704. Border border = DoOutline(InsideVerticalBorder.Outline, slide, type);
  705. if (InsideVerticalBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  706. {
  707. border.color.solidFill = DoMatrixReferenceColors(InsideVerticalBorder.LineReference, slide);
  708. border.color.type = 2;
  709. }
  710. cellBorders.Add(new CellBorder() { border = border, type = "InVer" });
  711. }
  712. var TopLeftToBottomRightBorder = TableCellBorders.TopLeftToBottomRightBorder;
  713. if (TopLeftToBottomRightBorder != null)
  714. {
  715. Border border = DoOutline(TopLeftToBottomRightBorder.Outline, slide, type);
  716. if (TopLeftToBottomRightBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  717. {
  718. border.color.solidFill = DoMatrixReferenceColors(TopLeftToBottomRightBorder.LineReference, slide);
  719. border.color.type = 2;
  720. }
  721. cellBorders.Add(new CellBorder() { border = border, type = "Tl2br" });
  722. }
  723. var TopRightToBottomLeftBorder = TableCellBorders.TopRightToBottomLeftBorder;
  724. if (TopRightToBottomLeftBorder != null)
  725. {
  726. Border border = DoOutline(TopRightToBottomLeftBorder.Outline, slide, type);
  727. if (TopRightToBottomLeftBorder.LineReference != null && (border.color.type == null || border.color.type == -1))
  728. {
  729. border.color.solidFill = DoMatrixReferenceColors(TopRightToBottomLeftBorder.LineReference, slide);
  730. border.color.type = 2;
  731. }
  732. cellBorders.Add(new CellBorder() { border = border, type = "Tr2bl" });
  733. }
  734. }
  735. cellStyle.cellBorders = cellBorders;
  736. var FillProperties = TableCellStyle.GetFirstChild<FillProperties>();
  737. Fill fill = DoFillProperties(FillProperties, partForm, slide);
  738. var FillReference = TableCellStyle.GetFirstChild<FillReference>();
  739. fill= DoFillReference(slide, fill, FillReference);
  740. cellStyle.fill = fill;
  741. //TODO
  742. var Cell3DProperties = TableCellStyle.GetFirstChild<Cell3DProperties>();
  743. }
  744. return cellStyle;
  745. }
  746. public static CellStyle DoTableCellProperties(TableCellProperties shapeProperties, PPTSlide slide ,string type ,string partForm) {
  747. CellStyle cellStyle = new CellStyle();
  748. //TODO
  749. var Cell3DProperties = shapeProperties.GetFirstChild<Cell3DProperties>();
  750. Fill fill = new Fill() { type = -1 };
  751. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  752. if (gradFill != null)
  753. {
  754. fill.type = 2;
  755. fill.gradientFill = DoGradientFill(gradFill, slide);
  756. }
  757. var noFill = shapeProperties.GetFirstChild<NoFill>();
  758. if (noFill != null)
  759. {
  760. fill.type = 0;
  761. }
  762. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  763. if (pattFill != null)
  764. {
  765. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  766. fill.type = 4;
  767. fill.pattFill = htexPattFill;
  768. }
  769. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  770. if (solidFill != null)
  771. {
  772. fill.type = 1;
  773. fill.solidFill = DoSolidFill(solidFill, slide);
  774. }
  775. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  776. if (groupFill != null)
  777. {
  778. fill.type = 5;
  779. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  780. }
  781. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  782. if (blipFill != null)
  783. {
  784. fill.type = 3;
  785. fill.blipFill = DoBlipFill(blipFill, slide, partForm);
  786. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  787. }
  788. cellStyle.fill = fill;
  789. List<CellBorder> cellBorders = new List<CellBorder>();
  790. var LeftBorder = shapeProperties.LeftBorderLineProperties;
  791. if (LeftBorder != null)
  792. {
  793. Border border = DoOutline(LeftBorder, slide, type);
  794. cellBorders.Add(new CellBorder() { border = border, type = "Left" });
  795. }
  796. var RightBorder = shapeProperties.RightBorderLineProperties;
  797. if (RightBorder != null)
  798. {
  799. Border border = DoOutline(RightBorder, slide, type);
  800. cellBorders.Add(new CellBorder() { border = border, type = "Right" });
  801. }
  802. var TopBorderLineProperties = shapeProperties.TopBorderLineProperties;
  803. if (TopBorderLineProperties != null)
  804. {
  805. Border border = DoOutline(TopBorderLineProperties, slide, type);
  806. cellBorders.Add(new CellBorder() { border = border, type = "Top" });
  807. }
  808. var BottomBorderLineProperties = shapeProperties.BottomBorderLineProperties;
  809. if (BottomBorderLineProperties != null)
  810. {
  811. Border border = DoOutline(BottomBorderLineProperties, slide, type);
  812. cellBorders.Add(new CellBorder() { border = border, type = "Bottom" });
  813. }
  814. var TopLeftToBottomRightBorderLineProperties = shapeProperties.TopLeftToBottomRightBorderLineProperties;
  815. if (TopLeftToBottomRightBorderLineProperties != null)
  816. {
  817. // \
  818. Border border = DoOutline(TopLeftToBottomRightBorderLineProperties, slide, type);
  819. cellBorders.Add(new CellBorder() { border = border, type = "Tl2br" });
  820. }
  821. var BottomLeftToTopRightBorderLineProperties = shapeProperties.BottomLeftToTopRightBorderLineProperties;
  822. if (BottomLeftToTopRightBorderLineProperties != null)
  823. { // /
  824. Border border = DoOutline(BottomLeftToTopRightBorderLineProperties, slide, type);
  825. cellBorders.Add(new CellBorder() { border = border, type = "Tr2bl" });
  826. }
  827. cellStyle.cellBorders = cellBorders;
  828. return cellStyle;
  829. }
  830. /// <summary>
  831. ///
  832. /// </summary>
  833. /// <param name="shapeProperties"></param>
  834. /// <param name="slide"></param>
  835. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(DocumentFormat.OpenXml.Presentation.ShapeProperties shapeProperties, PPTSlide slide, string type, string partForm) {
  836. //Outline
  837. var lnNode = shapeProperties.GetFirstChild<Outline>();
  838. Border border = null;
  839. if (lnNode != null)
  840. {
  841. border = DoOutline(lnNode, slide,type);
  842. }
  843. Fill fill = new Fill() { type = -1 };
  844. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  845. if (gradFill != null)
  846. {
  847. fill.type = 2;
  848. fill.gradientFill = DoGradientFill(gradFill, slide);
  849. }
  850. var noFill = shapeProperties.GetFirstChild<NoFill>();
  851. if (noFill != null)
  852. {
  853. fill.type = 0;
  854. }
  855. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  856. if (pattFill != null)
  857. {
  858. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  859. fill.type = 4;
  860. fill.pattFill = htexPattFill;
  861. }
  862. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  863. if (solidFill != null)
  864. {
  865. fill.type = 1;
  866. fill.solidFill = DoSolidFill(solidFill, slide);
  867. }
  868. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  869. if (groupFill != null)
  870. {
  871. fill.type =5;
  872. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  873. }
  874. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  875. if (blipFill != null)
  876. {
  877. fill.type = 3;
  878. fill.blipFill = DoBlipFill(blipFill, slide,partForm);
  879. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  880. }
  881. var EffectList= shapeProperties.GetFirstChild<EffectList>();
  882. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  883. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  884. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  885. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  886. return new HTEXLib.Models.HTEX.ShapeStyle {fill=fill,border= border };
  887. }
  888. public static Fill DoBackgroundProperties(DocumentFormat.OpenXml.Presentation.BackgroundProperties shapeProperties, PPTSlide slide, string partForm)
  889. {
  890. Fill fill = new Fill() { type = -1 };
  891. if (shapeProperties == null) {
  892. return fill;
  893. }
  894. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  895. if (gradFill != null)
  896. {
  897. fill.type = 2;
  898. fill.gradientFill = DoGradientFill(gradFill, slide);
  899. }
  900. var noFill = shapeProperties.GetFirstChild<NoFill>();
  901. if (noFill != null)
  902. {
  903. fill.type = 0;
  904. }
  905. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  906. if (pattFill != null)
  907. {
  908. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  909. fill.type = 4;
  910. fill.pattFill = htexPattFill;
  911. }
  912. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  913. if (solidFill != null)
  914. {
  915. fill.type = 1;
  916. fill.solidFill = DoSolidFill(solidFill, slide);
  917. }
  918. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  919. if (groupFill != null)
  920. {
  921. fill.type = 5;
  922. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  923. }
  924. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  925. if (blipFill != null)
  926. {
  927. fill.type = 3;
  928. fill.blipFill = DoBlipFill(blipFill, slide,partForm);
  929. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  930. }
  931. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  932. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  933. //var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  934. //var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  935. //var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  936. return fill;
  937. }
  938. public static HtexBlipFill DoBlipFill(DocumentFormat.OpenXml.Drawing.BlipFill blipFill, PPTSlide slide,string partForm) {
  939. HtexBlipFill htexBlipFill = new HtexBlipFill();
  940. SourceRectangle source= blipFill.SourceRectangle; ///源矩形 srcRect可能无用
  941. htexBlipFill.dip = blipFill.Dpi;
  942. if (blipFill.RotateWithShape != null) {
  943. htexBlipFill.rotWithShape = blipFill.RotateWithShape;
  944. }
  945. { htexBlipFill.rotWithShape =true; }
  946. var fillRect = blipFill.GetTextByPath("a:stretch/a:fillRect");///拉伸
  947. if (fillRect != null) {
  948. htexBlipFill.fillRect = new Vector
  949. {
  950. t = fillRect.Attribute("t") != null ? int.Parse(fillRect.Attribute("t").Value) / 1000 : 0,
  951. l = fillRect.Attribute("l") != null ? int.Parse(fillRect.Attribute("l").Value) / 1000 : 0,
  952. r = fillRect.Attribute("r") != null ? int.Parse(fillRect.Attribute("r").Value) / 1000 : 0,
  953. b = fillRect.Attribute("b") != null ? int.Parse(fillRect.Attribute("b").Value) / 1000 : 0
  954. };
  955. }
  956. var alphaModFix = blipFill.Blip.GetTextByPath("a:alphaModFix");
  957. if (alphaModFix != null) {
  958. if (alphaModFix.Attribute("amt") != null) {
  959. htexBlipFill.opacity = int.Parse(alphaModFix.Attribute("amt").Value) * 1.0 / 1000;
  960. }
  961. }
  962. Tile tile= blipFill.GetFirstChild <Tile>();
  963. if (tile != null) {
  964. ///algn (Alignment) flip (Tile Flipping) sx (Horizontal Ratio) sy (Vertical Ratio) tx (Horizontal Offset) ty (Vertical Offset)
  965. htexBlipFill.tile = new HtexTile { algn=tile.Alignment,flip=tile.Flip,sx=tile.HorizontalRatio,sy=tile.VerticalRatio,tx=tile.HorizontalOffset,ty=tile.VerticalOffset};
  966. }
  967. var Embed = blipFill.Blip.Embed;
  968. var Link = blipFill.Blip.Link;
  969. //slide.SlideLayoutPart
  970. IdPartPair part = null;
  971. if (partForm == "slide")
  972. {
  973. part = slide.SlidePart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  974. }
  975. if (partForm == "layout")
  976. {
  977. part = slide.SlideLayoutPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  978. }
  979. if (partForm == "master")
  980. {
  981. part = slide.slideMasterPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  982. }
  983. if (partForm == "chartPart") {
  984. part = slide.chartPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  985. }
  986. if (part != null) {
  987. var url= part.OpenXmlPart.Uri .ToString().Replace("../","/ppt/");
  988. var thumbnail = slide.Media.Where(x => x.Attributes().Select(y => y.Value == url).FirstOrDefault()).FirstOrDefault();
  989. if (thumbnail != null)
  990. {
  991. var contentType = thumbnail.Attribute("{http://schemas.microsoft.com/office/2006/xmlPackage}contentType").Value;
  992. //var data = thumbnail.Value;
  993. //url = "data:" + contentType + ";base64," + data;
  994. //url = url.Replace("\r\n", "");
  995. htexBlipFill.url = url;
  996. // htexBlipFill.urlType = "base64";
  997. // htexBlipFill.contentType = contentType;
  998. }
  999. }
  1000. //TODO 图片元素的更多信息需要后期继续实现,如滤镜,裁剪,图片颜色,图片校正等
  1001. return htexBlipFill;
  1002. }
  1003. public static HtexBlipFill DoBlipFill(DocumentFormat.OpenXml.Presentation.BlipFill blipFill, PPTSlide slide, string partForm)
  1004. {
  1005. HtexBlipFill htexBlipFill = new HtexBlipFill();
  1006. SourceRectangle source = blipFill.SourceRectangle; ///源矩形 srcRect可能无用
  1007. htexBlipFill.dip = blipFill.Dpi;
  1008. if (blipFill.RotateWithShape != null)
  1009. {
  1010. htexBlipFill.rotWithShape = blipFill.RotateWithShape;
  1011. }
  1012. { htexBlipFill.rotWithShape = true; }
  1013. var fillRect = blipFill.GetTextByPath("a:stretch/a:fillRect");///拉伸
  1014. if (fillRect != null)
  1015. {
  1016. htexBlipFill.fillRect = new Vector
  1017. {
  1018. t = fillRect.Attribute("t") != null ? int.Parse(fillRect.Attribute("t").Value) / 1000 : 0,
  1019. l = fillRect.Attribute("l") != null ? int.Parse(fillRect.Attribute("l").Value) / 1000 : 0,
  1020. r = fillRect.Attribute("r") != null ? int.Parse(fillRect.Attribute("r").Value) / 1000 : 0,
  1021. b = fillRect.Attribute("b") != null ? int.Parse(fillRect.Attribute("b").Value) / 1000 : 0
  1022. };
  1023. }
  1024. var alphaModFix = blipFill.Blip.GetTextByPath("a:alphaModFix");
  1025. if (alphaModFix != null)
  1026. {
  1027. if (alphaModFix.Attribute("amt") != null)
  1028. {
  1029. htexBlipFill.opacity = int.Parse(alphaModFix.Attribute("amt").Value) * 1.0 / 1000;
  1030. }
  1031. }
  1032. Tile tile = blipFill.GetFirstChild<Tile>();
  1033. if (tile != null)
  1034. {
  1035. ///algn (Alignment) flip (Tile Flipping) sx (Horizontal Ratio) sy (Vertical Ratio) tx (Horizontal Offset) ty (Vertical Offset)
  1036. }
  1037. var Embed = blipFill.Blip.Embed;
  1038. var Link = blipFill.Blip.Link;
  1039. //slide.SlideLayoutPart
  1040. IdPartPair part = null;
  1041. if (partForm == "slide") {
  1042. part = slide.SlidePart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  1043. }
  1044. if (partForm == "layout")
  1045. {
  1046. part = slide.SlideLayoutPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  1047. }
  1048. if (partForm == "master")
  1049. {
  1050. part = slide.slideMasterPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  1051. }
  1052. if (part != null)
  1053. {
  1054. var url = part.OpenXmlPart.Uri.ToString().Replace("../", "/ppt/");
  1055. var thumbnail = slide.Media.Where(x => x.Attributes().Select(y => y.Value == url).FirstOrDefault()).FirstOrDefault();
  1056. if (thumbnail != null)
  1057. {
  1058. //var contentType = thumbnail.Attribute("{http://schemas.microsoft.com/office/2006/xmlPackage}contentType").Value;
  1059. //var data = thumbnail.Value;
  1060. //url = "data:" + contentType + ";base64," + data;
  1061. //url = url.Replace("\r\n", "");
  1062. htexBlipFill.url = url;
  1063. // htexBlipFill.urlType = "base64";
  1064. // htexBlipFill.contentType = contentType;
  1065. }
  1066. }
  1067. //TODO 图片元素的更多信息需要后期继续实现,如滤镜,裁剪,图片颜色,图片校正等
  1068. return htexBlipFill;
  1069. }
  1070. /// <summary>
  1071. /// a:ln 图形的外边
  1072. /// </summary>
  1073. /// <param name="shapeProperties"></param>
  1074. /// <param name="slide"></param>
  1075. public static Border DoOutline(DocumentFormat.OpenXml.Drawing.LinePropertiesType outline, PPTSlide slide,string Shapetype) {
  1076. Border border = new Border() {color= new Fill { type=-1} };
  1077. //20.1.10.35 EMUs. 1 pt = 12700 EMUs.
  1078. double? Width = null;
  1079. if (outline == null && Shapetype== "CxnSp") {
  1080. return new Border { outline =new HtexOutline { Width=0.5} ,type= "solid",stroke="0" };
  1081. }
  1082. if (outline.Width != null)
  1083. {
  1084. Width = double.Parse(outline.Width) * 1.0 * Globals.px96 / Globals.px914400;
  1085. }
  1086. else {
  1087. if (Shapetype == "CxnSp") {
  1088. Width = 0.75;
  1089. }
  1090. }
  1091. // Center = 0,Insert = 1
  1092. //
  1093. //20.1.10.39
  1094. string algn = null;
  1095. if (outline.Alignment != null) {
  1096. algn = outline.Alignment;
  1097. }
  1098. /* 20.1.10.15 复合类型
  1099. Single = 0, 单线 默认
  1100. Double = 1, 双细线
  1101. ThickThin = 2, 粗到细
  1102. ThinThick = 3, 细到粗
  1103. Triple = 4 三线
  1104. */
  1105. string cmpd = null ;
  1106. if (outline.CompoundLineType!=null) {
  1107. cmpd = outline.CompoundLineType;
  1108. }
  1109. /* 20.1.10.31 线端类型
  1110. Round = 0, 方
  1111. Square = 1,圆
  1112. Flat = 2 平
  1113. */
  1114. string cap =null;
  1115. if (outline.CapType != null) {
  1116. cap = outline.CapType;
  1117. }
  1118. border.outline = new HtexOutline { Width = Width, algn = algn, cmpd = cmpd, cap = cap };
  1119. var gradFill = outline.GetFirstChild<GradientFill>();
  1120. if (gradFill != null)
  1121. {
  1122. border.color.type = 2;
  1123. border.color.gradientFill = DoGradientFill(gradFill, slide);
  1124. }
  1125. var noFill = outline.GetFirstChild<NoFill>();
  1126. if (noFill != null)
  1127. {
  1128. border.color.type = 0;
  1129. }
  1130. var pattFill = outline.GetFirstChild<PatternFill>();
  1131. if (pattFill != null)
  1132. {
  1133. HtexPattFill htexPattFill = DoPattFill(pattFill, slide);
  1134. border.color.type = 4;
  1135. border.color.pattFill = htexPattFill;
  1136. }
  1137. var solidFill = outline.GetFirstChild<SolidFill>();
  1138. if (solidFill != null)
  1139. {
  1140. border.color.type = 1;
  1141. border.color.solidFill = DoSolidFill(solidFill, slide);
  1142. }
  1143. if (Width!=null && Width > 0) {
  1144. border.type = "solid";
  1145. border.stroke = "0";
  1146. }
  1147. var prstDash= outline.GetFirstChild<PresetDash>(); //预设连接符
  1148. if (prstDash != null) {
  1149. // PresetDash
  1150. var (type, stroke) = DoPrstDash(prstDash);
  1151. border.type = type;
  1152. border.stroke = stroke;
  1153. }
  1154. if (outline.GetFirstChild<Bevel>() != null) {
  1155. // 棱台切角
  1156. border.corner = "bevel";
  1157. }
  1158. if (outline.GetFirstChild<Round>()!=null) {
  1159. //圆角
  1160. border.corner = "round";
  1161. }
  1162. if (outline.GetFirstChild<Miter>() != null) {
  1163. //斜角
  1164. border.corner = "miter";
  1165. }
  1166. var headEndNodeAttrs = outline.GetFirstChild<HeadEnd>(); //连接符开始符号
  1167. if (headEndNodeAttrs != null)
  1168. {
  1169. border.headEnd = headEndNodeAttrs.Type;
  1170. }
  1171. var tailEndNodeAttrs = outline.GetFirstChild<TailEnd>();//末端连接符
  1172. if (tailEndNodeAttrs != null)
  1173. {
  1174. border.tailEnd = tailEndNodeAttrs.Type;
  1175. }
  1176. //TODO
  1177. //outline.GetTextByPath("//a:extLst");
  1178. var custDash = outline.GetFirstChild<CustomDash>();//自定义连接线
  1179. if (custDash != null) {
  1180. var(type, stroke) = DoCustDash(custDash);
  1181. border.type = type;
  1182. border.stroke = stroke;
  1183. }
  1184. if (border.color != null && (border.color.type !=null || border.color.type !=1) && border.outline.Width==null) {
  1185. border.outline.Width = 1;
  1186. if (border.type == null) {
  1187. border.type = "solid";
  1188. border.stroke = "0";
  1189. }
  1190. }
  1191. return border;
  1192. }
  1193. /// <summary>
  1194. ///自定义连线符
  1195. /// </summary>
  1196. /// <param name="custDash"></param>
  1197. /// <returns></returns>
  1198. public static (string type, string stroke) DoCustDash(CustomDash custDash) {
  1199. string type = "cust";
  1200. string stroke = "0";
  1201. IEnumerable<XElement> elements= custDash.GetTextByPathList("ds");
  1202. if (elements != null) {
  1203. foreach (XElement element in elements) {
  1204. var d= element.Attribute("d");//线段长度
  1205. if (d != null) {
  1206. stroke =stroke+ d.Value + ",";
  1207. }
  1208. var sp= element.Attribute("sp");//空白间距长度
  1209. if (sp != null) {
  1210. stroke =stroke+ sp.Value + ",";
  1211. }
  1212. }
  1213. }
  1214. if (string.IsNullOrEmpty(stroke)) {
  1215. stroke = "0";
  1216. }
  1217. if (stroke.EndsWith(",")) {
  1218. stroke.Substring(0, stroke.Length - 1);
  1219. }
  1220. return (type,stroke);
  1221. }
  1222. /// <summary>
  1223. /// 预设连线符
  1224. /// </summary>
  1225. /// <param name="presetDash"></param>
  1226. /// <returns></returns>
  1227. public static (string type, string stroke) DoPrstDash(PresetDash presetDash)
  1228. {
  1229. Border border = new Border() { type= "solid",stroke="0"
  1230. };
  1231. //PresetLineDashValues 枚举
  1232. string val = presetDash.Val;
  1233. switch (val)
  1234. {
  1235. case "solid":
  1236. border.type = "solid";
  1237. border.stroke = "0";
  1238. break;
  1239. case "dash":
  1240. border.type += "dashed";
  1241. border.stroke = "5";
  1242. break;
  1243. case "dashDot":
  1244. border.type = "dashed";
  1245. border.stroke = "5, 5, 1, 5";
  1246. break;
  1247. case "dot":
  1248. border.type = "dotted";
  1249. border.stroke = "1, 5";
  1250. break;
  1251. case "lgDash":
  1252. border.type = "dashed";
  1253. border.stroke = "10, 5";
  1254. break;
  1255. case "lgDashDotDot":
  1256. border.type = "dashed";
  1257. border.stroke = "10, 5, 1, 5, 1, 5";
  1258. break;
  1259. case "sysDash":
  1260. border.type = "dashed";
  1261. border.stroke = "5, 2";
  1262. break;
  1263. case "sysDashDot":
  1264. border.type = "dashed";
  1265. border.stroke = "5, 2, 1, 5";
  1266. break;
  1267. case "sysDashDotDot":
  1268. border.type = "dashed";
  1269. border.stroke = "5, 2, 1, 5, 1, 5";
  1270. break;
  1271. case "sysDot":
  1272. border.type = "dotted";
  1273. border.stroke = "2, 5";
  1274. break;
  1275. case null:
  1276. //console.log(borderType);
  1277. default:
  1278. border.type = "solid";
  1279. border.stroke = "0";
  1280. break;
  1281. }
  1282. return (border.type, border.stroke);
  1283. }
  1284. public static HtexPattFill DoPattFill(DocumentFormat.OpenXml.Drawing.PatternFill patternFill , PPTSlide slide) {
  1285. HtexPattFill htexPattFill = new HtexPattFill();
  1286. Color FgClr ;
  1287. Color BgClr ;
  1288. if (patternFill.ForegroundColor != null)
  1289. {
  1290. var fgClr = ColorTypeColors(patternFill.ForegroundColor, slide);
  1291. htexPattFill.fgClr = fgClr;
  1292. FgClr= ColorTranslator.FromHtml(fgClr);
  1293. }
  1294. if (patternFill.BackgroundColor != null)
  1295. {
  1296. var bgClr = ColorTypeColors(patternFill.BackgroundColor, slide);
  1297. htexPattFill.bgClr = bgClr;
  1298. BgClr= ColorTranslator.FromHtml(bgClr);
  1299. }
  1300. htexPattFill.prst = patternFill.Preset.ToString();
  1301. MemoryStream ms = new MemoryStream();
  1302. System.Drawing.Bitmap bm = PatternFillHelper.GetVmlPatternFill(patternFill.Preset, FgClr, BgClr);
  1303. Graphics graphics = Graphics.FromImage(bm);
  1304. graphics.Save();
  1305. graphics.Dispose();
  1306. bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  1307. byte[] arr = new byte[ms.Length];
  1308. ms.Position = 0;
  1309. ms.Read(arr, 0, (int)ms.Length);
  1310. ms.Close();
  1311. bm.Dispose();
  1312. string sImageData = "data:image/png;base64,"+ Convert.ToBase64String(arr);
  1313. htexPattFill.base64 = sImageData;
  1314. return htexPattFill;
  1315. }
  1316. /// <summary>
  1317. /// 渐变颜色
  1318. /// GradientStopList (渐变停止列表)(默认)
  1319. /// LinearGradientFill(线性渐变填充)
  1320. /// PathGradientFill (路径渐变)根据具体形状描边式
  1321. /// TileRectangle (平铺矩形) 右下角是一个举行
  1322. /// </summary>
  1323. /// <param name="GradientFill"></param>
  1324. /// <returns></returns>
  1325. public static HtexGradientFill DoGradientFill(DocumentFormat.OpenXml.Drawing.GradientFill GradientFill, PPTSlide slide)
  1326. {
  1327. HtexGradientFill gradientFill = new HtexGradientFill();
  1328. ///右下角弧形模式
  1329. string type = "";
  1330. ///平铺翻转模式
  1331. ///Horizontal 水平
  1332. ///HorizontalAndVertical 水平和垂直。
  1333. ///Vertical 垂直
  1334. ///None 无
  1335. string name = "None";
  1336. if (GradientFill.Flip != null) {
  1337. name = GradientFill.Flip.Value.ToString();
  1338. }
  1339. gradientFill.flip = name;
  1340. List<ColorPosition> colors = new List<ColorPosition>();
  1341. bool RotateWithShape = true;
  1342. if (GradientFill.RotateWithShape != null)
  1343. {
  1344. RotateWithShape = GradientFill.RotateWithShape;
  1345. }
  1346. gradientFill.RotateWithShape = RotateWithShape;
  1347. foreach (GradientStop gradientStop in GradientFill.GradientStopList)
  1348. {
  1349. colors.Add(new ColorPosition
  1350. {
  1351. Color = ColorTypeColors(gradientStop, slide),
  1352. Position = gradientStop.Position *1.0/ 1000 /// 位置,色带的位置 百分比0-100
  1353. });
  1354. }
  1355. gradientFill.colors = colors;
  1356. var lin = GradientFill.GetTextByPath("//a:lin");
  1357. double? rot = null;
  1358. string scaled = "";
  1359. //线性模式
  1360. if (lin != null) {
  1361. rot = 90;
  1362. type = "line";
  1363. var ang = lin.Attribute("ang");
  1364. var scaledNode = lin.Attribute("scaled");
  1365. if (ang != null) {
  1366. rot= ShapeHelper.AngleToDegrees(ang.Value);
  1367. }
  1368. if (scaledNode != null) {
  1369. scaled = scaledNode.Value;
  1370. }
  1371. gradientFill.line = new LineFill { rot = rot, scaled = scaled };
  1372. }
  1373. var path = GradientFill.GetTextByPath("//a:path");
  1374. if (path != null) {
  1375. type = "path";
  1376. ///circle
  1377. ///rect
  1378. ///shape
  1379. var pathType= path.Attribute("path").Value;
  1380. var fillToRect=path.GetTextByPath("//a:fillToRect");
  1381. if (fillToRect != null) {
  1382. Vector vector= new Vector();
  1383. var bnode = fillToRect.Attribute("b");
  1384. var lnode = fillToRect.Attribute("l");
  1385. var rnode = fillToRect.Attribute("r");
  1386. var tnode = fillToRect.Attribute("t");
  1387. if (bnode != null)
  1388. {
  1389. vector.b= int.Parse(bnode.Value) / 1000;
  1390. }
  1391. if (lnode != null)
  1392. {
  1393. vector.l = int.Parse(lnode.Value) / 1000;
  1394. }
  1395. if (rnode != null)
  1396. {
  1397. vector.r = int.Parse(rnode.Value) / 1000;
  1398. }
  1399. if (tnode != null)
  1400. {
  1401. vector.t = int.Parse(tnode.Value) / 1000;
  1402. }
  1403. gradientFill.path =new PathFill {fillToRect= vector,pathType= pathType } ;
  1404. }
  1405. }
  1406. var tileRect = GradientFill.GetTextByPath("//a:tileRect");
  1407. if (tileRect != null) {
  1408. ///四个方向的偏移量,矩形的下边缘。例如50000/1000= 50% ,辐射百分比
  1409. Vector vector = new Vector();
  1410. var bnode = tileRect.Attribute("b");
  1411. var lnode = tileRect.Attribute("l");
  1412. var rnode = tileRect.Attribute("r");
  1413. var tnode = tileRect.Attribute("t");
  1414. if (bnode != null)
  1415. {
  1416. vector.b = int.Parse(bnode.Value) / 1000;
  1417. }
  1418. if (lnode != null)
  1419. {
  1420. vector.l = int.Parse(lnode.Value) / 1000;
  1421. }
  1422. if (rnode != null)
  1423. {
  1424. vector.r = int.Parse(rnode.Value) / 1000;
  1425. }
  1426. if (tnode != null)
  1427. {
  1428. vector.t = int.Parse(tnode.Value) / 1000;
  1429. }
  1430. gradientFill.tileRect = vector;
  1431. }
  1432. gradientFill.type = type;
  1433. return gradientFill;
  1434. }
  1435. public static string ColorTypeColors(GradientStop Color, PPTSlide slide)
  1436. {
  1437. string FontColor = "";
  1438. if (Color.RgbColorModelHex != null)
  1439. {
  1440. FontColor = ReadColor(Color.RgbColorModelHex);
  1441. }
  1442. else if (Color.RgbColorModelPercentage != null)
  1443. {
  1444. FontColor = ReadColor(Color.RgbColorModelPercentage);
  1445. }
  1446. else if (Color.SystemColor != null)
  1447. {
  1448. FontColor = ReadColor(Color.SystemColor, slide);
  1449. }
  1450. if (Color.SchemeColor != null)
  1451. {
  1452. FontColor = ReadColor(Color.SchemeColor, slide);
  1453. }
  1454. if (Color.HslColor != null)
  1455. {
  1456. FontColor = ReadColor(Color.HslColor);
  1457. }
  1458. if (Color.PresetColor != null)
  1459. {
  1460. FontColor = ReadColor(Color.PresetColor);
  1461. }
  1462. return FontColor;
  1463. }
  1464. public static string ColorTypeColors(ColorType Color, PPTSlide slide)
  1465. {
  1466. string FontColor = "";
  1467. if (Color.RgbColorModelHex != null)
  1468. {
  1469. FontColor = ReadColor(Color.RgbColorModelHex);
  1470. }
  1471. else if (Color.RgbColorModelPercentage != null)
  1472. {
  1473. FontColor = ReadColor(Color.RgbColorModelPercentage);
  1474. }
  1475. else if (Color.SystemColor != null)
  1476. {
  1477. FontColor = ReadColor(Color.SystemColor, slide);
  1478. }
  1479. if (Color.SchemeColor != null)
  1480. {
  1481. FontColor = ReadColor(Color.SchemeColor, slide);
  1482. }
  1483. if (Color.HslColor != null)
  1484. {
  1485. FontColor = ReadColor(Color.HslColor);
  1486. }
  1487. if (Color.PresetColor != null)
  1488. {
  1489. FontColor = ReadColor(Color.PresetColor);
  1490. }
  1491. return FontColor;
  1492. }
  1493. public static string FontReferenceColors(FontReference Color, PPTSlide slide)
  1494. {
  1495. string FontColor = "";
  1496. if (Color.RgbColorModelHex != null)
  1497. {
  1498. FontColor = ReadColor(Color.RgbColorModelHex);
  1499. }
  1500. else if (Color.RgbColorModelPercentage != null)
  1501. {
  1502. FontColor = ReadColor(Color.RgbColorModelPercentage);
  1503. }
  1504. else if (Color.SystemColor != null)
  1505. {
  1506. FontColor = ReadColor(Color.SystemColor, slide);
  1507. }
  1508. if (Color.SchemeColor != null)
  1509. {
  1510. FontColor = ReadColor(Color.SchemeColor, slide);
  1511. }
  1512. if (Color.HslColor != null)
  1513. {
  1514. FontColor = ReadColor(Color.HslColor);
  1515. }
  1516. if (Color.PresetColor != null)
  1517. {
  1518. FontColor = ReadColor(Color.PresetColor);
  1519. }
  1520. return FontColor;
  1521. }
  1522. public static string DoMatrixReferenceColors(StyleMatrixReferenceType Color, PPTSlide slide) {
  1523. string FontColor = "";
  1524. if (Color == null) {
  1525. return FontColor;
  1526. }
  1527. if (Color.RgbColorModelHex != null)
  1528. {
  1529. FontColor = ReadColor(Color.RgbColorModelHex);
  1530. }
  1531. else if (Color.RgbColorModelPercentage != null)
  1532. {
  1533. FontColor = ReadColor(Color.RgbColorModelPercentage);
  1534. }
  1535. else if (Color.SystemColor != null)
  1536. {
  1537. FontColor = ReadColor(Color.SystemColor, slide);
  1538. }
  1539. if (Color.SchemeColor != null)
  1540. {
  1541. FontColor = ReadColor(Color.SchemeColor, slide);
  1542. }
  1543. if (Color.HslColor != null)
  1544. {
  1545. FontColor = ReadColor(Color.HslColor);
  1546. }
  1547. if (Color.PresetColor != null)
  1548. {
  1549. FontColor = ReadColor(Color.PresetColor);
  1550. }
  1551. return FontColor;
  1552. }
  1553. public static string DoSolidFill(SolidFill Color, PPTSlide slide)
  1554. {
  1555. string FontColor = "";
  1556. if (Color.RgbColorModelHex != null)
  1557. {
  1558. FontColor = ReadColor(Color.RgbColorModelHex);
  1559. }
  1560. else if (Color.RgbColorModelPercentage != null)
  1561. {
  1562. FontColor = ReadColor(Color.RgbColorModelPercentage);
  1563. }
  1564. else if (Color.SystemColor != null)
  1565. {
  1566. FontColor = ReadColor(Color.SystemColor, slide);
  1567. }
  1568. if (Color.SchemeColor != null)
  1569. {
  1570. FontColor = ReadColor(Color.SchemeColor, slide);
  1571. }
  1572. if (Color.HslColor != null)
  1573. {
  1574. FontColor = ReadColor(Color.HslColor);
  1575. }
  1576. if (Color.PresetColor != null)
  1577. {
  1578. FontColor= ReadColor(Color.PresetColor);
  1579. }
  1580. return FontColor;
  1581. }
  1582. public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelHex RgbColorModelHex)
  1583. {
  1584. string FontColor = "";
  1585. FontColor = "#" + RgbColorModelHex.Val;
  1586. FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelHex.OuterXml, FontColor.Replace("#", ""));
  1587. return FontColor;
  1588. }
  1589. public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelPercentage RgbColorModelPercentage)
  1590. {
  1591. string FontColor = "";
  1592. int red =RgbColorModelPercentage.RedPortion.Value;
  1593. int green = RgbColorModelPercentage.GreenPortion.Value;
  1594. int blue = RgbColorModelPercentage.BluePortion.Value;
  1595. var colorFromRGB = System.Drawing.Color.FromArgb(red, green, blue);
  1596. string fontcolor = System.Drawing.ColorTranslator.ToHtml(colorFromRGB);
  1597. FontColor = fontcolor;
  1598. FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelPercentage.OuterXml, FontColor.Replace("#", ""));
  1599. return FontColor;
  1600. }
  1601. public static string ReadColor(DocumentFormat.OpenXml.Drawing.SystemColor SystemColor, PPTSlide slide)
  1602. {
  1603. string FontColor = "";
  1604. FontColor = "#" + SystemColor.LastColor.Value;
  1605. FontColor = "#" + ShapeHelper.ColorToning(SystemColor.OuterXml, FontColor.Replace("#", ""));
  1606. return FontColor;
  1607. }
  1608. public static string ReadColor(DocumentFormat.OpenXml.Drawing.SchemeColor SchemeColor,PPTSlide slide) {
  1609. string FontColor = "";
  1610. FontColor = DoSchemeColor(SchemeColor, slide);
  1611. FontColor = "#" + ShapeHelper.ColorToning(SchemeColor.OuterXml, FontColor.Replace("#", ""));
  1612. return FontColor;
  1613. }
  1614. public static string ReadColor(DocumentFormat.OpenXml.Drawing.HslColor HslColor)
  1615. {
  1616. string FontColor = "";
  1617. var hue =HslColor.HueValue / 100000;
  1618. var sat = HslColor.SatValue / 100;
  1619. var lum =HslColor.LumValue / 100;
  1620. var hsl2rgb = ShapeHelper.HslToRgb(hue, sat, lum);
  1621. FontColor = "#" + ShapeHelper.ToHex(hsl2rgb.r) + ShapeHelper.ToHex(hsl2rgb.g) + ShapeHelper.ToHex(hsl2rgb.b);
  1622. FontColor = "#" + ShapeHelper.ColorToning(HslColor.OuterXml, FontColor.Replace("#", ""));
  1623. return FontColor;
  1624. }
  1625. public static string ReadColor(PresetColor PresetColor) {
  1626. string FontColor = "";
  1627. //https://m.fontke.com/tool/rgb/0000ff/
  1628. //判断是否是拿到名字 还是值
  1629. FontColor = ShapeHelper.GetColorName2Hex(PresetColor.Val);
  1630. FontColor = "#" + ShapeHelper.ColorToning(PresetColor.OuterXml, FontColor.Replace("#", ""));
  1631. //TODO
  1632. return FontColor;
  1633. }
  1634. public static string DoTextFontType(TextFontType latinFonts, PPTSlide slide)
  1635. {
  1636. string FontFamily = "";
  1637. FontScheme allSchemeFonts =
  1638. slide.SlideLayoutPart.SlideMasterPart.ThemePart.Theme
  1639. .ThemeElements.FontScheme;
  1640. var minorFont = allSchemeFonts.GetFirstChild<MinorFont>();
  1641. var majorFont = allSchemeFonts.GetFirstChild<MajorFont>();
  1642. if (latinFonts != null)
  1643. {
  1644. FontFamily = latinFonts.Typeface.Value;
  1645. if (latinFonts.Typeface.Value.Equals("+mj-lt"))
  1646. {
  1647. FontFamily = majorFont.LatinFont.Typeface.Value;
  1648. }
  1649. else if (latinFonts.Typeface.Value.Equals("+mj-cs"))
  1650. {
  1651. FontFamily = majorFont.ComplexScriptFont.Typeface.Value;
  1652. }
  1653. else if (latinFonts.Typeface.Value.Equals("+mj-ea"))
  1654. {
  1655. FontFamily = majorFont.EastAsianFont.Typeface.Value;
  1656. }
  1657. else if (latinFonts.Typeface.Value.Equals("+mn-lt"))
  1658. {
  1659. FontFamily = minorFont.LatinFont.Typeface.Value;
  1660. }
  1661. else if (latinFonts.Typeface.Value.Equals("+mn-cs"))
  1662. {
  1663. FontFamily = minorFont.ComplexScriptFont.Typeface.Value;
  1664. }
  1665. else if (latinFonts.Typeface.Value.Equals("+mn-ea"))
  1666. {
  1667. FontFamily = minorFont.EastAsianFont.Typeface.Value;
  1668. }
  1669. }
  1670. return FontFamily;
  1671. }
  1672. public static string DoSchemeColor(SchemeColor schemeColor, PPTSlide slide)
  1673. {
  1674. var light1Color1= slide.SlideLayoutPart.SlideMasterPart.
  1675. ThemePart;
  1676. string FontColor = "";
  1677. if (schemeColor.Val == "bg1")
  1678. {
  1679. Light1Color light1Color = slide.SlideLayoutPart.SlideMasterPart.
  1680. ThemePart.Theme.ThemeElements.ColorScheme.Light1Color;
  1681. if (light1Color.RgbColorModelHex != null)
  1682. {
  1683. FontColor = "#" + light1Color.RgbColorModelHex.Val.Value;
  1684. }
  1685. else if (light1Color.SystemColor != null)
  1686. {
  1687. FontColor = "#" + light1Color.SystemColor.LastColor.Value;
  1688. }
  1689. }
  1690. else if (schemeColor.Val == "bg2")
  1691. {
  1692. Light2Color light2Color = slide.SlideLayoutPart.SlideMasterPart.
  1693. ThemePart.Theme.ThemeElements.ColorScheme.Light2Color;
  1694. if (light2Color.RgbColorModelHex != null)
  1695. {
  1696. FontColor = "#" + light2Color.RgbColorModelHex.Val.Value;
  1697. }
  1698. else if (light2Color.SystemColor != null)
  1699. {
  1700. FontColor = "#" + light2Color.SystemColor.LastColor.Value;
  1701. }
  1702. }
  1703. else if (schemeColor.Val == "tx1")
  1704. {
  1705. Dark1Color dark1Color = slide.SlideLayoutPart.SlideMasterPart.
  1706. ThemePart.Theme.ThemeElements.ColorScheme.Dark1Color;
  1707. if (dark1Color.RgbColorModelHex != null)
  1708. {
  1709. FontColor = "#" + dark1Color.RgbColorModelHex.Val.Value;
  1710. }
  1711. else if (dark1Color.SystemColor != null)
  1712. {
  1713. FontColor = "#" + dark1Color.SystemColor.LastColor.Value;
  1714. }
  1715. }
  1716. else if (schemeColor.Val == "tx2")
  1717. {
  1718. Dark2Color dark2Color = slide.SlideLayoutPart.SlideMasterPart.
  1719. ThemePart.Theme.ThemeElements.ColorScheme.Dark2Color;
  1720. if (dark2Color.RgbColorModelHex != null)
  1721. {
  1722. FontColor = "#" + dark2Color.RgbColorModelHex.Val.Value;
  1723. }
  1724. else if (dark2Color.SystemColor != null)
  1725. {
  1726. FontColor = "#" + dark2Color.SystemColor.LastColor.Value;
  1727. }
  1728. }
  1729. else if (schemeColor.Val == "hlink")
  1730. {
  1731. Hyperlink HyperLink = slide.SlideLayoutPart.SlideMasterPart.
  1732. ThemePart.Theme.ThemeElements.ColorScheme.Hyperlink;
  1733. if (HyperLink.RgbColorModelHex != null)
  1734. {
  1735. FontColor = "#" + HyperLink.RgbColorModelHex.Val.Value;
  1736. }
  1737. }
  1738. else if (schemeColor.Val == "folHlink")
  1739. {
  1740. FollowedHyperlinkColor folHyperLink = slide.SlideLayoutPart.SlideMasterPart.
  1741. ThemePart.Theme.ThemeElements.ColorScheme.FollowedHyperlinkColor;
  1742. if (folHyperLink.RgbColorModelHex != null)
  1743. {
  1744. FontColor = "#" + folHyperLink.RgbColorModelHex.Val.Value;
  1745. }
  1746. }
  1747. if (string.IsNullOrEmpty(FontColor)) {
  1748. FontColor = ReadAccentSchemeColors(schemeColor, slide);
  1749. }
  1750. if (FontColor.Replace("#", "") == "") {
  1751. var chren= slide.SlideLayoutPart.SlideMasterPart.ThemePart.Theme.ThemeElements.ColorScheme.ChildElements;
  1752. foreach(var cl in chren) {
  1753. if (schemeColor.Val == cl.LocalName) {
  1754. if (schemeColor.Val == "dk1" || schemeColor.Val == "lt1")
  1755. {
  1756. if (cl.GetFirstChild<SystemColor>() != null)
  1757. {
  1758. FontColor = "#" + cl.GetFirstChild<SystemColor>().LastColor;
  1759. }
  1760. else {
  1761. FontColor = "#" + cl.GetFirstChild<RgbColorModelHex>().Val.Value; ;
  1762. }
  1763. }
  1764. else {
  1765. FontColor ="#"+cl.GetFirstChild<RgbColorModelHex>().Val.Value; ;
  1766. }
  1767. }
  1768. }
  1769. }
  1770. return FontColor;
  1771. }
  1772. public static string ReadAccentSchemeColors(SchemeColor schemeColor, PPTSlide slide)
  1773. {
  1774. string FontColor = "";
  1775. for (int i = 1; i <= 6; i++)
  1776. {
  1777. if (schemeColor.Val == "accent" + i)
  1778. {
  1779. switch (i)
  1780. {
  1781. case 1:
  1782. {
  1783. Accent1Color c = slide.SlideLayoutPart.SlideMasterPart.
  1784. ThemePart.Theme.ThemeElements.ColorScheme.Accent1Color;
  1785. if (c.RgbColorModelHex != null)
  1786. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1787. }
  1788. break;
  1789. case 2:
  1790. {
  1791. Accent2Color c = slide.SlideLayoutPart.SlideMasterPart.
  1792. ThemePart.Theme.ThemeElements.ColorScheme.Accent2Color;
  1793. if (c.RgbColorModelHex != null)
  1794. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1795. }
  1796. break;
  1797. case 3:
  1798. {
  1799. Accent3Color c = slide.SlideLayoutPart.SlideMasterPart.
  1800. ThemePart.Theme.ThemeElements.ColorScheme.Accent3Color;
  1801. if (c.RgbColorModelHex != null)
  1802. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1803. }
  1804. break;
  1805. case 4:
  1806. {
  1807. Accent4Color c = slide.SlideLayoutPart.SlideMasterPart.
  1808. ThemePart.Theme.ThemeElements.ColorScheme.Accent4Color;
  1809. if (c.RgbColorModelHex != null)
  1810. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1811. }
  1812. break;
  1813. case 5:
  1814. {
  1815. Accent5Color c = slide.SlideLayoutPart.SlideMasterPart.
  1816. ThemePart.Theme.ThemeElements.ColorScheme.Accent5Color;
  1817. if (c.RgbColorModelHex != null)
  1818. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1819. }
  1820. break;
  1821. case 6:
  1822. {
  1823. Accent6Color c = slide.SlideLayoutPart.SlideMasterPart.
  1824. ThemePart.Theme.ThemeElements.ColorScheme.Accent6Color;
  1825. if (c.RgbColorModelHex != null)
  1826. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  1827. }
  1828. break;
  1829. default: { } break;
  1830. }
  1831. }
  1832. }
  1833. return FontColor;
  1834. }
  1835. }
  1836. }