PPTXHelper.cs 64 KB

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