ShapeGenerator.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. using DocumentFormat.OpenXml.Packaging;
  2. using HiTeachCC.Model.PowerPoint;
  3. using HiTeachCC.Service.PowerPoint.Interface;
  4. using Microsoft.AspNetCore.Http;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. using System.Xml.Linq;
  11. using TEAMModelOS.SDK.Context.Constant;
  12. using TEAMModelOS.SDK.Context.Constant.Common;
  13. using TEAMModelOS.SDK.Context.Exception;
  14. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  15. using TEAMModelOS.SDK.Module.AzureBlob.Container;
  16. using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
  17. using TEAMModelOS.Service.Models.PowerPoint.Inner;
  18. using TEAMModelOS.Service.Services.PowerPoint.Implement;
  19. using Fill = HiTeachCC.Model.PowerPoint.Fill;
  20. using Media = HiTeachCC.Model.PowerPoint.Media;
  21. using Position = HiTeachCC.Model.PowerPoint.Position;
  22. namespace HiTeachCC.Service.PowerPoint.Implement
  23. {
  24. public class ShapeGenerator : IShapeGenerator
  25. {
  26. private string fileShaCode { get; set; }
  27. private bool isDone = false;
  28. private List<object> MsgQueue = new List<object>();
  29. private Dictionary<string, string> slideLayoutClrOvride { get; set; }
  30. // private XmlNode themeContent { get; set; }
  31. private int chartID = 0;
  32. private int _order = 1;
  33. private int titleFontSize = 42;
  34. private int bodyFontSize = 20;
  35. private int otherFontSize = 16;
  36. private bool isSlideMode = false;
  37. private Dictionary<string, Dictionary<string, string>> styleTable = new Dictionary<string, Dictionary<string, string>>();
  38. private const double px96 = 96.00, px72 = 72.00, px914400 = 914400.00, px12700 = 12700.00;
  39. private const double rot60000 = 60000.00;
  40. public readonly IAzureBlobDBRepository azureBlobDBRepository;
  41. public ShapeGenerator(IAzureBlobDBRepository _azureBlobDBRepository)
  42. {
  43. azureBlobDBRepository = _azureBlobDBRepository;
  44. }
  45. public async Task<Dictionary<string, object>> LoadPresentation(IFormFile file)
  46. {
  47. Dictionary<string, object> resdict = new Dictionary<string, object>();
  48. if (FileType.GetExtention(file.FileName).ToLower().Equals("pptx"))
  49. {
  50. await ConvertPPTX(file, resdict);
  51. return resdict;
  52. }
  53. else if (FileType.GetExtention(file.FileName).ToLower().Equals("pdf"))
  54. {
  55. // await ProcessPDF(file, resdict);
  56. return resdict;
  57. }
  58. else
  59. {
  60. throw new BizException("file type does not support!", 500);
  61. }
  62. }
  63. public async Task<Dictionary<string, object>> ConvertPPTX(IFormFile file, Dictionary<string, object> resdict)
  64. {
  65. string shaCode = fileShaCode = ShaHashHelper.GetSHA1(file.OpenReadStream());
  66. PresentationDocument presentationDocument = PresentationDocument.Open(file.OpenReadStream(), false);
  67. if (presentationDocument == null)
  68. {
  69. throw new ArgumentNullException("presentationDocument");
  70. }
  71. XDocument xdoc = presentationDocument.ToFlatOpcDocument();
  72. XmlDocument xmlDocument = new XmlDocument();
  73. xmlDocument.LoadXml(xdoc.ToString());
  74. var rslt_ary = await ProcessPPTX(xmlDocument, shaCode);
  75. //TODO
  76. return null;
  77. }
  78. /// <summary>
  79. /// 加载PPTX文件
  80. /// </summary>
  81. /// <param name="presentationFile"></param>
  82. /// <returns></returns>
  83. public async Task<List<Dictionary<string, object>>> ProcessPPTX(XmlDocument xdoc, string shaCode)
  84. {
  85. List<Dictionary<string, object>> post_ary = new List<Dictionary<string, object>>();
  86. var dateBefore = DateTimeOffset.Now.UtcTicks;
  87. AzureBlobModel thumbnailModel = await GetThumbnailModel(xdoc, shaCode);
  88. post_ary.Add(new Dictionary<string, object> { { "pptx-thumb", thumbnailModel } });
  89. //获取全部的/ppt/slides/slide1.xml--->>/ppt/slides/slide(n).xml
  90. var slideNodes = xdoc.GetTextByPathList("//pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.presentationml.slide+xml']");
  91. //获取全部的/ppt/slideLayouts/slideLayout1.xml--->>/ppt/slideLayouts/slideLayout(n).xml
  92. var slideLayoutNodes = xdoc.GetTextByPathList("//pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml']");
  93. //获取ppt的宽高
  94. var slideSize = ShapeHelper.GetSlideSize(xdoc);
  95. post_ary.Add(new Dictionary<string, object> { { "slideSize", slideSize } });
  96. //加载当前PPT使用到的PPT节点
  97. var themeContent = LoadTheme(xdoc);
  98. //获取所有的表格样式节点
  99. var tableStyles = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/tableStyles.xml']/pkg:xmlData/a:tblStyleLst");
  100. //处理slide1---slide(n)
  101. int numOfSlides = slideNodes.Count;
  102. for (int i = 0; i < numOfSlides; i++)
  103. {
  104. string filename = slideNodes[i].GetTextByPath("@pkg:name").Value;
  105. //处理当前页的幻灯片
  106. string slideHtml = await ProcessSingleSlide(xdoc, slideNodes[i], filename, i, slideSize, themeContent);
  107. post_ary.Add(new Dictionary<string, object> { { "slide", slideHtml } });
  108. post_ary.Add(new Dictionary<string, object> { { "progress-update", (i + 1) * 100 / numOfSlides } });
  109. }
  110. post_ary.Add(new Dictionary<string, object> { { "globalCSS", GenGlobalCSS() } });
  111. var dateAfter = DateTimeOffset.Now.UtcTicks;
  112. post_ary.Add(new Dictionary<string, object> { { "ExecutionTime", dateAfter - dateBefore } });
  113. return post_ary;
  114. }
  115. public Dictionary<string, string> GenGlobalCSS()
  116. {
  117. Dictionary<string, string> cssText = new Dictionary<string, string>();
  118. foreach (var key in styleTable.Keys)
  119. {
  120. // cssText += "div ." + styleTable[key]["name"] + "{" + styleTable[key]["text"] + "}\n"; //section > div
  121. var cssTextKey = styleTable[key]["name"];
  122. var cssTextValue = styleTable[key]["text"];
  123. cssText.Add(cssTextKey, cssTextValue);
  124. }
  125. return cssText;
  126. }
  127. public async Task<string> ProcessSingleSlide(XmlDocument xdoc, XmlNode xnode, string sldFileName, int index, dynamic slideSize, XmlNode themeContent)
  128. {
  129. var resName = sldFileName.Replace("slides/slide", "slides/_rels/slide") + ".rels";
  130. var RelationshipArray = xdoc.GetTextByPathList("//pkg:part[@pkg:name='" + resName + "']/pkg:xmlData/rel:Relationships/rel:Relationship");
  131. Dictionary<string, Dictionary<string, string>> slideResObj = new Dictionary<string, Dictionary<string, string>>();
  132. var layoutFilename = "";
  133. foreach (XmlNode Relationship in RelationshipArray)
  134. {
  135. var RelationshipType = Relationship.GetTextByPath("@Type");
  136. if (RelationshipType.Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"))
  137. {
  138. layoutFilename = Relationship.GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  139. }
  140. else
  141. {
  142. //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide":
  143. //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image":
  144. //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart":
  145. //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink":
  146. Dictionary<string, string> dict = new Dictionary<string, string>();
  147. string Type = Relationship.GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
  148. string Target = Relationship.GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  149. string Id = Relationship.GetTextByPath("@Id").Value;
  150. dict.Add("type", Type);
  151. dict.Add("target", Target);
  152. slideResObj.Add(Id, dict);
  153. }
  154. }
  155. // Open slideLayoutXX.xml
  156. var slideLayoutContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + layoutFilename + "']");
  157. NodesTable slideLayoutTables = ShapeHelper.IndexNodes(slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout"));
  158. ///ppt/slides/slide5.xml 也包含a:overrideClrMapping
  159. ////ppt/slideLayouts/slideLayout13.xml 也包含a:overrideClrMapping
  160. var sldLayoutClrOvr = slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout/p:clrMapOvr/a:overrideClrMapping");
  161. if (sldLayoutClrOvr != null)
  162. {
  163. //获取 overrideClrMapping所有属性节点
  164. slideLayoutClrOvride = new Dictionary<string, string>() {
  165. { "accent1", sldLayoutClrOvr.GetTextByPath("@accent1").Value},
  166. { "accent2", sldLayoutClrOvr.GetTextByPath("@accent2").Value},
  167. { "accent3", sldLayoutClrOvr.GetTextByPath("@accent3").Value},
  168. { "accent4", sldLayoutClrOvr.GetTextByPath("@accent4").Value},
  169. { "accent5", sldLayoutClrOvr.GetTextByPath("@accent5").Value},
  170. { "accent6", sldLayoutClrOvr.GetTextByPath("@accent6").Value},
  171. { "bg1", sldLayoutClrOvr.GetTextByPath("@bg1").Value},
  172. { "bg2", sldLayoutClrOvr.GetTextByPath("@bg2").Value},
  173. { "folHlink", sldLayoutClrOvr.GetTextByPath("@folHlink").Value},
  174. { "hlink", sldLayoutClrOvr.GetTextByPath("@hlink").Value},
  175. { "tx1", sldLayoutClrOvr.GetTextByPath("@tx1").Value},
  176. { "tx2", sldLayoutClrOvr.GetTextByPath("@tx2").Value}
  177. };
  178. }
  179. // =====< Step 2 >=====
  180. // Read slide master filename of the slidelayout (Get slideMasterXX.xml)
  181. // @resName: ppt/slideLayouts/slideLayout1.xml
  182. // @masterName: ppt/slideLayouts/_rels/slideLayout1.xml.rels
  183. var slideLayoutResFilename = layoutFilename.Replace("slideLayouts/slideLayout", "slideLayouts/_rels/slideLayout") + ".rels";
  184. var slideLayoutResContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + slideLayoutResFilename + "']");
  185. ///查看js 是替换之前 还是添加
  186. RelationshipArray = slideLayoutResContent.GetTextByPathList("pkg:xmlData/rel:Relationships/rel:Relationship");
  187. var masterFilename = "";
  188. Dictionary<string, Dictionary<string, string>> layoutResObj = new Dictionary<string, Dictionary<string, string>>();
  189. for (int i = 0; i < RelationshipArray.Count; i++)
  190. {
  191. if (RelationshipArray[i].GetTextByPath("@Type").Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"))
  192. {
  193. masterFilename = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  194. }
  195. else
  196. {
  197. Dictionary<string, string> dict = new Dictionary<string, string>();
  198. string Type = RelationshipArray[i].GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
  199. string Target = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  200. string Id = RelationshipArray[i].GetTextByPath("@Id").Value;
  201. dict.Add("type", Type);
  202. dict.Add("target", Target);
  203. layoutResObj.Add(Id, dict);
  204. }
  205. }
  206. // Open slideMasterXX.xml
  207. var slideMasterContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + masterFilename + "']");
  208. var slideMasterTextStyles = slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster/p:txStyles");
  209. var slideMasterTables = ShapeHelper.IndexNodes(slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster"));
  210. /////////////////Amir/////////////
  211. //Open slideMasterXX.xml.rels
  212. var slideMasterResFilename = masterFilename.Replace("slideMasters/slideMaster", "slideMasters/_rels/slideMaster") + ".rels";
  213. var slideMasterResContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + slideMasterResFilename + "']");
  214. RelationshipArray = slideMasterResContent.GetTextByPathList("pkg:xmlData/rel:Relationships/rel:Relationship");
  215. var themeFilename = "";
  216. Dictionary<string, Dictionary<string, string>> masterResObj = new Dictionary<string, Dictionary<string, string>>();
  217. for (int i = 0; i < RelationshipArray.Count; i++)
  218. {
  219. if (RelationshipArray[i].GetTextByPath("@Type").Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"))
  220. {
  221. themeFilename = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  222. }
  223. else
  224. {
  225. Dictionary<string, string> dict = new Dictionary<string, string>();
  226. string Type = RelationshipArray[i].GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
  227. string Target = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
  228. string Id = RelationshipArray[i].GetTextByPath("@Id").Value;
  229. dict.Add("type", Type);
  230. dict.Add("target", Target);
  231. masterResObj.Add(Id, dict);
  232. }
  233. }
  234. //Load Theme file
  235. if (!string.IsNullOrEmpty(themeFilename))
  236. {
  237. themeContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + themeFilename + "']");
  238. }
  239. // =====< Step 3 >===== throw new NotImplementedException();
  240. var slideContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + sldFileName + "']");
  241. var spTreeNode = slideContent.GetTextByPath("pkg:xmlData/p:sld/p:cSld/p:spTree");
  242. WarpObj warpObj = new WarpObj
  243. {
  244. zip = xdoc,
  245. slideLayoutTables = slideLayoutTables,
  246. slideMasterTables = slideMasterTables,
  247. slideMasterTextStyles = slideMasterTextStyles,
  248. slideResObj = slideResObj,
  249. layoutResObj = layoutResObj,
  250. masterResObj = masterResObj,
  251. slideLayoutContent = slideLayoutContent,
  252. slideMasterContent = slideMasterContent,
  253. themeContent = themeContent
  254. };
  255. var bgColor = GetSlideBackgroundFill(slideContent, index, warpObj);
  256. string result = "<div class='slide' style='width:" + slideSize.width + "px; height:" + slideSize.height + "px;" + bgColor + "'>";
  257. //result += "<div>"+getBackgroundShapes(slideContent, slideLayoutContent, slideMasterContent,warpObj) + "</div>" - TODO
  258. if (spTreeNode != null)
  259. {
  260. var nodes = spTreeNode.ChildNodes;
  261. int order = 1;
  262. foreach (XmlNode node in nodes)
  263. {
  264. result += await ProcessNodesInSlide(node.Name, node, order, warpObj);
  265. order += 1;
  266. }
  267. }
  268. return result;
  269. }
  270. public async Task<string> ProcessNodesInSlide(string nodeKey, XmlNode node, int order, WarpObj warpObj)
  271. {
  272. string result = "";
  273. switch (nodeKey)
  274. {
  275. case "p:sp": // Shape, Text
  276. result = await ProcessSpNode(node, order, warpObj);
  277. break;
  278. case "p:cxnSp": // Shape, Text (with connection)
  279. result = await ProcessCxnSpNode(node, order, warpObj);
  280. break;
  281. case "p:pic": // Picture
  282. await ProcessPicNode(node, order, warpObj);
  283. break;
  284. case "p:graphicFrame": // Chart, Diagram, Table
  285. result = ProcessGraphicFrameNode(node, order, warpObj);
  286. break;
  287. case "p:grpSp":
  288. await ProcessGroupSpNode(node, order, warpObj);
  289. break;
  290. case "mc:AlternateContent": //Equations and formulas as Image 处理公式 方程等
  291. var mcFallbackNode = node.GetTextByPath("mc:Fallback/p:sp");
  292. result = await ProcessSpNode(mcFallbackNode, order, warpObj);
  293. break;
  294. default:
  295. break;
  296. }
  297. return result;
  298. }
  299. public async Task<string> ProcessGroupSpNode(XmlNode node, int order, WarpObj warpObj)
  300. {
  301. var factor = 1.00 * px96 / px914400;
  302. var xfrmNode = node.GetTextByPath("p:grpSpPr/a:xfrm");
  303. var x = int.Parse(xfrmNode.GetTextByPath("a:off/@x").Value) * factor;
  304. var y = int.Parse(xfrmNode.GetTextByPath("a:off/@y").Value) * factor;
  305. var chx = int.Parse(xfrmNode.GetTextByPath("a:chOff/@x").Value) * factor;
  306. var chy = int.Parse(xfrmNode.GetTextByPath("a:chOff/@y").Value) * factor;
  307. var cx = int.Parse(xfrmNode.GetTextByPath("a:ext/@cx").Value) * factor;
  308. var cy = int.Parse(xfrmNode.GetTextByPath("a:ext/@cy").Value) * factor;
  309. var chcx = int.Parse(xfrmNode.GetTextByPath("a:chExt/@cx").Value) * factor;
  310. var chcy = int.Parse(xfrmNode.GetTextByPath("a:chExt/@cy").Value) * factor;
  311. string result = "<div class='block group' style='z-index: " + order + "; top: " + (y - chy) + "px; left: " + (x - chx) + "px; width: " + (cx - chcx) + "px; height: " + (cy - chcy) + "px;'>";
  312. // Procsee all child nodes
  313. var nodes = node.ChildNodes;
  314. foreach (XmlNode nd in nodes)
  315. {
  316. result += await ProcessNodesInSlide(nd.Name, nd, order, warpObj);
  317. order += 1;
  318. }
  319. result += "</div>";
  320. return result;
  321. }
  322. public string ProcessGraphicFrameNode(XmlNode node, int order, WarpObj warpObj)
  323. {
  324. var result = "";
  325. var graphicTypeUri = node.GetTextByPath("a:graphic/a:graphicData/uri");
  326. if (graphicTypeUri != null)
  327. {
  328. switch (graphicTypeUri.Value)
  329. {
  330. case "http://schemas.openxmlformats.org/drawingml/2006/table":
  331. result = GenTable(node, warpObj);
  332. break;
  333. case "http://schemas.openxmlformats.org/drawingml/2006/chart":
  334. result = GenChart(node, warpObj);
  335. break;
  336. case "http://schemas.openxmlformats.org/drawingml/2006/diagram":
  337. result = GenDiagram(node, warpObj);
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. return result;
  344. }
  345. public string GenDiagram(XmlNode node, WarpObj warpObj)
  346. {
  347. return "";
  348. }
  349. public string GenChart(XmlNode node, WarpObj warpObj)
  350. {
  351. return "";
  352. }
  353. public string GenTable(XmlNode node, WarpObj warpObj)
  354. {
  355. return "";
  356. }
  357. public async Task<string> ProcessCxnSpNode(XmlNode node, int order, WarpObj warpObj)
  358. {
  359. var id = node.GetTextByPath("p:nvCxnSpPr/p:cNvPr/@id");
  360. var name = node.GetTextByPath("p:nvCxnSpPr/p:cNvPr/@name");
  361. return await GenShape(node, null, null, id, name, null, null, order, warpObj);
  362. }
  363. public async Task<string> ProcessSpNode(XmlNode node, int order, WarpObj warpObj)
  364. {
  365. /*
  366. * 958 <xsd:complexType name="CT_GvmlShape">
  367. * 959 <xsd:sequence>
  368. * 960 <xsd:element name="nvSpPr" type="CT_GvmlShapeNonVisual" minOccurs="1" maxOccurs="1"/>
  369. * 961 <xsd:element name="spPr" type="CT_ShapeProperties" minOccurs="1" maxOccurs="1"/>
  370. * 962 <xsd:element name="txSp" type="CT_GvmlTextShape" minOccurs="0" maxOccurs="1"/>
  371. * 963 <xsd:element name="style" type="CT_ShapeStyle" minOccurs="0" maxOccurs="1"/>
  372. * 964 <xsd:element name="extLst" type="CT_OfficeArtExtensionList" minOccurs="0" maxOccurs="1"/>
  373. * 965 </xsd:sequence>
  374. * 966 </xsd:complexType>
  375. */
  376. var id = node.GetTextByPath("p:nvSpPr/p:cNvPr/@id");
  377. var name = node.GetTextByPath("p:nvSpPr/p:cNvPr/@name");
  378. var idx = node.GetTextByPath("p:nvSpPr/p:nvPr/p:ph/@idx");
  379. var type = node.GetTextByPath("p:nvSpPr/p:nvPr/p:ph/@type");
  380. XmlNode slideLayoutSpNode = ShapeHelper.GetNodesTable(id, idx, type, warpObj, "Layout");
  381. XmlNode slideMasterSpNode = ShapeHelper.GetNodesTable(id, idx, type, warpObj, "Master");
  382. if (type == null)
  383. {
  384. if (slideLayoutSpNode != null)
  385. {
  386. type = slideLayoutSpNode.GetTextByPath("p:nvSpPr/p:nvPr/p:ph/@type");
  387. }
  388. // type = slideLayoutSpNode.TryGetValue"p:nvSpPr", "p:nvPr", "p:ph", "attrs", "type"]);
  389. if (type == null)
  390. {
  391. if (slideMasterSpNode != null)
  392. {
  393. type = slideMasterSpNode.GetTextByPath("p:nvSpPr/p:nvPr/p:ph/@type");
  394. }
  395. // type = getTextByPathList(slideMasterSpNode, ["p:nvSpPr", "p:nvPr", "p:ph", "attrs", "type"]);
  396. }
  397. }
  398. string s = await GenShape(node, slideLayoutSpNode, slideMasterSpNode, id, name, idx, type, order, warpObj);
  399. return "";
  400. }
  401. public async Task<string> GenShape(XmlNode node, XmlNode slideLayoutSpNode, XmlNode slideMasterSpNode, XmlNode id, XmlNode name, XmlNode idx, XmlNode type, int order, WarpObj warpObj)
  402. {
  403. var xfrmList = "p:spPr/a:xfrm";
  404. var slideXfrmNode = node.GetTextByPath(xfrmList);
  405. XmlNode slideLayoutXfrmNode = null;
  406. if (slideLayoutSpNode != null)
  407. {
  408. slideLayoutXfrmNode = slideLayoutSpNode.GetTextByPath(xfrmList);
  409. }
  410. XmlNode slideMasterXfrmNode = null;
  411. if (slideMasterSpNode != null)
  412. {
  413. slideMasterXfrmNode = slideMasterSpNode.GetTextByPath(xfrmList);
  414. }
  415. if (slideXfrmNode == null)
  416. {
  417. slideXfrmNode = slideLayoutXfrmNode;
  418. }
  419. if (slideXfrmNode == null)
  420. {
  421. slideXfrmNode = slideMasterXfrmNode;
  422. }
  423. var result = "";
  424. var shapType = node.GetTextByPath("p:spPr/a:prstGeom/@prst");
  425. var custShapType = node.GetTextByPath("p:spPr/a:custGeom");
  426. var isFlipV = 0;
  427. var isFlipH = 0;
  428. if (slideXfrmNode.GetTextByPath("@flipV") != null && slideXfrmNode.GetTextByPath("@flipV").Value == "1")
  429. {// if ( getTextByPathList(slideXfrmNode, ["attrs", "flipV"]) === "1" || getTextByPathList(slideXfrmNode, ["attrs", "flipH"]) === "1")
  430. isFlipV = 1;
  431. }
  432. if (slideXfrmNode.GetTextByPath("@flipH") != null && slideXfrmNode.GetTextByPath("@flipH").Value == "1")
  433. {
  434. isFlipH = 1;
  435. }
  436. //rotate
  437. var rotate = ShapeHelper.AngleToDegrees(slideXfrmNode.GetTextByPath("@rot"));
  438. //console.log("rotate: "+rotate);
  439. double txtRotate;
  440. var txtXframeNode = node.GetTextByPath("p:txXfrm/@rot");
  441. if (txtXframeNode != null)
  442. {
  443. txtRotate = ShapeHelper.AngleToDegrees(txtXframeNode) + 90;
  444. }
  445. else
  446. {
  447. txtRotate = rotate;
  448. }
  449. if (shapType != null || custShapType != null)
  450. {
  451. Position position = ShapeHelper.GetPosition(slideXfrmNode, slideLayoutXfrmNode, slideMasterXfrmNode);
  452. position.Rot = rotate;
  453. position.FlipH = isFlipH;
  454. position.FlipV = isFlipV;
  455. //result += "<svg class='drawing' _id='" + id + "' _idx='" + idx + "' _type='" + type + "' _name='" + name +
  456. // "' style='" +
  457. // getPosition(slideXfrmNode, null, null) +
  458. // getSize(slideXfrmNode, null, null) +
  459. // " z-index: " + order + ";" +
  460. // "transform: rotate(" + rotate + "deg);" +
  461. // "'>";
  462. //result += "<defs>";
  463. // Fill Color
  464. var Fill = await GetShapeFill(node, warpObj);
  465. var grndFillFlg = false;
  466. var imgFillFlg = false;
  467. var clrFillType = ShapeHelper.GetFillType(node.GetTextByPath("p:spPr"));
  468. /////////////////////////////////////////
  469. if (clrFillType == "GRADIENT_FILL")
  470. {
  471. grndFillFlg = true;
  472. var color_arry = Fill.gradColor;
  473. var angl = Fill.Rot;
  474. var svgGrdnt = ShapeHelper.GetSvgGradient(position.Cx, position.Cy, angl, color_arry, order, slideLayoutClrOvride, warpObj.themeContent);
  475. //fill="url(#linGrd)"
  476. result += svgGrdnt;
  477. }
  478. else if (clrFillType == "PIC_FILL")
  479. {
  480. imgFillFlg = true;
  481. var svgBgImg = ShapeHelper.GetSvgImagePattern(Fill.Image, order);
  482. //fill="url(#imgPtrn)"
  483. //console.log(svgBgImg)
  484. result += svgBgImg;
  485. }
  486. else
  487. {
  488. if (clrFillType != null && clrFillType != "SOLID_FILL" && clrFillType != "PATTERN_FILL")
  489. {
  490. Fill.HtmlText = "none";
  491. }
  492. if (shapType != null)
  493. {
  494. if (shapType.Value == "arc" ||
  495. shapType.Value == "bracketPair" ||
  496. shapType.Value == "bracePair" ||
  497. shapType.Value == "leftBracket" ||
  498. shapType.Value == "leftBrace" ||
  499. shapType.Value == "rightBrace" ||
  500. shapType.Value == "rightBracket")
  501. { //Temp. solution - TODO
  502. Fill.HtmlText = "none";
  503. }
  504. }
  505. }
  506. // Border Color
  507. var border = ShapeHelper.GetBorder(node, true, "shape", slideLayoutClrOvride, warpObj.themeContent);
  508. var headEndNodeAttrs = node.GetTextByPath("p:spPr/a:ln/a:headEnd");
  509. var tailEndNodeAttrs = node.GetTextByPath("p:spPr/a:ln/a:tailEnd");
  510. // type: none, triangle, stealth, diamond, oval, arrow
  511. if ((headEndNodeAttrs != null && (headEndNodeAttrs.GetTextByPath("@type").Value == "triangle" || (headEndNodeAttrs.GetTextByPath("@type").Value == "arrow")) ||
  512. (tailEndNodeAttrs != null && (tailEndNodeAttrs.GetTextByPath("@type").Value == "triangle" || (tailEndNodeAttrs.GetTextByPath("@type").Value == "arrow")))))
  513. {
  514. var triangleMarker = "<marker id='markerTriangle_" + order + "' viewBox='0 0 10 10' refX='1' refY='5' markerWidth='5' markerHeight='5' stroke='" + border.Color + "' fill='" + border.Color +
  515. "' orient='auto-start-reverse' markerUnits='strokeWidth'><path d='M 0 0 L 10 5 L 0 10 z' /></marker>";
  516. result += triangleMarker;
  517. }
  518. result += "</defs>";
  519. }
  520. return result;
  521. }
  522. /// <summary>
  523. /// 幻灯片 背景色填充
  524. /// </summary>
  525. /// <param name="slideContent"></param>
  526. /// <param name="slideLayoutContent"></param>
  527. /// <param name="slideMasterContent"></param>
  528. /// <param name="index"></param>
  529. /// <param name="warpObj"></param>
  530. /// <returns></returns>
  531. public async Task<Fill> GetSlideBackgroundFill(XmlNode slideContent, int index, WarpObj warpObj)
  532. {
  533. var bgPr = slideContent.GetTextByPath("p:sld/p:cSld/p:bg/p:bgPr");
  534. var bgRef = slideContent.GetTextByPath("p:sld/p:cSld/p:bg/p:bgRef");
  535. if (bgPr != null)
  536. {
  537. var bgFillTyp = ShapeHelper.GetFillType(bgPr);
  538. if (bgFillTyp == "SOLID_FILL")
  539. {
  540. var sldFill = bgPr.GetTextByPath("a:solidFill");
  541. var bgColor = ShapeHelper.GetSolidFill(sldFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  542. bgColor = PowerPointHelper.ColorToning(sldFill.OuterXml, bgColor);
  543. return new Fill { Type = 1, Color = bgColor };
  544. }
  545. else if (bgFillTyp == "GRADIENT_FILL")
  546. {
  547. return ShapeHelper.GetBgGradientFill(bgPr, null, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  548. }
  549. else if (bgFillTyp == "PIC_FILL")
  550. {
  551. return await GetPicFill("slideBg", bgPr.GetTextByPath("a:blipFill"), warpObj);
  552. }
  553. }
  554. else if (bgRef != null)
  555. {
  556. string phClr = ShapeHelper.GetSolidFill(bgRef, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  557. phClr = PowerPointHelper.ColorToning(bgRef.OuterXml, phClr);
  558. var idx = bgRef.GetTextByPath("@idx");
  559. if (idx != null)
  560. {
  561. int value = int.Parse(idx.Value);
  562. if (value == 0 || value == 1000)
  563. {
  564. return new Fill { Type = 0 };
  565. //no background
  566. }
  567. else if (value > 0 && value < 1000)
  568. {
  569. //fillStyleLst in themeContent
  570. //themeContent["a:fmtScheme"]["a:fillStyleLst"]
  571. //bgcolor = "background: red;";
  572. }
  573. else if (value > 1000)
  574. {
  575. var trueIdx = value - 1000;
  576. var bgFillLst = warpObj.themeContent.GetTextByPath("a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst");
  577. if (bgFillLst != null) {
  578. XmlNodeList nodeList = bgFillLst.ChildNodes;
  579. if (trueIdx < nodeList.Count) {
  580. string type= ShapeHelper.GetFillType(nodeList[trueIdx - 1]);
  581. if (type.Equals("SOLID_FILL"))
  582. {
  583. if (string.IsNullOrEmpty(phClr))
  584. {
  585. phClr = ShapeHelper.GetSolidFill(nodeList[trueIdx - 1], slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  586. }
  587. phClr = PowerPointHelper.ColorToning(nodeList[trueIdx - 1].Value, phClr);
  588. return new Fill { Type = 1, Color = phClr };
  589. }
  590. else if (type.Equals("GRADIENT_FILL"))
  591. {
  592. return ShapeHelper.GetBgGradientFill(nodeList[trueIdx - 1], phClr, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  593. }
  594. else if (type.Equals("PIC_FILL"))
  595. {
  596. return await GetPicFill("slideBg", nodeList[trueIdx - 1].GetTextByPath("a:blipFill"), warpObj);
  597. }
  598. else {
  599. return new Fill { Type = 0 };
  600. }
  601. }
  602. }
  603. }
  604. }
  605. //var node = bgRef.GetTextByPath("");
  606. //if (bgRef["a:srgbClr"] !== undefined)
  607. //{
  608. // phClr = getTextByPathList(bgRef,["a:srgbClr", "attrs", "val"]); //#...
  609. //}
  610. //else if (bgRef["a:schemeClr"] !== undefined)
  611. //{ //a:schemeClr
  612. // var schemeClr = getTextByPathList(bgRef,["a:schemeClr", "attrs", "val"]);
  613. // phClr = getSchemeColorFromTheme("a:" + schemeClr, slideMasterContent); //#...
  614. // //console.log("schemeClr",schemeClr,"phClr=",phClr)
  615. //}
  616. }
  617. else
  618. {
  619. bgPr =warpObj. slideLayoutContent.GetTextByPath("p:sldLayout/p:cSld/p:bg/p:bgPr");
  620. bgRef = warpObj. slideLayoutContent.GetTextByPath("p:sldLayout/p:cSld/p:bg/p:bgRef");
  621. if (bgPr != null)
  622. {
  623. var bgFillTyp = ShapeHelper.GetFillType(bgPr);
  624. if (bgFillTyp == "SOLID_FILL")
  625. {
  626. var sldFill = bgPr.GetTextByPath("a:solidFill");
  627. var bgColor = ShapeHelper.GetSolidFill(sldFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  628. bgColor = PowerPointHelper.ColorToning(sldFill.OuterXml, bgColor);
  629. return new Fill { Type = 1, Color = bgColor };
  630. }
  631. else if (bgFillTyp == "GRADIENT_FILL")
  632. {
  633. return ShapeHelper.GetBgGradientFill(bgPr, null, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  634. }
  635. else if (bgFillTyp == "PIC_FILL")
  636. {
  637. return await GetPicFill("slideBg", bgPr.GetTextByPath("a:blipFill"), warpObj);
  638. }
  639. }
  640. else if (bgRef != null)
  641. {
  642. string phClr = ShapeHelper.GetSolidFill(bgRef, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  643. phClr = PowerPointHelper.ColorToning(bgRef.OuterXml, phClr);
  644. var idx = bgRef.GetTextByPath("@idx");
  645. if (idx != null)
  646. {
  647. int value = int.Parse(idx.Value);
  648. if (value == 0 || value == 1000)
  649. {
  650. return new Fill { Type = 0 };
  651. //no background
  652. }
  653. else if (value > 0 && value < 1000)
  654. {
  655. //fillStyleLst in themeContent
  656. //themeContent["a:fmtScheme"]["a:fillStyleLst"]
  657. //bgcolor = "background: red;";
  658. }
  659. else if (value > 1000)
  660. {
  661. var trueIdx = value - 1000;
  662. var bgFillLst = warpObj.themeContent.GetTextByPath("a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst");
  663. if (bgFillLst != null)
  664. {
  665. XmlNodeList nodeList = bgFillLst.ChildNodes;
  666. if (trueIdx < nodeList.Count)
  667. {
  668. string type = ShapeHelper.GetFillType(nodeList[trueIdx - 1]);
  669. if (type.Equals("SOLID_FILL"))
  670. {
  671. if (string.IsNullOrEmpty(phClr))
  672. {
  673. phClr = ShapeHelper.GetSolidFill(nodeList[trueIdx - 1], slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  674. }
  675. phClr = PowerPointHelper.ColorToning(nodeList[trueIdx - 1].Value, phClr);
  676. return new Fill { Type = 1, Color = phClr };
  677. }
  678. else if (type.Equals("GRADIENT_FILL"))
  679. {
  680. return ShapeHelper.GetBgGradientFill(nodeList[trueIdx - 1], phClr, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  681. }
  682. else if (type.Equals("PIC_FILL"))
  683. {
  684. return await GetPicFill("slideBg", nodeList[trueIdx - 1].GetTextByPath("a:blipFill"), warpObj);
  685. }
  686. else
  687. {
  688. return new Fill { Type = 0 };
  689. }
  690. }
  691. }
  692. }
  693. }
  694. }
  695. }
  696. return null;
  697. }
  698. public async Task<Fill> GetPicFill(string type, XmlNode node, WarpObj warpObj)
  699. {
  700. //Need to test/////////////////////////////////////////////
  701. //rId
  702. //TODO - Image Properties - Tile, Stretch, or Display Portion of Image
  703. //(http://officeopenxml.com/drwPic-tile.php)
  704. // var img = "";
  705. var rId = node.GetTextByPath("a:blip/@r:embed");//node["a:blip"]["attrs"]["r:embed"];
  706. Dictionary<string, string> imgPath = new Dictionary<string, string>();
  707. if (type == "slideBg")
  708. {
  709. imgPath = warpObj.slideResObj[rId.Value];
  710. }
  711. else if (type == "layoutBg")
  712. {
  713. imgPath = warpObj.layoutResObj[rId.Value];
  714. }
  715. else if (type == "masterBg")
  716. {
  717. imgPath = warpObj.masterResObj[rId.Value];
  718. }
  719. if (imgPath == null)
  720. {
  721. return null;
  722. }
  723. //var imgExt = imgPath["type"];
  724. //if (imgExt == "xml")
  725. //{
  726. // return null;
  727. //}
  728. var imgData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + imgPath["target"] + "']");
  729. if (imgData != null)
  730. {
  731. AzureBlobModel model = await SaveBase64ToBolob(imgData.InnerText, imgData.GetTextByPath("@pkg:contentType").Value,
  732. fileShaCode + "/imgs", null);
  733. return new Fill { Type = 3, Image = model.BlobUrl };
  734. }
  735. else { return null; }
  736. // var imgArrayBuffer = warpObj.zip.GetTextByPath(imgPath).Value.asArrayBuffer();
  737. //var imgMimeType = GetMimeType(imgExt);
  738. //img = "data:" + imgMimeType + ";base64," + base64ArrayBuffer(imgArrayBuffer);
  739. }
  740. public async Task<Fill> GetShapeFill(XmlNode node, WarpObj warpObj)
  741. {
  742. // 1. presentationML
  743. // p:spPr/ [a:noFill, solidFill, gradFill, blipFill, pattFill, grpFill]
  744. // From slide
  745. //Fill Type:
  746. //console.log("ShapeFill: ", node)
  747. var fillType = ShapeHelper.GetFillType(node.GetTextByPath("p:spPr"));
  748. Fill fill = new Fill();
  749. //0
  750. if (fillType == "NO_FILL")
  751. {
  752. fill.SvgText = "node";
  753. fill.Type = 0;
  754. fill.HtmlText = "background-color: initial;";
  755. return fill;
  756. //return isSvgMode ? "none" : "background-color: initial;";
  757. }
  758. //1纯色填充
  759. else if (fillType == "SOLID_FILL")
  760. {
  761. XmlNode shpFill = node.GetTextByPath("p:spPr/a:solidFill");// node["p:spPr"]["a:solidFill"];
  762. string color = ShapeHelper.GetSolidFill(shpFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  763. return new Fill { Type = 1, Color = color };
  764. }
  765. //2渐变色填充
  766. else if (fillType == "GRADIENT_FILL")
  767. {
  768. var shpFill = node.GetTextByPath("p:spPr/a:gradFill");// node["p:spPr"]["a:gradFill"];
  769. // fillColor = GetSolidFill(shpFill);
  770. fill = ShapeHelper.GetGradientFill(shpFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  771. //console.log("shpFill",shpFill,grndColor.color)
  772. }//3图片 纹理填充
  773. else if (fillType == "PIC_FILL")
  774. {
  775. var shpFill = node.GetTextByPath("p:spPr/a:blipFill"); //node["p:spPr"]["a:blipFill"];
  776. fill = await GetPicFill("slideBg", shpFill, warpObj);
  777. }//4 图案填充
  778. else if (fillType == "PATTERN_FILL")
  779. {
  780. /*
  781. *
  782. *<a:pattFill prst="ltDnDiag">
  783. <a:fgClr>
  784. <a:schemeClr val="dk1">
  785. <a:lumMod val="15000"/>
  786. <a:lumOff val="85000"/>
  787. </a:schemeClr>
  788. </a:fgClr>
  789. <a:bgClr>
  790. <a:schemeClr val="lt1"/>
  791. </a:bgClr>
  792. </a:pattFill>
  793. */
  794. var shpFill = node.GetTextByPath("p:spPr/a:pattFill");// node["p:spPr"]["a:pattFill"];
  795. fill = ShapeHelper.GetPatternFill(shpFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  796. }
  797. // 2. drawingML namespace
  798. if (fill == null)
  799. {
  800. var clrName = node.GetTextByPath("p:style/a:fillRef");
  801. string color = ShapeHelper.GetSolidFill(clrName, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
  802. }
  803. if (fill != null)
  804. {
  805. if (fillType == "GRADIENT_FILL")
  806. {
  807. //if (isSvgMode)
  808. //{
  809. // // console.log("GRADIENT_FILL color", fillColor.color[0])
  810. // return fill;
  811. //}
  812. //else
  813. //{
  814. var colorAry = fill.gradColor;
  815. var rot = fill.Rot;
  816. var bgcolor = "background: linear-gradient(" + rot + "deg,";
  817. for (var i = 0; i < colorAry.Count; i++)
  818. {
  819. if (i == colorAry.Count - 1)
  820. {
  821. bgcolor += colorAry[i] + ");";
  822. }
  823. else
  824. {
  825. bgcolor += colorAry[i] + ", ";
  826. }
  827. }
  828. fill.HtmlText = bgcolor;
  829. return fill;
  830. // }
  831. }
  832. else if (fillType == "PIC_FILL")
  833. {
  834. //if (isSvgMode)
  835. //{
  836. // return fill;
  837. //}
  838. //else
  839. //{
  840. fill.HtmlText = "background-image:url(" + fill.Image + ");";
  841. return fill;
  842. // }
  843. }
  844. else
  845. {
  846. Console.WriteLine("#" + fill.Color);
  847. //if (isSvgMode)
  848. //{
  849. // var color = new colz.Color(fill);
  850. // fill = color.rgb.toString();
  851. fill.HtmlText = "background-color: #" + fill.Color + ";";
  852. return fill;
  853. //}
  854. //else
  855. //{
  856. // //console.log(node,"fillColor: ",fillColor,"fillType: ",fillType,"isSvgMode: ",isSvgMode)
  857. // return fill;
  858. //}
  859. }
  860. }
  861. else
  862. {
  863. //if (isSvgMode)
  864. //{
  865. // return fill;
  866. //}
  867. //else
  868. //{
  869. fill.SvgText = "none";
  870. fill.HtmlText = "background-color: initial;";
  871. return fill;
  872. // }
  873. }
  874. }
  875. public async Task<AzureBlobModel> GetThumbnailModel(XmlDocument xdoc, string shaCode)
  876. {
  877. var thumbnailNode = xdoc.GetTextByPath("//pkg:part[@pkg:name='/docProps/thumbnail.jpeg']");
  878. if (thumbnailNode != null)
  879. {
  880. var contentType = thumbnailNode.GetTextByPath("@pkg:contentType");
  881. var thumbnail = thumbnailNode.InnerText;
  882. return await SaveBase64ToBolob(thumbnail, contentType.Value, shaCode + "/imgs", "thumbnail");
  883. }
  884. return null;
  885. }
  886. public XmlNode LoadTheme(XmlDocument xdoc)
  887. {
  888. var themeRelationship = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/_rels/presentation.xml.rels']/pkg:xmlData/rel:Relationships/rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme']/@Target");
  889. // "/ppt/theme/theme1.xml"
  890. string themeURI = "/ppt/" + themeRelationship.Value;
  891. return xdoc.GetTextByPath("//pkg:part[@pkg:name='" + themeURI + "']");
  892. }
  893. public async Task<AzureBlobModel> SaveBase64ToBolob(string data, string contentType, string FolderName, string fileName)
  894. {
  895. string fileExt;
  896. if (ContentTypeDict.extdict.TryGetValue(contentType, out string ext))
  897. {
  898. fileExt = ext;
  899. }
  900. else
  901. {
  902. //解决多种扩展名不能获取的
  903. string[] sp = contentType.Split("/");
  904. fileExt = sp[sp.Length - 1];
  905. sp = fileExt.Split("+");
  906. fileExt = "." + sp[sp.Length - 1];
  907. }
  908. //data:image/jpeg;base64,
  909. if (string.IsNullOrEmpty(fileName))
  910. {
  911. Stream stream = new MemoryStream(Convert.FromBase64String(data));
  912. string shaCode = ShaHashHelper.GetSHA1(stream);
  913. fileName = shaCode + fileExt;
  914. }
  915. else
  916. {
  917. if (fileName.Split(".").Length == 1)
  918. {
  919. fileName += fileExt;
  920. }
  921. }
  922. Stream streamBlob = new MemoryStream(Convert.FromBase64String(data));
  923. AzureBlobModel model = await azureBlobDBRepository.UploadFileByFolder(streamBlob, FolderName, fileName, "pptx", false);
  924. return model;
  925. }
  926. public async Task<Media> ProcessPicNode(XmlNode node, int order, WarpObj warpObj)
  927. {
  928. var xfrmList = "p:spPr/a:xfrm";
  929. var xfrmNode = node.GetTextByPath(xfrmList);
  930. Media media = new Media() { Type = "Pic" };
  931. var id = node.GetTextByPath("p:nvPicPr/p:cNvPr/@id");
  932. XmlNode slideLayoutSpNode = ShapeHelper.GetNodesTable(id, null, null, warpObj, "Layout");
  933. XmlNode slideMasterSpNode = ShapeHelper.GetNodesTable(id, null, null, warpObj, "Master");
  934. XmlNode slideLayoutXfrmNode = null;
  935. if (slideLayoutSpNode != null)
  936. {
  937. slideLayoutXfrmNode = slideLayoutSpNode.GetTextByPath(xfrmList);
  938. }
  939. XmlNode slideMasterXfrmNode = null;
  940. if (slideMasterSpNode != null)
  941. {
  942. slideMasterXfrmNode = slideMasterSpNode.GetTextByPath(xfrmList);
  943. }
  944. double rotate = 0;
  945. var rotateNode = node.GetTextByPath("p:spPr/a:xfrm/@rot");
  946. if (rotateNode != null)
  947. {
  948. rotate = ShapeHelper.AngleToDegrees(rotateNode);
  949. }
  950. Fill fill = await GetShapeFill(node, warpObj);
  951. media.Fill = fill;
  952. Border border = ShapeHelper.GetBorder(node, true, "shape", slideLayoutClrOvride, warpObj.themeContent);
  953. media.Border = border;
  954. Position position = ShapeHelper.GetPosition(xfrmNode, slideLayoutXfrmNode, slideMasterXfrmNode);
  955. media.Position = position;
  956. position.Rot = rotate;
  957. var rtrnData = "";
  958. //image
  959. var rid = node.GetTextByPath("p:blipFill/a:blip/@r:embed");
  960. if (rid != null)
  961. {
  962. var imgName = warpObj.slideResObj[rid.Value]["target"];
  963. Console.WriteLine(imgName);
  964. if (!string.IsNullOrEmpty(imgName))
  965. {
  966. var imgData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + imgName + "']");
  967. var imgFileExt = imgData.GetTextByPath("@pkg:contentType");
  968. AzureBlobModel imgModel = await SaveBase64ToBolob(imgData.InnerText, imgFileExt.Value, fileShaCode + "/imgs", "");
  969. media.Image = imgModel.BlobUrl;
  970. media.MediaType = "image";
  971. }
  972. }
  973. ///////////////////////////////////////Amir//////////////////////////////
  974. //video
  975. var vdoNode = node.GetTextByPath("p:nvPicPr/p:nvPr/a:videoFile/@r:link");
  976. if (vdoNode != null)
  977. {
  978. string vdoFile = warpObj.slideResObj[vdoNode.Value]["target"];
  979. if (!string.IsNullOrEmpty(vdoFile))
  980. {
  981. var videoData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + vdoFile + "']");
  982. var contentType = videoData.GetTextByPath("@pkg:contentType");
  983. AzureBlobModel vdoModel = await SaveBase64ToBolob(videoData.InnerText, contentType.Value, fileShaCode + "/media", "");
  984. media.Url = vdoModel.BlobUrl;
  985. media.MediaType = "video";
  986. }
  987. /// 处理为AzureBlob 上传
  988. // vdoMimeType = getMimeType(vdoFileExt);
  989. // blob = new Blob([uInt8Array], {
  990. // type: vdoMimeType
  991. // });
  992. // vdoBlob = URL.createObjectURL(blob);
  993. }
  994. //audio
  995. var audioNode = node.GetTextByPath("p:nvPicPr/p:nvPr/a:audioFile/@r:link");
  996. // var audioObjc;
  997. if (audioNode != null)
  998. {
  999. string audioFile = warpObj.slideResObj[audioNode.Value]["target"];
  1000. if (!string.IsNullOrEmpty(audioFile))
  1001. {
  1002. var audioData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + audioFile + "']");
  1003. var contentType = audioData.GetTextByPath("@pkg:contentType");
  1004. AzureBlobModel adoModel = await SaveBase64ToBolob(audioData.InnerText, contentType.Value, fileShaCode + "/media", "");
  1005. media.Url = adoModel.BlobUrl;
  1006. media.MediaType = "audio";
  1007. }
  1008. //uInt8ArrayAudio = zip.file(audioFile).asArrayBuffer();
  1009. //blobAudio = new Blob([uInt8ArrayAudio]);
  1010. //audioBlob = URL.createObjectURL(blobAudio);
  1011. //var cx = parseInt(xfrmNode["a:ext"]["attrs"]["cx"]) * 20;
  1012. //var cy = xfrmNode["a:ext"]["attrs"]["cy"];
  1013. //var x = parseInt(xfrmNode["a:off"]["attrs"]["x"]) / 2.5;
  1014. //var y = xfrmNode["a:off"]["attrs"]["y"];
  1015. //audioObjc = {
  1016. // "a:ext" : {
  1017. // "attrs":{
  1018. // "cx":cx,
  1019. // "cy":cy
  1020. // }
  1021. // },
  1022. // "a:off":{
  1023. // "attrs":{
  1024. // "x":x,
  1025. // "y":y
  1026. // }
  1027. // }
  1028. //}
  1029. // mediaSupportFlag = true;
  1030. }
  1031. //console.log(node)
  1032. //////////////////////////////////////////////////////////////////////////
  1033. //mimeType = imgFileExt;
  1034. //rtrnData = "<div class='block content' style='" +
  1035. // (( audioPlayerFlag) ? GetPosition(audioObjc, null, null) : GetPosition(xfrmNode, null, null)) +
  1036. // (( audioPlayerFlag) ? GetSize(audioObjc, null, null) :GetSize(xfrmNode, null, null)) +
  1037. // " z-index: " + order + ";" +
  1038. // "transform: rotate(" + rotate + "deg);'>";
  1039. //if ((vdoNode ==null && audioNode ==null) || !mediaSupportFlag)
  1040. //{
  1041. // rtrnData += "<img src='data:" + mimeType + ";base64," + base64ArrayBuffer(imgArrayBuffer) + "' style='width: 100%; height: 100%'/>";
  1042. //}
  1043. //else if ((vdoNode !=null || audioNode !=null) && mediaSupportFlag)
  1044. //{
  1045. // if (vdoNode !=null)
  1046. // {
  1047. // rtrnData += "<video src='" + vdoBlob + "' controls style='width: 100%; height: 100%'>Your browser does not support the video tag.</video>";
  1048. // }
  1049. // if (audioNode !=null)
  1050. // {
  1051. // rtrnData += "<audio id='audio_player' controls ><source src='" + audioBlob + "'></audio>";
  1052. // }
  1053. //}
  1054. rtrnData += "<span style='color:red;font-size:40px;position: absolute;'>This media file Not supported by HTML5</span>";
  1055. //if ((vdoNode != null || audioNode != null) && mediaSupportFlag)
  1056. //{
  1057. // //console.log("Founded supported media file but media process disabled (mediaProcess=false)");
  1058. //}
  1059. rtrnData += "</div>";
  1060. //console.log(rtrnData)
  1061. return media;
  1062. }
  1063. }
  1064. }