WORDHelper.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. using DocumentFormat.OpenXml.Drawing;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using HTEXLib.Helpers.ColorHelpers;
  4. using HTEXLib.Helpers.ShapeHelpers;
  5. using HTEXLib.Models.Inner;
  6. using OpenXmlPowerTools;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Xml.Linq;
  16. using System.Xml.XPath;
  17. namespace HTEXLib.DOCX.OpenXmlTool
  18. {
  19. public static class WORDHelper
  20. {
  21. public static XmlNamespaceManager xmlnsManager;
  22. public static IEnumerable<XElement> GetWordNodeListByPath(this XElement xElement, string expression)
  23. {
  24. return xElement.XPathSelectElements(expression, xmlnsManager);
  25. }
  26. public static XElement GetWordNodeByPath(this XElement xElement, string expression)
  27. {
  28. return xElement.XPathSelectElement(expression, xmlnsManager);
  29. }
  30. static WORDHelper()
  31. {
  32. xmlnsManager = new XmlNamespaceManager(new NameTable());
  33. xmlnsManager.AddNamespace("activex", "http://schemas.microsoft.com/office/2006/activeX");
  34. xmlnsManager.AddNamespace("biblio", "http://schemas.microsoft.com/office/word/2004/10/bibliography");
  35. xmlnsManager.AddNamespace("ink", "http://schemas.microsoft.com/ink/2010/main");
  36. xmlnsManager.AddNamespace("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
  37. xmlnsManager.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
  38. xmlnsManager.AddNamespace("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
  39. xmlnsManager.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
  40. xmlnsManager.AddNamespace("cdr", "http://schemas.openxmlformats.org/drawingml/2006/chartDrawing");
  41. xmlnsManager.AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  42. xmlnsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
  43. xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
  44. xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
  45. xmlnsManager.AddNamespace("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
  46. xmlnsManager.AddNamespace("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
  47. xmlnsManager.AddNamespace("cx", "http://schemas.microsoft.com/office/drawing/2014/chartex");
  48. xmlnsManager.AddNamespace("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex");
  49. xmlnsManager.AddNamespace("cx2", "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex");
  50. xmlnsManager.AddNamespace("cx3", "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex");
  51. xmlnsManager.AddNamespace("cx4", "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex");
  52. xmlnsManager.AddNamespace("cx5", "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex");
  53. xmlnsManager.AddNamespace("cx6", "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex");
  54. xmlnsManager.AddNamespace("cx7", "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex");
  55. xmlnsManager.AddNamespace("cx8", "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex");
  56. xmlnsManager.AddNamespace("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  57. xmlnsManager.AddNamespace("aink", "http://schemas.microsoft.com/office/drawing/2016/ink");
  58. xmlnsManager.AddNamespace("am3d", "http://schemas.microsoft.com/office/drawing/2017/model3d");
  59. xmlnsManager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
  60. xmlnsManager.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  61. xmlnsManager.AddNamespace("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
  62. xmlnsManager.AddNamespace("v", "urn:schemas-microsoft-com:vml");
  63. xmlnsManager.AddNamespace("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
  64. xmlnsManager.AddNamespace("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
  65. xmlnsManager.AddNamespace("w10", "urn:schemas-microsoft-com:office:word");
  66. xmlnsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
  67. xmlnsManager.AddNamespace("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
  68. xmlnsManager.AddNamespace("w15", "http://schemas.microsoft.com/office/word/2012/wordml");
  69. xmlnsManager.AddNamespace("w16cex", "http://schemas.microsoft.com/office/word/2018/wordml/cex");
  70. xmlnsManager.AddNamespace("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid");
  71. xmlnsManager.AddNamespace("w16", "http://schemas.microsoft.com/office/word/2018/wordml");
  72. xmlnsManager.AddNamespace("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex");
  73. xmlnsManager.AddNamespace("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
  74. xmlnsManager.AddNamespace("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
  75. xmlnsManager.AddNamespace("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
  76. xmlnsManager.AddNamespace("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
  77. xmlnsManager.AddNamespace("pt14", "http://powertools.codeplex.com/2011");
  78. }
  79. /// <summary>
  80. /// 处理 LineReference 轮廓 a:lnRef
  81. /// 处理 FillReference 填充 a:fillRef
  82. /// 处理 EffectReference 效果 a:effectRef
  83. /// 处理 FontReference 字体 a:fontRef
  84. /// </summary>
  85. /// <param name="shapeStyle"></param>
  86. public static SlideColor DoShapeStyle(DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeStyle shapeStyle, WordprocessingDocument wordDoc)
  87. {
  88. if (shapeStyle == null)
  89. {
  90. return null;
  91. }
  92. string LineColor = DoMatrixReferenceColors(shapeStyle.LineReference, wordDoc);
  93. string FillColor = DoMatrixReferenceColors(shapeStyle.FillReference, wordDoc);
  94. string EffectColor = DoMatrixReferenceColors(shapeStyle.EffectReference, wordDoc);
  95. string FontColor = FontReferenceColors(shapeStyle.FontReference, wordDoc);
  96. return new SlideColor { LineColor = LineColor, FillColor = FillColor, EffectColor = EffectColor, FontColor = FontColor };
  97. }
  98. public static string DoMatrixReferenceColors(StyleMatrixReferenceType Color, WordprocessingDocument slide)
  99. {
  100. string FontColor = "";
  101. if (Color == null)
  102. {
  103. return FontColor;
  104. }
  105. if (Color.RgbColorModelHex != null)
  106. {
  107. FontColor = ReadColor(Color.RgbColorModelHex);
  108. }
  109. else if (Color.RgbColorModelPercentage != null)
  110. {
  111. FontColor = ReadColor(Color.RgbColorModelPercentage);
  112. }
  113. else if (Color.SystemColor != null)
  114. {
  115. FontColor = ReadColor(Color.SystemColor);
  116. }
  117. if (Color.SchemeColor != null)
  118. {
  119. FontColor = ReadColor(Color.SchemeColor, slide);
  120. }
  121. if (Color.HslColor != null)
  122. {
  123. FontColor = ReadColor(Color.HslColor);
  124. }
  125. if (Color.PresetColor != null)
  126. {
  127. FontColor = ReadColor(Color.PresetColor);
  128. }
  129. return FontColor;
  130. }
  131. public static string FontReferenceColors(FontReference Color, WordprocessingDocument slide)
  132. {
  133. string FontColor = "";
  134. if (Color.RgbColorModelHex != null)
  135. {
  136. FontColor = ReadColor(Color.RgbColorModelHex);
  137. }
  138. else if (Color.RgbColorModelPercentage != null)
  139. {
  140. FontColor = ReadColor(Color.RgbColorModelPercentage);
  141. }
  142. else if (Color.SystemColor != null)
  143. {
  144. FontColor = ReadColor(Color.SystemColor);
  145. }
  146. if (Color.SchemeColor != null)
  147. {
  148. FontColor = ReadColor(Color.SchemeColor, slide);
  149. }
  150. if (Color.HslColor != null)
  151. {
  152. FontColor = ReadColor(Color.HslColor);
  153. }
  154. if (Color.PresetColor != null)
  155. {
  156. FontColor = ReadColor(Color.PresetColor);
  157. }
  158. return FontColor;
  159. }
  160. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.Charts.ShapeProperties shapeProperties, XElement element, Func<ImageInfo, XElement> ImageHandler, Position position)
  161. {
  162. if (shapeProperties == null)
  163. {
  164. return null;
  165. }
  166. //Outline
  167. var lnNode = shapeProperties.GetFirstChild<Outline>();
  168. Border border = null;
  169. if (lnNode != null)
  170. {
  171. border = DoOutline(lnNode, wordDoc);
  172. }
  173. Fill fill = new Fill() { type = -1 };
  174. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  175. if (gradFill != null)
  176. {
  177. fill.type = 2;
  178. fill.gradientFill = DoGradientFill(gradFill, wordDoc);
  179. }
  180. var noFill = shapeProperties.GetFirstChild<NoFill>();
  181. if (noFill != null)
  182. {
  183. fill.type = 0;
  184. }
  185. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  186. if (pattFill != null)
  187. {
  188. HtexPattFill htexPattFill = DoPattFill(pattFill, wordDoc);
  189. fill.type = 4;
  190. fill.pattFill = htexPattFill;
  191. }
  192. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  193. if (solidFill != null)
  194. {
  195. fill.type = 1;
  196. fill.solidFill = DoSolidFill(solidFill);
  197. }
  198. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  199. if (groupFill != null)
  200. {
  201. fill.type = 5;
  202. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  203. }
  204. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  205. if (blipFill != null)
  206. {
  207. fill.type = 3;
  208. fill.blipFill = DoBlipFill(wordDoc, blipFill, ImageHandler, element, position);
  209. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  210. }
  211. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  212. // var effect = doEffect(EffectList,partForm);
  213. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  214. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  215. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  216. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  217. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */};
  218. }
  219. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.Charts.ChartShapeProperties shapeProperties, XElement element, Func<ImageInfo, XElement> ImageHandler, Position position)
  220. {
  221. if (shapeProperties == null)
  222. {
  223. return null;
  224. }
  225. //Outline
  226. var lnNode = shapeProperties.GetFirstChild<Outline>();
  227. Border border = null;
  228. if (lnNode != null)
  229. {
  230. border = DoOutline(lnNode, wordDoc);
  231. }
  232. Fill fill = new Fill() { type = -1 };
  233. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  234. if (gradFill != null)
  235. {
  236. fill.type = 2;
  237. fill.gradientFill = DoGradientFill(gradFill, wordDoc);
  238. }
  239. var noFill = shapeProperties.GetFirstChild<NoFill>();
  240. if (noFill != null)
  241. {
  242. fill.type = 0;
  243. }
  244. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  245. if (pattFill != null)
  246. {
  247. HtexPattFill htexPattFill = DoPattFill(pattFill, wordDoc);
  248. fill.type = 4;
  249. fill.pattFill = htexPattFill;
  250. }
  251. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  252. if (solidFill != null)
  253. {
  254. fill.type = 1;
  255. fill.solidFill = DoSolidFill(solidFill);
  256. }
  257. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  258. if (groupFill != null)
  259. {
  260. fill.type = 5;
  261. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  262. }
  263. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  264. if (blipFill != null)
  265. {
  266. fill.type = 3;
  267. fill.blipFill = DoBlipFill(wordDoc, blipFill, ImageHandler, element, position);
  268. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  269. }
  270. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  271. // var effect = doEffect(EffectList,partForm);
  272. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  273. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  274. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  275. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  276. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */};
  277. }
  278. public static HTEXLib.Models.HTEX.ShapeStyle DoShapeProperties(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Office2010.Word.DrawingShape.ShapeProperties shapeProperties, XElement element, Func<ImageInfo, XElement> ImageHandler, Position position)
  279. {
  280. if (shapeProperties == null)
  281. {
  282. return null;
  283. }
  284. //Outline
  285. var lnNode = shapeProperties.GetFirstChild<Outline>();
  286. Border border = null;
  287. if (lnNode != null)
  288. {
  289. border = DoOutline(lnNode,wordDoc);
  290. }
  291. Fill fill = new Fill() { type = -1 };
  292. var gradFill = shapeProperties.GetFirstChild<GradientFill>();
  293. if (gradFill != null)
  294. {
  295. fill.type = 2;
  296. fill.gradientFill = DoGradientFill(gradFill,wordDoc);
  297. }
  298. var noFill = shapeProperties.GetFirstChild<NoFill>();
  299. if (noFill != null)
  300. {
  301. fill.type = 0;
  302. }
  303. var pattFill = shapeProperties.GetFirstChild<PatternFill>();
  304. if (pattFill != null)
  305. {
  306. HtexPattFill htexPattFill = DoPattFill(pattFill,wordDoc);
  307. fill.type = 4;
  308. fill.pattFill = htexPattFill;
  309. }
  310. var solidFill = shapeProperties.GetFirstChild<SolidFill>();
  311. if (solidFill != null)
  312. {
  313. fill.type = 1;
  314. fill.solidFill = DoSolidFill(solidFill);
  315. }
  316. var groupFill = shapeProperties.GetFirstChild<GroupFill>();
  317. if (groupFill != null)
  318. {
  319. fill.type = 5;
  320. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  321. }
  322. var blipFill = shapeProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.BlipFill>();
  323. if (blipFill != null)
  324. {
  325. fill.type = 3;
  326. fill.blipFill = DoBlipFill(wordDoc,blipFill, ImageHandler, element, position);
  327. // fill.solidFill = ReadSolidFillColors(new SolidFill(solidFill.ToString()), slide);
  328. }
  329. var EffectList = shapeProperties.GetFirstChild<EffectList>();
  330. // var effect = doEffect(EffectList,partForm);
  331. var EffectDag = shapeProperties.GetFirstChild<EffectDag>();
  332. var Scene3DType = shapeProperties.GetFirstChild<Scene3DType>();
  333. var Shape3DType = shapeProperties.GetFirstChild<Shape3DType>();
  334. var ShapePropertiesExtensionList = shapeProperties.GetFirstChild<ShapePropertiesExtensionList>();
  335. return new HTEXLib.Models.HTEX.ShapeStyle { fill = fill, border = border/*, effect = effect */};
  336. }
  337. public static string DoSolidFill(SolidFill Color)
  338. {
  339. string FontColor = "#000000";
  340. if (Color.RgbColorModelHex != null)
  341. {
  342. FontColor = ReadColor(Color.RgbColorModelHex);
  343. }
  344. else if (Color.RgbColorModelPercentage != null)
  345. {
  346. FontColor = ReadColor(Color.RgbColorModelPercentage);
  347. }
  348. else if (Color.SystemColor != null)
  349. {
  350. FontColor = ReadColor(Color.SystemColor);
  351. }
  352. if (Color.HslColor != null)
  353. {
  354. FontColor = ReadColor(Color.HslColor);
  355. }
  356. if (Color.PresetColor != null)
  357. {
  358. FontColor = ReadColor(Color.PresetColor);
  359. }
  360. return FontColor;
  361. }
  362. public static HtexPattFill DoPattFill(DocumentFormat.OpenXml.Drawing.PatternFill patternFill, WordprocessingDocument wordDoc)
  363. {
  364. HtexPattFill htexPattFill = new HtexPattFill();
  365. Color FgClr = Color.Empty;
  366. Color BgClr = Color.Empty;
  367. if (patternFill.ForegroundColor != null)
  368. {
  369. var fgClr = ColorTypeColors(patternFill.ForegroundColor,wordDoc);
  370. htexPattFill.fgClr = fgClr;
  371. FgClr = ColorTranslator.FromHtml(fgClr);
  372. }
  373. if (patternFill.BackgroundColor != null)
  374. {
  375. var bgClr = ColorTypeColors(patternFill.BackgroundColor,wordDoc);
  376. htexPattFill.bgClr = bgClr;
  377. BgClr = ColorTranslator.FromHtml(bgClr);
  378. }
  379. htexPattFill.prst = patternFill.Preset.ToString();
  380. MemoryStream ms = new MemoryStream();
  381. System.Drawing.Bitmap bm = PatternFillHelper.GetVmlPatternFill(patternFill.Preset, FgClr, BgClr);
  382. Graphics graphics = Graphics.FromImage(bm);
  383. graphics.Save();
  384. graphics.Dispose();
  385. bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  386. byte[] arr = new byte[ms.Length];
  387. ms.Position = 0;
  388. ms.Read(arr, 0, (int)ms.Length);
  389. ms.Close();
  390. bm.Dispose();
  391. string sImageData = "data:image/png;base64," + Convert.ToBase64String(arr);
  392. htexPattFill.base64 = sImageData;
  393. return htexPattFill;
  394. }
  395. public static HtexBlipFill DoBlipFill(WordprocessingDocument wordDoc, DocumentFormat.OpenXml.Drawing.BlipFill blipFill, Func<ImageInfo, XElement> imageHandler, XElement element, Position position)
  396. {
  397. HtexBlipFill htexBlipFill = new HtexBlipFill();
  398. SourceRectangle source = blipFill.SourceRectangle; ///源矩形 srcRect可能无用
  399. htexBlipFill.dip = blipFill.Dpi;
  400. if (blipFill.RotateWithShape != null)
  401. {
  402. htexBlipFill.rotWithShape = blipFill.RotateWithShape;
  403. }
  404. { htexBlipFill.rotWithShape = true; }
  405. var fillRect = blipFill.GetPPTXNodeByPath("a:stretch/a:fillRect");///拉伸
  406. if (fillRect != null)
  407. {
  408. htexBlipFill.fillRect = new Vector
  409. {
  410. t = fillRect.Attribute("t") != null ? int.Parse(fillRect.Attribute("t").Value) / 1000 : 0,
  411. l = fillRect.Attribute("l") != null ? int.Parse(fillRect.Attribute("l").Value) / 1000 : 0,
  412. r = fillRect.Attribute("r") != null ? int.Parse(fillRect.Attribute("r").Value) / 1000 : 0,
  413. b = fillRect.Attribute("b") != null ? int.Parse(fillRect.Attribute("b").Value) / 1000 : 0
  414. };
  415. }
  416. var alphaModFix = blipFill.Blip.GetPPTXNodeByPath("a:alphaModFix");
  417. if (alphaModFix != null)
  418. {
  419. if (alphaModFix.Attribute("amt") != null)
  420. {
  421. htexBlipFill.opacity = int.Parse(alphaModFix.Attribute("amt").Value) * 1.0 / 1000;
  422. }
  423. }
  424. Tile tile = blipFill.GetFirstChild<Tile>();
  425. if (tile != null)
  426. {
  427. ///algn (Alignment) flip (Tile Flipping) sx (Horizontal Ratio) sy (Vertical Ratio) tx (Horizontal Offset) ty (Vertical Offset)
  428. htexBlipFill.tile = new HtexTile { algn = tile.Alignment, flip = tile.Flip, sx = tile.HorizontalRatio, sy = tile.VerticalRatio, tx = tile.HorizontalOffset, ty = tile.VerticalOffset };
  429. }
  430. var Embed = blipFill.Blip.Embed;
  431. var Link = blipFill.Blip.Link;
  432. //slide.SlideLayoutPart
  433. IdPartPair part = null;
  434. if (blipFill == null) return null;
  435. // var imageRid = (string)blipFill.Elements(A.blip).Attributes(R.embed).FirstOrDefault();
  436. //if (imageRid == null) return null;
  437. var pp3 = wordDoc.MainDocumentPart.Parts.FirstOrDefault(pp => pp.RelationshipId == Embed);
  438. if (pp3 == null) return null;
  439. var imagePart = (ImagePart)pp3.OpenXmlPart;
  440. if (imagePart == null) return null;
  441. // If the image markup points to a NULL image, then following will throw an ArgumentOutOfRangeException
  442. try
  443. {
  444. imagePart = (ImagePart)wordDoc.MainDocumentPart.GetPartById(Embed);
  445. }
  446. catch (ArgumentOutOfRangeException)
  447. {
  448. return null;
  449. }
  450. var contentType = imagePart.ContentType;
  451. if (!WmlToHtmlConverter. ImageContentTypes.Contains(contentType))
  452. return null;
  453. using (var partStream = imagePart.GetStream()) {
  454. using (var bitmap = new Bitmap(partStream))
  455. {
  456. var imageInfo = new ImageInfo()
  457. {
  458. Bitmap = bitmap,
  459. ImgStyleAttribute = new XAttribute("style",
  460. string.Format(NumberFormatInfo.InvariantInfo,
  461. "width: {0}px; height: {1}in",
  462. position.cx,
  463. position.cy)),
  464. ContentType = contentType,
  465. // DrawingElement = element,
  466. //AltText = altText,
  467. };
  468. var imgElement2 = imageHandler(imageInfo);
  469. //imgElement2 = new XElement(XhtmlNoNamespace.a,
  470. // //new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
  471. // imgElement2);
  472. //return imgElement2;
  473. //var imageInfo2 = new ImageInfo()
  474. //{
  475. // Bitmap = bitmap,
  476. // ContentType = contentType,
  477. // //DrawingElement = element,
  478. // //AltText = altText,
  479. //};
  480. //var imgElement = imageHandler(imageInfo2);
  481. //imgElement= new XElement(XhtmlNoNamespace.a,
  482. // // new XAttribute(XhtmlNoNamespace.href, hyperlinkUri),
  483. // imgElement);
  484. htexBlipFill.url = imgElement2.ToString();
  485. // return imgElement;
  486. }
  487. }
  488. return htexBlipFill;
  489. //if (partForm == "slide")
  490. //{
  491. // part = slide.SlidePart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  492. //}
  493. //if (partForm == "layout")
  494. //{
  495. // part = slide.SlideLayoutPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  496. //}
  497. //if (partForm == "master")
  498. //{
  499. // part = slide.slideMasterPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  500. //}
  501. //if (partForm == "chartPart")
  502. //{
  503. // part = slide.chartPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  504. //}
  505. //if (partForm == "drawing")
  506. //{
  507. // part = slide.diagramDataPart.Parts.Where(x => x.RelationshipId == Embed).FirstOrDefault();
  508. //}
  509. //if (part != null)
  510. //{
  511. // var url = part.OpenXmlPart.Uri.ToString().Replace("../", "/ppt/");
  512. // var name = url.Replace("/", "");
  513. // var thumbnail = slide.Media.Where(x => x.Attributes().Select(y => y.Value == url).FirstOrDefault()).FirstOrDefault();
  514. // if (thumbnail != null)
  515. // {
  516. // var contentType = thumbnail.Attribute("{http://schemas.microsoft.com/office/2006/xmlPackage}contentType").Value;
  517. // //var data = thumbnail.Value;
  518. // //url = "data:" + contentType + ";base64," + data;
  519. // //url = url.Replace("\r\n", "");
  520. // htexBlipFill.url = name;
  521. // // htexBlipFill.urlType = "base64";
  522. // // htexBlipFill.contentType = contentType;
  523. // }
  524. //}
  525. //TODO 图片元素的更多信息需要后期继续实现,如滤镜,裁剪,图片颜色,图片校正等
  526. return htexBlipFill;
  527. }
  528. /// <summary>
  529. /// 渐变颜色
  530. /// GradientStopList (渐变停止列表)(默认)
  531. /// LinearGradientFill(线性渐变填充)
  532. /// PathGradientFill (路径渐变)根据具体形状描边式
  533. /// TileRectangle (平铺矩形) 右下角是一个举行
  534. /// </summary>
  535. /// <param name="GradientFill"></param>
  536. /// <returns></returns>
  537. public static HtexGradientFill DoGradientFill(DocumentFormat.OpenXml.Drawing.GradientFill GradientFill, WordprocessingDocument wordDoc)
  538. {
  539. HtexGradientFill gradientFill = new HtexGradientFill();
  540. ///右下角弧形模式
  541. string type = "";
  542. ///平铺翻转模式
  543. ///Horizontal 水平
  544. ///HorizontalAndVertical 水平和垂直。
  545. ///Vertical 垂直
  546. ///None 无
  547. string name = "None";
  548. if (GradientFill.Flip != null)
  549. {
  550. name = GradientFill.Flip.Value.ToString();
  551. }
  552. gradientFill.flip = name;
  553. List<ColorPosition> colors = new List<ColorPosition>();
  554. bool RotateWithShape = true;
  555. if (GradientFill.RotateWithShape != null)
  556. {
  557. RotateWithShape = GradientFill.RotateWithShape;
  558. }
  559. gradientFill.RotateWithShape = RotateWithShape;
  560. foreach (GradientStop gradientStop in GradientFill.GradientStopList)
  561. {
  562. colors.Add(new ColorPosition
  563. {
  564. color = ColorTypeColors(gradientStop,wordDoc),
  565. position = gradientStop.Position * 1.0 / 1000 /// 位置,色带的位置 百分比0-100
  566. });
  567. }
  568. gradientFill.colors = colors;
  569. var lin = GradientFill.GetPPTXNodeByPath("//a:lin");
  570. double? rot = null;
  571. string scaled = "";
  572. //线性模式
  573. if (lin != null)
  574. {
  575. rot = 90;
  576. type = "line";
  577. var ang = lin.Attribute("ang");
  578. var scaledNode = lin.Attribute("scaled");
  579. if (ang != null)
  580. {
  581. rot = ShapeHelper.AngleToDegrees(ang.Value);
  582. }
  583. if (scaledNode != null)
  584. {
  585. scaled = scaledNode.Value;
  586. }
  587. gradientFill.line = new LineFill { rot = rot, scaled = scaled };
  588. }
  589. var path = GradientFill.GetPPTXNodeByPath("//a:path");
  590. if (path != null)
  591. {
  592. type = "path";
  593. ///circle
  594. ///rect
  595. ///shape
  596. var pathType = path.Attribute("path").Value;
  597. var fillToRect = path.GetPPTXNodeByPath("//a:fillToRect");
  598. if (fillToRect != null)
  599. {
  600. Vector vector = new Vector();
  601. var bnode = fillToRect.Attribute("b");
  602. var lnode = fillToRect.Attribute("l");
  603. var rnode = fillToRect.Attribute("r");
  604. var tnode = fillToRect.Attribute("t");
  605. if (bnode != null)
  606. {
  607. vector.b = int.Parse(bnode.Value) / 1000;
  608. }
  609. if (lnode != null)
  610. {
  611. vector.l = int.Parse(lnode.Value) / 1000;
  612. }
  613. if (rnode != null)
  614. {
  615. vector.r = int.Parse(rnode.Value) / 1000;
  616. }
  617. if (tnode != null)
  618. {
  619. vector.t = int.Parse(tnode.Value) / 1000;
  620. }
  621. gradientFill.path = new PathFill { fillToRect = vector, pathType = pathType };
  622. }
  623. }
  624. var tileRect = GradientFill.GetPPTXNodeByPath("//a:tileRect");
  625. if (tileRect != null)
  626. {
  627. ///四个方向的偏移量,矩形的下边缘。例如50000/1000= 50% ,辐射百分比
  628. Vector vector = new Vector();
  629. var bnode = tileRect.Attribute("b");
  630. var lnode = tileRect.Attribute("l");
  631. var rnode = tileRect.Attribute("r");
  632. var tnode = tileRect.Attribute("t");
  633. if (bnode != null)
  634. {
  635. vector.b = int.Parse(bnode.Value) / 1000;
  636. }
  637. if (lnode != null)
  638. {
  639. vector.l = int.Parse(lnode.Value) / 1000;
  640. }
  641. if (rnode != null)
  642. {
  643. vector.r = int.Parse(rnode.Value) / 1000;
  644. }
  645. if (tnode != null)
  646. {
  647. vector.t = int.Parse(tnode.Value) / 1000;
  648. }
  649. gradientFill.tileRect = vector;
  650. }
  651. gradientFill.type = type;
  652. return gradientFill;
  653. }
  654. public static string ColorTypeColors(GradientStop Color, WordprocessingDocument wordDoc)
  655. {
  656. string FontColor = "#000000";
  657. if (Color.RgbColorModelHex != null)
  658. {
  659. FontColor = ReadColor(Color.RgbColorModelHex);
  660. }
  661. else if (Color.RgbColorModelPercentage != null)
  662. {
  663. FontColor = ReadColor(Color.RgbColorModelPercentage);
  664. }
  665. else if (Color.SystemColor != null)
  666. {
  667. FontColor = ReadColor(Color.SystemColor);
  668. }
  669. if (Color.SchemeColor != null)
  670. {
  671. FontColor = ReadColor(Color.SchemeColor, wordDoc);
  672. }
  673. if (Color.HslColor != null)
  674. {
  675. FontColor = ReadColor(Color.HslColor);
  676. }
  677. if (Color.PresetColor != null)
  678. {
  679. FontColor = ReadColor(Color.PresetColor);
  680. }
  681. return FontColor;
  682. }
  683. public static string ColorTypeColors(ColorType Color, WordprocessingDocument wordDoc)
  684. {
  685. string FontColor = "#000000";
  686. if (Color.RgbColorModelHex != null)
  687. {
  688. FontColor = ReadColor(Color.RgbColorModelHex);
  689. }
  690. else if (Color.RgbColorModelPercentage != null)
  691. {
  692. FontColor = ReadColor(Color.RgbColorModelPercentage);
  693. }
  694. else if (Color.SystemColor != null)
  695. {
  696. FontColor = ReadColor(Color.SystemColor);
  697. }
  698. if (Color.SchemeColor != null)
  699. {
  700. FontColor = ReadColor(Color.SchemeColor, wordDoc);
  701. }
  702. if (Color.HslColor != null)
  703. {
  704. FontColor = ReadColor(Color.HslColor);
  705. }
  706. if (Color.PresetColor != null)
  707. {
  708. FontColor = ReadColor(Color.PresetColor);
  709. }
  710. return FontColor;
  711. }
  712. public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelHex RgbColorModelHex)
  713. {
  714. string FontColor = "#000000";
  715. FontColor = "#" + RgbColorModelHex.Val;
  716. FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelHex.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  717. return FontColor;
  718. }
  719. public static string ReadColor(DocumentFormat.OpenXml.Drawing.RgbColorModelPercentage RgbColorModelPercentage)
  720. {
  721. string FontColor = "#000000";
  722. int red = RgbColorModelPercentage.RedPortion.Value;
  723. int green = RgbColorModelPercentage.GreenPortion.Value;
  724. int blue = RgbColorModelPercentage.BluePortion.Value;
  725. var colorFromRGB = System.Drawing.Color.FromArgb(red, green, blue);
  726. string fontcolor = System.Drawing.ColorTranslator.ToHtml(colorFromRGB);
  727. FontColor = fontcolor;
  728. FontColor = "#" + ShapeHelper.ColorToning(RgbColorModelPercentage.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  729. return FontColor;
  730. }
  731. public static string ReadColor(DocumentFormat.OpenXml.Drawing.SystemColor SystemColor)
  732. {
  733. string FontColor = "#000000";
  734. FontColor = "#" + SystemColor.LastColor.Value;
  735. FontColor = "#" + ShapeHelper.ColorToning(SystemColor.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  736. return FontColor;
  737. }
  738. public static string ReadColor(DocumentFormat.OpenXml.Drawing.SchemeColor SchemeColor, WordprocessingDocument wordDoc)
  739. {
  740. string FontColor = "";
  741. FontColor = DoSchemeColor(SchemeColor,wordDoc);
  742. FontColor = "#" + ShapeHelper.ColorToning(SchemeColor.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  743. return FontColor;
  744. }
  745. public static string ReadColor(DocumentFormat.OpenXml.Drawing.HslColor HslColor)
  746. {
  747. string FontColor = "#000000";
  748. var hue = HslColor.HueValue / 100000;
  749. var sat = HslColor.SatValue / 100;
  750. var lum = HslColor.LumValue / 100;
  751. var hsl2rgb = ShapeHelper.HslToRgb(hue, sat, lum);
  752. FontColor = "#" + ShapeHelper.ToHex(hsl2rgb.r) + ShapeHelper.ToHex(hsl2rgb.g) + ShapeHelper.ToHex(hsl2rgb.b);
  753. FontColor = "#" + ShapeHelper.ColorToning(HslColor.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  754. return FontColor;
  755. }
  756. public static string ReadColor(PresetColor PresetColor)
  757. {
  758. string FontColor = "#000000";
  759. //https://m.fontke.com/tool/rgb/0000ff/
  760. //判断是否是拿到名字 还是值
  761. FontColor = ShapeHelper.GetColorName2Hex(PresetColor.Val);
  762. FontColor = "#" + ShapeHelper.ColorToning(PresetColor.OuterXml, FontColor.Replace("#", "")).Replace("#", "");
  763. //TODO
  764. return FontColor;
  765. }
  766. public static string DoSchemeColor(SchemeColor schemeColor, WordprocessingDocument wordDoc )
  767. {
  768. //var light1Color1= slide.SlideLayoutPart.SlideMasterPart.
  769. // ThemePart;
  770. string FontColor = "";
  771. if (schemeColor.Val == "bg1")
  772. {
  773. Light1Color light1Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Light1Color;
  774. if (light1Color.RgbColorModelHex != null)
  775. {
  776. FontColor = "#" + light1Color.RgbColorModelHex.Val.Value;
  777. }
  778. else if (light1Color.SystemColor != null)
  779. {
  780. FontColor = "#" + light1Color.SystemColor.LastColor.Value;
  781. }
  782. }
  783. else if (schemeColor.Val == "bg2")
  784. {
  785. Light2Color light2Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Light2Color;
  786. if (light2Color.RgbColorModelHex != null)
  787. {
  788. FontColor = "#" + light2Color.RgbColorModelHex.Val.Value;
  789. }
  790. else if (light2Color.SystemColor != null)
  791. {
  792. FontColor = "#" + light2Color.SystemColor.LastColor.Value;
  793. }
  794. }
  795. else if (schemeColor.Val == "tx1")
  796. {
  797. Dark1Color dark1Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Dark1Color;
  798. if (dark1Color.RgbColorModelHex != null)
  799. {
  800. FontColor = "#" + dark1Color.RgbColorModelHex.Val.Value;
  801. }
  802. else if (dark1Color.SystemColor != null)
  803. {
  804. FontColor = "#" + dark1Color.SystemColor.LastColor.Value;
  805. }
  806. }
  807. else if (schemeColor.Val == "tx2")
  808. {
  809. Dark2Color dark2Color = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Dark2Color;
  810. if (dark2Color.RgbColorModelHex != null)
  811. {
  812. FontColor = "#" + dark2Color.RgbColorModelHex.Val.Value;
  813. }
  814. else if (dark2Color.SystemColor != null)
  815. {
  816. FontColor = "#" + dark2Color.SystemColor.LastColor.Value;
  817. }
  818. }
  819. else if (schemeColor.Val == "hlink")
  820. {
  821. Hyperlink HyperLink = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Hyperlink;
  822. if (HyperLink.RgbColorModelHex != null)
  823. {
  824. FontColor = "#" + HyperLink.RgbColorModelHex.Val.Value;
  825. }
  826. }
  827. else if (schemeColor.Val == "folHlink")
  828. {
  829. FollowedHyperlinkColor folHyperLink = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.FollowedHyperlinkColor;
  830. if (folHyperLink.RgbColorModelHex != null)
  831. {
  832. FontColor = "#" + folHyperLink.RgbColorModelHex.Val.Value;
  833. }
  834. }
  835. if (string.IsNullOrEmpty(FontColor))
  836. {
  837. FontColor = ReadAccentSchemeColors(schemeColor, wordDoc);
  838. }
  839. if (FontColor.Replace("#", "") == "")
  840. {
  841. var chren = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.ChildElements;
  842. foreach (var cl in chren)
  843. {
  844. if (schemeColor.Val == cl.LocalName)
  845. {
  846. if (schemeColor.Val == "dk1" || schemeColor.Val == "lt1")
  847. {
  848. if (cl.GetFirstChild<SystemColor>() != null)
  849. {
  850. FontColor = "#" + cl.GetFirstChild<SystemColor>().LastColor;
  851. }
  852. else
  853. {
  854. FontColor = "#" + cl.GetFirstChild<RgbColorModelHex>().Val.Value; ;
  855. }
  856. }
  857. else
  858. {
  859. FontColor = "#" + cl.GetFirstChild<RgbColorModelHex>().Val.Value; ;
  860. }
  861. }
  862. }
  863. }
  864. return FontColor;
  865. }
  866. public static string ReadAccentSchemeColors(SchemeColor schemeColor, WordprocessingDocument wordDoc)
  867. {
  868. string FontColor = "";
  869. for (int i = 1; i <= 6; i++)
  870. {
  871. if (schemeColor.Val == "accent" + i)
  872. {
  873. switch (i)
  874. {
  875. case 1:
  876. {
  877. Accent1Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent1Color;
  878. if (c.RgbColorModelHex != null)
  879. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  880. }
  881. break;
  882. case 2:
  883. {
  884. Accent2Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent2Color;
  885. if (c.RgbColorModelHex != null)
  886. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  887. }
  888. break;
  889. case 3:
  890. {
  891. Accent3Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent3Color;
  892. if (c.RgbColorModelHex != null)
  893. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  894. }
  895. break;
  896. case 4:
  897. {
  898. Accent4Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent4Color;
  899. if (c.RgbColorModelHex != null)
  900. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  901. }
  902. break;
  903. case 5:
  904. {
  905. Accent5Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent5Color;
  906. if (c.RgbColorModelHex != null)
  907. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  908. }
  909. break;
  910. case 6:
  911. {
  912. Accent6Color c = wordDoc.MainDocumentPart.ThemePart.Theme.ThemeElements.ColorScheme.Accent6Color;
  913. if (c.RgbColorModelHex != null)
  914. FontColor = "#" + c.RgbColorModelHex.Val.Value;
  915. }
  916. break;
  917. default: { } break;
  918. }
  919. }
  920. }
  921. return FontColor;
  922. }
  923. /// <summary>
  924. /// a:ln 图形的外边
  925. /// </summary>
  926. /// <param name="shapeProperties"></param>
  927. /// <param name="slide"></param>
  928. public static Border DoOutline(DocumentFormat.OpenXml.Drawing.LinePropertiesType outline, WordprocessingDocument wordDoc)
  929. {
  930. Border border = new Border() { color = new Fill { type = -1 }, type = "none" };
  931. //20.1.10.35 EMUs. 1 pt = 12700 EMUs.
  932. double? Width = null;
  933. if (outline == null )
  934. {
  935. return new Border { outline = new HtexOutline { width = 0.75 }, type = "solid", stroke = "0" };
  936. }
  937. if (outline.Width != null)
  938. {
  939. Width = double.Parse(outline.Width) * 1.0 * Globals.px96 / Globals.px914400;
  940. }
  941. else
  942. {
  943. Width = 0.75;
  944. }
  945. // Center = 0,Insert = 1
  946. //
  947. //20.1.10.39
  948. string algn = null;
  949. if (outline.Alignment != null)
  950. {
  951. algn = outline.Alignment;
  952. }
  953. /* 20.1.10.15 复合类型
  954. Single = 0, 单线 默认
  955. Double = 1, 双细线
  956. ThickThin = 2, 粗到细
  957. ThinThick = 3, 细到粗
  958. Triple = 4 三线
  959. */
  960. string cmpd = null;
  961. if (outline.CompoundLineType != null)
  962. {
  963. cmpd = outline.CompoundLineType;
  964. }
  965. /* 20.1.10.31 线端类型
  966. Round = 0, 方
  967. Square = 1,圆
  968. Flat = 2 平
  969. */
  970. string cap = null;
  971. if (outline.CapType != null)
  972. {
  973. cap = outline.CapType;
  974. }
  975. border.outline = new HtexOutline { width = Width, algn = algn, cmpd = cmpd, cap = cap };
  976. var gradFill = outline.GetFirstChild<GradientFill>();
  977. if (gradFill != null)
  978. {
  979. border.color.type = 2;
  980. border.color.gradientFill = DoGradientFill(gradFill,wordDoc);
  981. }
  982. var noFill = outline.GetFirstChild<NoFill>();
  983. if (noFill != null)
  984. {
  985. border.color.type = 0;
  986. }
  987. var pattFill = outline.GetFirstChild<PatternFill>();
  988. if (pattFill != null)
  989. {
  990. HtexPattFill htexPattFill = DoPattFill(pattFill,wordDoc);
  991. border.color.type = 4;
  992. border.color.pattFill = htexPattFill;
  993. }
  994. var solidFill = outline.GetFirstChild<SolidFill>();
  995. if (solidFill != null)
  996. {
  997. border.color.type = 1;
  998. border.color.solidFill = DoSolidFill(solidFill);
  999. }
  1000. if (Width != null && Width > 0)
  1001. {
  1002. border.type = "solid";
  1003. border.stroke = "0";
  1004. }
  1005. var prstDash = outline.GetFirstChild<PresetDash>(); //预设连接符
  1006. if (prstDash != null)
  1007. {
  1008. // PresetDash
  1009. var (type, stroke) = DoPrstDash(prstDash);
  1010. border.type = type;
  1011. border.stroke = stroke;
  1012. }
  1013. if (outline.GetFirstChild<Bevel>() != null)
  1014. {
  1015. // 棱台切角
  1016. border.corner = "bevel";
  1017. }
  1018. if (outline.GetFirstChild<Round>() != null)
  1019. {
  1020. //圆角
  1021. border.corner = "round";
  1022. }
  1023. if (outline.GetFirstChild<Miter>() != null)
  1024. {
  1025. //斜角
  1026. border.corner = "miter";
  1027. }
  1028. var headEndNodeAttrs = outline.GetFirstChild<HeadEnd>(); //连接符开始符号
  1029. if (headEndNodeAttrs != null)
  1030. {
  1031. border.headEnd = headEndNodeAttrs.Type;
  1032. }
  1033. var tailEndNodeAttrs = outline.GetFirstChild<TailEnd>();//末端连接符
  1034. if (tailEndNodeAttrs != null)
  1035. {
  1036. border.tailEnd = tailEndNodeAttrs.Type;
  1037. }
  1038. //TODO
  1039. //outline.GetTextByPath("//a:extLst");
  1040. var custDash = outline.GetFirstChild<CustomDash>();//自定义连接线
  1041. if (custDash != null)
  1042. {
  1043. var (type, stroke) = DoCustDash(custDash);
  1044. border.type = type;
  1045. border.stroke = stroke;
  1046. }
  1047. if (border.color != null && (border.color.type != null || border.color.type != 1) && border.outline.width == null)
  1048. {
  1049. border.outline.width = 1;
  1050. if (border.type == "none" || border.type == null)
  1051. {
  1052. border.type = "solid";
  1053. border.stroke = "0";
  1054. }
  1055. }
  1056. return border;
  1057. }
  1058. /// <summary>
  1059. ///自定义连线符
  1060. /// </summary>
  1061. /// <param name="custDash"></param>
  1062. /// <returns></returns>
  1063. public static (string type, string stroke) DoCustDash(CustomDash custDash)
  1064. {
  1065. string type = "cust";
  1066. string stroke = "0";
  1067. IEnumerable<XElement> elements = custDash.GetPPTXNodeListByPath("ds");
  1068. if (elements != null)
  1069. {
  1070. foreach (XElement element in elements)
  1071. {
  1072. var d = element.Attribute("d");//线段长度
  1073. if (d != null)
  1074. {
  1075. stroke = stroke + d.Value + ",";
  1076. }
  1077. var sp = element.Attribute("sp");//空白间距长度
  1078. if (sp != null)
  1079. {
  1080. stroke = stroke + sp.Value + ",";
  1081. }
  1082. }
  1083. }
  1084. if (string.IsNullOrEmpty(stroke))
  1085. {
  1086. stroke = "0";
  1087. }
  1088. if (stroke.EndsWith(","))
  1089. {
  1090. stroke.Substring(0, stroke.Length - 1);
  1091. }
  1092. return (type, stroke);
  1093. }
  1094. /// <summary>
  1095. /// 预设连线符
  1096. /// </summary>
  1097. /// <param name="presetDash"></param>
  1098. /// <returns></returns>
  1099. public static (string type, string stroke) DoPrstDash(PresetDash presetDash)
  1100. {
  1101. Border border = new Border()
  1102. {
  1103. type = "solid",
  1104. stroke = "0"
  1105. };
  1106. //PresetLineDashValues 枚举
  1107. string val = presetDash.Val;
  1108. switch (val)
  1109. {
  1110. case "solid":
  1111. border.type = "solid";
  1112. border.stroke = "0";
  1113. break;
  1114. case "dash":
  1115. border.type += "dashed";
  1116. border.stroke = "5";
  1117. break;
  1118. case "dashDot":
  1119. border.type = "dashed";
  1120. border.stroke = "5, 5, 1, 5";
  1121. break;
  1122. case "dot":
  1123. border.type = "dotted";
  1124. border.stroke = "1, 5";
  1125. break;
  1126. case "lgDash":
  1127. border.type = "dashed";
  1128. border.stroke = "10, 5";
  1129. break;
  1130. case "lgDashDotDot":
  1131. border.type = "dashed";
  1132. border.stroke = "10, 5, 1, 5, 1, 5";
  1133. break;
  1134. case "sysDash":
  1135. border.type = "dashed";
  1136. border.stroke = "5, 2";
  1137. break;
  1138. case "sysDashDot":
  1139. border.type = "dashed";
  1140. border.stroke = "5, 2, 1, 5";
  1141. break;
  1142. case "sysDashDotDot":
  1143. border.type = "dashed";
  1144. border.stroke = "5, 2, 1, 5, 1, 5";
  1145. break;
  1146. case "sysDot":
  1147. border.type = "dotted";
  1148. border.stroke = "2, 5";
  1149. break;
  1150. case null:
  1151. //console.log(borderType);
  1152. default:
  1153. border.type = "solid";
  1154. border.stroke = "0";
  1155. break;
  1156. }
  1157. return (border.type, border.stroke);
  1158. }
  1159. }
  1160. }