123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- using DocumentFormat.OpenXml.Drawing;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using HiTeachCC.Model.PowerPoint;
- using HiTeachCC.Service.PowerPoint.Interface;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.XPath;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.Context.Constant.Common;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Common.StringHelper;
- using TEAMModelOS.SDK.Helper.Security.ShaHash;
- using TEAMModelOS.SDK.Module.AzureBlob.Container;
- using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
- using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
- using Fill = HiTeachCC.Model.PowerPoint.Fill;
- namespace HiTeachCC.Service.PowerPoint.Implement
- {
- public class ShapeGenerator : IShapeGenerator
- {
- bool isDone = false;
- List<object> MsgQueue = new List<object>();
- private Dictionary<string, string> slideLayoutClrOvride { get; set; }
- private XmlNode themeContent { get; set; }
- int chartID = 0;
- int _order = 1;
- int titleFontSize = 42;
- int bodyFontSize = 20;
- int otherFontSize = 16;
- bool isSlideMode = false;
- Dictionary<string, Dictionary<string, string>> styleTable = new Dictionary<string, Dictionary<string, string>>();
- const double px96 = 96.00, px72 = 72.00, px914400 = 914400.00, px12700 = 12700.00;
- const double rot60000 = 60000.00;
- private readonly IAzureBlobDBRepository azureBlobDBRepository;
- public ShapeGenerator(IAzureBlobDBRepository _azureBlobDBRepository)
- {
- azureBlobDBRepository = _azureBlobDBRepository;
- }
- public async Task<Dictionary<string, object>> LoadPresentation(IFormFile file)
- {
- Dictionary<string, object> resdict = new Dictionary<string, object>();
- if (FileType.GetExtention(file.FileName).ToLower().Equals("pptx"))
- {
- ConvertPPTX(file, resdict);
- return resdict;
- }
- else if (FileType.GetExtention(file.FileName).ToLower().Equals("pdf"))
- {
- // await ProcessPDF(file, resdict);
- return resdict;
- }
- else
- {
- throw new BizException("file type does not support!", 500);
- }
- }
- public Dictionary<string, object> ConvertPPTX(IFormFile file, Dictionary<string, object> resdict)
- {
- string shaCode = ShaHashHelper.GetSHA1(file.OpenReadStream());
- using (PresentationDocument presentationDocument = PresentationDocument.Open(file.OpenReadStream(), false))
- {
- if (presentationDocument == null)
- {
- throw new ArgumentNullException("presentationDocument");
- }
- XDocument xdoc = presentationDocument.ToFlatOpcDocument();
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.LoadXml(xdoc.ToString());
- var rslt_ary = ProcessPPTX(xmlDocument, shaCode);
- //TODO
- return null;
- }
- }
- /// <summary>
- /// 加载PPTX文件
- /// </summary>
- /// <param name="presentationFile"></param>
- /// <returns></returns>
- public async Task<List<Dictionary<string, object>>> ProcessPPTX(XmlDocument xdoc, string shaCode)
- {
- List<Dictionary<string, object>> post_ary = new List<Dictionary<string, object>>();
- var dateBefore = DateTimeOffset.Now.UtcTicks;
- AzureBlobModel thumbnailModel = await GetThumbnailModel(xdoc, shaCode);
- post_ary.Add(new Dictionary<string, object> { { "pptx-thumb", thumbnailModel } });
- //获取全部的/ppt/slides/slide1.xml--->>/ppt/slides/slide(n).xml
- var slideNodes = xdoc.GetTextByPathList("//pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.presentationml.slide+xml']");
- //获取全部的/ppt/slideLayouts/slideLayout1.xml--->>/ppt/slideLayouts/slideLayout(n).xml
- var slideLayoutNodes = xdoc.GetTextByPathList("//pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml']");
- //获取ppt的宽高
- var slideSize = GetSlideSize(xdoc);
- post_ary.Add(new Dictionary<string, object> { { "slideSize", slideSize } });
- //加载当前PPT使用到的PPT节点
- themeContent = LoadTheme(xdoc);
- //获取所有的表格样式节点
- var tableStyles = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/tableStyles.xml']/pkg:xmlData/a:tblStyleLst");
- //处理slide1---slide(n)
- int numOfSlides = slideNodes.Count;
- for (int i = 0; i < numOfSlides; i++)
- {
- string filename = slideNodes[i].GetTextByPath("@pkg:name").Value;
- //处理当前页的幻灯片
- string slideHtml = ProcessSingleSlide(xdoc, slideNodes[i], filename, i, slideSize);
- post_ary.Add(new Dictionary<string, object> { { "slide", slideHtml } });
- post_ary.Add(new Dictionary<string, object> { { "progress-update", (i + 1) * 100 / numOfSlides } });
- }
- post_ary.Add(new Dictionary<string, object> { { "globalCSS", GenGlobalCSS() } });
- var dateAfter = DateTimeOffset.Now.UtcTicks;
- post_ary.Add(new Dictionary<string, object> { { "ExecutionTime", dateAfter - dateBefore } });
- return post_ary;
- }
- private Dictionary<string, string> GenGlobalCSS()
- {
- Dictionary<string, string> cssText = new Dictionary<string, string>();
- foreach (var key in styleTable.Keys)
- {
- // cssText += "div ." + styleTable[key]["name"] + "{" + styleTable[key]["text"] + "}\n"; //section > div
- var cssTextKey = styleTable[key]["name"];
- var cssTextValue = styleTable[key]["text"];
- cssText.Add(cssTextKey, cssTextValue);
- }
- return cssText;
- }
- private string ProcessSingleSlide(XmlDocument xdoc, XmlNode xnode, string sldFileName, int index, dynamic slideSize)
- {
- var resName = sldFileName.Replace("slides/slide", "slides/_rels/slide") + ".rels";
- var RelationshipArray = xdoc.GetTextByPathList("//pkg:part[@pkg:name='" + resName + "']/pkg:xmlData/rel:Relationships/rel:Relationship");
- Dictionary<string, Dictionary<string, string>> slideResObj = new Dictionary<string, Dictionary<string, string>>();
- var layoutFilename = "";
- foreach (XmlNode Relationship in RelationshipArray)
- {
- var RelationshipType = Relationship.GetTextByPath("@Type");
- if (RelationshipType.Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"))
- {
- layoutFilename = Relationship.GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- }
- else
- {
- //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide":
- //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image":
- //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart":
- //case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink":
- Dictionary<string, string> dict = new Dictionary<string, string>();
- string Type = Relationship.GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
- string Target = Relationship.GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- string Id = Relationship.GetTextByPath("@Id").Value;
- dict.Add("type", Type);
- dict.Add("target", Target);
- slideResObj.Add(Id, dict);
- }
- }
- // Open slideLayoutXX.xml
- var slideLayoutContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + layoutFilename + "']");
- dynamic slideLayoutTables = IndexNodes(slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout"));
- ///ppt/slides/slide5.xml 也包含a:overrideClrMapping
- ////ppt/slideLayouts/slideLayout13.xml 也包含a:overrideClrMapping
- var sldLayoutClrOvr = slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout/p:clrMapOvr/a:overrideClrMapping");
- if (sldLayoutClrOvr != null)
- {
- //获取 overrideClrMapping所有属性节点
- slideLayoutClrOvride = new Dictionary<string, string>() {
- { "accent1", sldLayoutClrOvr.GetTextByPath("@accent1").Value},
- { "accent2", sldLayoutClrOvr.GetTextByPath("@accent2").Value},
- { "accent3", sldLayoutClrOvr.GetTextByPath("@accent3").Value},
- { "accent4", sldLayoutClrOvr.GetTextByPath("@accent4").Value},
- { "accent5", sldLayoutClrOvr.GetTextByPath("@accent5").Value},
- { "accent6", sldLayoutClrOvr.GetTextByPath("@accent6").Value},
- { "bg1", sldLayoutClrOvr.GetTextByPath("@bg1").Value},
- { "bg2", sldLayoutClrOvr.GetTextByPath("@bg2").Value},
- { "folHlink", sldLayoutClrOvr.GetTextByPath("@folHlink").Value},
- { "hlink", sldLayoutClrOvr.GetTextByPath("@hlink").Value},
- { "tx1", sldLayoutClrOvr.GetTextByPath("@tx1").Value},
- { "tx2", sldLayoutClrOvr.GetTextByPath("@tx2").Value}
- };
- }
- // =====< Step 2 >=====
- // Read slide master filename of the slidelayout (Get slideMasterXX.xml)
- // @resName: ppt/slideLayouts/slideLayout1.xml
- // @masterName: ppt/slideLayouts/_rels/slideLayout1.xml.rels
- var slideLayoutResFilename = layoutFilename.Replace("slideLayouts/slideLayout", "slideLayouts/_rels/slideLayout") + ".rels";
- var slideLayoutResContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + slideLayoutResFilename + "']");
- ///查看js 是替换之前 还是添加
- RelationshipArray = slideLayoutResContent.GetTextByPathList("pkg:xmlData/rel:Relationships/rel:Relationship");
- var masterFilename = "";
- Dictionary<string, Dictionary<string, string>> layoutResObj = new Dictionary<string, Dictionary<string, string>>();
- for (int i = 0; i < RelationshipArray.Count; i++)
- {
- if (RelationshipArray[i].GetTextByPath("@Type").Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"))
- {
- masterFilename = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- }
- else
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- string Type = RelationshipArray[i].GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
- string Target = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- string Id = RelationshipArray[i].GetTextByPath("@Id").Value;
- dict.Add("type", Type);
- dict.Add("target", Target);
- layoutResObj.Add(Id, dict);
- }
- }
- // Open slideMasterXX.xml
- var slideMasterContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + masterFilename + "']");
- var slideMasterTextStyles = slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster/p:txStyles");
- var slideMasterTables = IndexNodes(slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster"));
- /////////////////Amir/////////////
- //Open slideMasterXX.xml.rels
- var slideMasterResFilename = masterFilename.Replace("slideMasters/slideMaster", "slideMasters/_rels/slideMaster") + ".rels";
- var slideMasterResContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + slideMasterResFilename + "']");
- RelationshipArray = slideLayoutResContent.GetTextByPathList("pkg:xmlData/rel:Relationships/rel:Relationship");
- var themeFilename = "";
- Dictionary<string, Dictionary<string, string>> masterResObj = new Dictionary<string, Dictionary<string, string>>();
- for (int i = 0; i < RelationshipArray.Count; i++)
- {
- if (RelationshipArray[i].GetTextByPath("@Type").Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"))
- {
- themeFilename = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- }
- else
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- string Type = RelationshipArray[i].GetTextByPath("@Type").Value.Replace("http://schemas.openxmlformats.org/officeDocument/2006/relationships/", "");
- string Target = RelationshipArray[i].GetTextByPath("@Target").Value.Replace("../", "/ppt/");
- string Id = RelationshipArray[i].GetTextByPath("@Id").Value;
- dict.Add("type", Type);
- dict.Add("target", Target);
- masterResObj.Add(Id, dict);
- }
- }
- //Load Theme file
- if (!string.IsNullOrEmpty(themeFilename))
- {
- themeContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + themeFilename + "']");
- }
- // =====< Step 3 >=====
- var slideContent = xdoc.GetTextByPath("//pkg:part[@pkg:name='" + sldFileName + "']");
- var nodes = slideContent.GetTextByPath("p:sld/p:cSld/p:spTree").ChildNodes;
- dynamic warpObj = new
- {
- zip = xdoc,
- slideLayoutTables,
- slideMasterTables,
- slideResObj,
- slideMasterTextStyles,
- layoutResObj,
- masterResObj
- };
- var bgColor = GetSlideBackgroundFill(slideContent, slideLayoutContent, slideMasterContent, warpObj);
- var result = "<div class='slide' style='width:" + slideSize.width + "px; height:" + slideSize.height + "px;" + bgColor + "'>";
- //result += "<div>"+getBackgroundShapes(slideContent, slideLayoutContent, slideMasterContent,warpObj) + "</div>" - TODO
- foreach (XmlNode node in nodes)
- {
- result += ProcessNodesInSlide(node.Name, node, warpObj);
- }
- return "";
- }
- private dynamic ProcessNodesInSlide(string nodeKey, XmlNode nodeValue, dynamic warpObj)
- {
- var result = "";
- switch (nodeKey)
- {
- case "p:sp": // Shape, Text
- result = ProcessSpNode(nodeValue, warpObj);
- break;
- case "p:cxnSp": // Shape, Text (with connection)
- result = ProcessCxnSpNode(nodeValue, warpObj);
- break;
- case "p:pic": // Picture
- result = ProcessPicNode(nodeValue, warpObj);
- break;
- case "p:graphicFrame": // Chart, Diagram, Table
- result = ProcessGraphicFrameNode(nodeValue, warpObj);
- break;
- case "p:grpSp":
- result = ProcessGroupSpNode(nodeValue, warpObj);
- break;
- case "mc:AlternateContent": //Equations and formulas as Image
- var mcFallbackNode = nodeValue.GetTextByPath("mc:Fallback/p:sp");
- result = ProcessSpNode(mcFallbackNode, warpObj);
- break;
- default:
- break;
- }
- return result;
- }
- private string ProcessGroupSpNode(XmlNode node, dynamic warpObj)
- {
- var factor =1.00* px96 / px914400;
- var xfrmNode = node.GetTextByPath("p:grpSpPr/a:xfrm");
- var x = int.Parse(xfrmNode.GetTextByPath("a:off/@x").Value) * factor;
- var y = int.Parse(xfrmNode.GetTextByPath("a:off/@y").Value) * factor;
- var chx = int.Parse(xfrmNode.GetTextByPath("a:chOff/@x").Value) * factor;
- var chy = int.Parse(xfrmNode.GetTextByPath("a:chOff/@y").Value) * factor;
- var cx = int.Parse(xfrmNode.GetTextByPath("a:ext/@cx").Value) * factor;
- var cy = int.Parse(xfrmNode.GetTextByPath("a:ext/@cy").Value) * factor;
- var chcx = int.Parse(xfrmNode.GetTextByPath("a:chExt/@cx").Value) * factor;
- var chcy = int.Parse(xfrmNode.GetTextByPath("a:chExt/@cy").Value) * factor;
- var order = node["attrs"]["order"];
- var 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;'>";
- // Procsee all child nodes
- var nodes = node.ChildNodes;
- foreach(XmlNode nd in nodes)
- {
- result += ProcessNodesInSlide(nd.Name, nd, warpObj);
- }
- result += "</div>";
- return result;
- }
- private string ProcessGraphicFrameNode(XmlNode nodeValue, dynamic warpObj)
- {
- throw new NotImplementedException();
- }
- private string ProcessPicNode(XmlNode nodeValue, dynamic warpObj)
- {
- throw new NotImplementedException();
- }
- private string ProcessCxnSpNode(XmlNode nodeValue, dynamic warpObj)
- {
- throw new NotImplementedException();
- }
- private string ProcessSpNode(XmlNode nodeValue, dynamic warpObj)
- {
- throw new NotImplementedException();
- }
- private Fill GetSlideBackgroundFill(XmlNode slideContent ,XmlNode slideLayoutContent , XmlNode slideMasterContent , dynamic warpObj) {
- var bgPr = slideContent.GetTextByPath("p:sld/p:cSld/p:bg/p:bgPr");
- var bgRef = slideContent.GetTextByPath("p:sld/p:cSld/p:bg/p:bgRef");
- var bgcolor="";
- if (bgPr != null)
- {
- var bgFillTyp = GetFillType(bgPr);
- if (bgFillTyp == "SOLID_FILL")
- {
- var sldFill = bgPr.GetTextByPath("a:solidFill");
- var bgColor = GetSolidFill(sldFill);
- var sldTint = GetColorOpacity(sldFill);
- bgcolor = "background: rgba(" + HexToRgbNew(bgColor.Color) + "," + sldTint + ");";
- }
- else if (bgFillTyp == "GRADIENT_FILL")
- {
- bgcolor = getBgGradientFill(bgPr, null, slideMasterContent);
- }
- else if (bgFillTyp == "PIC_FILL")
- {
- bgcolor = getBgPicFill(bgPr, "slideBg", warpObj);
- }
- }
- else if (bgRef != null)
- {
- }
- else {
-
- }
- return null;
- }
- private string getBgPicFill(XmlNode bgPr, string sorce,dynamic warpObj)
- {
- var bgcolor="";
- var picFillBase64 = GetPicFill(sorce, bgPr.GetTextByPath("a:blipFill"), warpObj);
- var ordr = bgPr["attrs"]["order"];
- //a:srcRect
- //a:stretch => a:fillRect =>attrs (l:-17000, r:-17000)
- bgcolor = "background-image: url(" + picFillBase64 + "); z-index: " + ordr + ";";
- return bgcolor;
- }
- private static Fill GetPicFill(string type, XmlNode node, dynamic warpObj)
- {
- //Need to test/////////////////////////////////////////////
- //rId
- //TODO - Image Properties - Tile, Stretch, or Display Portion of Image
- //(http://officeopenxml.com/drwPic-tile.php)
- var img = "";
- var rId = PowerPointHelper.GetTextByPath(node, "a:blip/@r:embed");//node["a:blip"]["attrs"]["r:embed"];
- var imgPath = "";
- if (type == "slideBg")
- {
- imgPath = PowerPointHelper.GetTextByPath(warpObj, "slideResObj/" + rId + "/target").Value;
- }
- else if (type == "layoutBg")
- {
- imgPath = PowerPointHelper.GetTextByPath(warpObj, "layoutResObj/" + rId + "/target").Value;
- }
- else if (type == "masterBg")
- {
- imgPath = PowerPointHelper.GetTextByPath(warpObj, "masterResObj/" + rId + "/target").Value;
- }
- if (imgPath == null)
- {
- return null;
- }
- var imgExt = imgPath.Split(".").Last();
- if (imgExt == "xml")
- {
- return null;
- }
- var imgArrayBuffer = warpObj.zip.file(imgPath).asArrayBuffer();
- //var imgMimeType = GetMimeType(imgExt);
- //img = "data:" + imgMimeType + ";base64," + base64ArrayBuffer(imgArrayBuffer);
- return null;
- }
- private string getBgGradientFill(XmlNode bgPr, XmlNode phClr, XmlNode slideMasterContent)
- {
- var bgcolor = "";
- if (bgPr !=null)
- {
- var grdFill = bgPr.GetTextByPath("a:gradFill");
- var gsLst = grdFill.GetTextByPathList("a:gsLst/a:gs");
- // var startColorNode, endColorNode;
- var color_ary = new List<string >();
- var tint_ary = new List<int>();
- for (var i = 0; i < gsLst.Count; i++)
- {
- var lo_tint ="" ;
- var lo_color = "";
- if (gsLst[i]["a:srgbClr"] != null)
- {
- if (phClr == null)
- {
- lo_color = gsLst[i].GetTextByPath("a:srgbClr/@val").Value; //#...
- }
- lo_tint = gsLst[i].GetTextByPath("a:srgbClr/a:tint/@val").Value;
- }
- else if (gsLst[i]["a:schemeClr"] != null)
- { //a:schemeClr
- if (phClr == null)
- {
- var schemeClr = gsLst[i].GetTextByPath("a:schemeClr/@val");
- lo_color = GetSchemeColorFromTheme("a:" + schemeClr, slideMasterContent); //#...
- }
- lo_tint = gsLst[i].GetTextByPath("a:schemeClr/a:tint/@val").Value;
- //console.log("schemeClr",schemeClr,slideMasterContent)
- }
- //console.log("lo_color",lo_color)
- color_ary[i] = lo_color;
- tint_ary[i] = (lo_tint !=null) ? int.Parse(lo_tint) / 100000 : 1;
- }
- //get rot
- var lin = grdFill["a:lin"];
- var rot = 90.0;
- if (lin !=null)
- {
- rot = AngleToDegrees(lin.GetTextByPath("@ang")) + 90;
- }
- bgcolor = "background: linear-gradient(" + rot + "deg,";
- for (var i = 0; i < gsLst.Count; i++)
- {
- if (i == gsLst.Count - 1)
- {
- if (phClr ==null)
- {
- bgcolor += "rgba(" + HexToRgbNew(color_ary[i]) + "," + tint_ary[i] + ")" + ");";
- }
- else
- {
- bgcolor += "rgba(" + HexToRgbNew(phClr.Value) + "," + tint_ary[i] + ")" + ");";
- }
- }
- else
- {
- if (phClr == null)
- {
- bgcolor += "rgba(" + HexToRgbNew(color_ary[i]) + "," + tint_ary[i] + ")" + ", ";
- }
- else
- {
- bgcolor += "rgba(" + HexToRgbNew(phClr.Value) + "," + tint_ary[i] + ")" + ", ";
- }
- }
- }
- }
- else
- {
- if (phClr == null)
- {
- bgcolor = "rgba(" + HexToRgbNew(phClr.Value) + ",0);";
- }
- }
- return bgcolor;
- }
- public double AngleToDegrees(XmlNode angle)
- {
- if (angle.Value == "" || angle == null)
- {
- return 0;
- }
- int.TryParse(angle.Value, out int angleInt);
- double a = angleInt / 60000;
- return System.Math.Round(a);
- }
- private string HexToRgbNew(string bgColor)
- {
- //var arrBuff = new ArrayBuffer(4);
- //var vw = new DataView(arrBuff);
- //vw.setUint32(0, parseInt(hex, 16), false);
- //var arrByte = new Uint8Array(arrBuff);
- int r = Convert.ToInt32("0x" + bgColor.Substring(0, 2), 16);
- int g = Convert.ToInt32("0x" + bgColor.Substring(2, 2), 16);
- int b = Convert.ToInt32("0x" + bgColor.Substring(4, 2), 16);
- return r + "," + g+ "," +b;
- }
- private double GetColorOpacity(XmlNode solidFill)
- {
- double opcity = 1;
- if (solidFill == null)
- {
- return opcity;
- }
- if (solidFill.GetTextByPath("a:srgbClr") != null)
- {
- var tint = solidFill.GetTextByPath("a:srgbClr/a:tint/@val");
- if (tint !=null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- else if (solidFill.GetTextByPath("a:schemeClr") != null)
- {
- var tint = solidFill.GetTextByPath("a:schemeClr/a:tint/@val");
- if (tint != null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- else if (solidFill.GetTextByPath("a:scrgbClr")!= null)
- {
- var tint = solidFill.GetTextByPath("a:scrgbClr/a:tint/@val");
- if (tint != null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- else if (solidFill.GetTextByPath("a:prstClr") != null)
- {
- var tint = solidFill.GetTextByPath("a:prstClr/a:tint/@val");
- if (tint != null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- else if (solidFill.GetTextByPath("a:hslClr") != null)
- {
- var tint = solidFill.GetTextByPath("a:hslClr/a:tint/@val");
- if (tint != null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- else if (solidFill.GetTextByPath("a:sysClr") !=null)
- {
- var tint = solidFill.GetTextByPath("a:sysClr/a:tint/@val");
- if (tint != null)
- {
- opcity = double.Parse(tint.Value) / 100000;
- }
- }
- return opcity;
- }
- public Fill GetSolidFill(XmlNode node)
- {
- if (node == null)
- {
- return null;
- }
- Fill fill = new Fill() { Color = "FFF" };
- if (PowerPointHelper.GetTextByPath(node, "a:srgbClr").Value != null)
- {
- fill.Color = PowerPointHelper.GetTextByPath(node, "a:srgbClr/@val").Value; //#...
- }
- else if (PowerPointHelper.GetTextByPath(node, "a:schemeClr").Value != null)//node["a:schemeClr"] != null)
- { //a:schemeClr
- var schemeClr = PowerPointHelper.GetTextByPath(node, "a:schemeClr/@val");
- //console.log(schemeClr)
- fill.Color = GetSchemeColorFromTheme(schemeClr.Value, null); //#...
- }
- else if (PowerPointHelper.GetTextByPath(node, "a:scrgbClr").Value != null)
- {
- //<a:scrgbClr r="50%" g="50%" b="50%"/> //Need to test/////////////////////////////////////////////
-
- var defBultColorValsR = PowerPointHelper.GetTextByPath(node, "a:scrgbClr/@r");
- var defBultColorValsG = PowerPointHelper.GetTextByPath(node, "a:scrgbClr/@g");
- var defBultColorValsB = PowerPointHelper.GetTextByPath(node, "a:scrgbClr/@b");
- var red = (defBultColorValsR.Value.IndexOf("%") != -1) ? defBultColorValsR.Value.Split("%").First() : defBultColorValsR.Value;
- var green = (defBultColorValsG.Value.IndexOf("%") != -1) ? defBultColorValsG.Value.Split("%").First() : defBultColorValsG.Value;
- var blue = (defBultColorValsB.Value.IndexOf("%") != -1) ? defBultColorValsB.Value.Split("%").First() : defBultColorValsB.Value;
- var scrgbClr = red + "," + green + "," + blue;
- fill.Color = ToHex(255 * (double.Parse(red) / 100)) + ToHex(255 * (double.Parse(green) / 100)) + ToHex(255 * (double.Parse(blue) / 100));
- //console.log("scrgbClr: " + scrgbClr);
- }
- else if (PowerPointHelper.GetTextByPath(node, "a:prstClr").Value != null)
- {
- //<a:prstClr val="black"/> //Need to test/////////////////////////////////////////////
- var prstClr = PowerPointHelper.GetTextByPath(node, "a:prstClr/@val");// node["a:prstClr"]["attrs"]["val"];
- fill.Color = GetColorName2Hex(prstClr.Value);
- //console.log("prstClr: " + prstClr+" => hexClr: "+color);
- }
- else if (PowerPointHelper.GetTextByPath(node, "a:hslClr").Value != null)
- {
- //<a:hslClr hue="14400000" sat="100%" lum="50%"/> //Need to test/////////////////////////////////////////////
- var defBultColorVals = PowerPointHelper.GetTextByPath(node, "a:hslClr");//["attrs"];
- var defBultColorVals_hue = PowerPointHelper.GetTextByPath(defBultColorVals, "@hue");
- var defBultColorVals_sat = PowerPointHelper.GetTextByPath(defBultColorVals, "@sat");
- var defBultColorVals_lum = PowerPointHelper.GetTextByPath(defBultColorVals, "@lum");
- var hue = double.Parse(defBultColorVals_hue.Value) / 100000;
- var sat = double.Parse((defBultColorVals_sat.Value.IndexOf("%") != -1) ? defBultColorVals_sat.Value.Split("%").First() : defBultColorVals_sat.Value) / 100;
- var lum = double.Parse((defBultColorVals_lum.Value.IndexOf("%") != -1) ? defBultColorVals_lum.Value.Split("%").First() : defBultColorVals_lum.Value) / 100;
- var hslClr = defBultColorVals_hue.Value.ToString() + "," + defBultColorVals_sat.Value.ToString() + "," + defBultColorVals_lum.Value.ToString();
- var hsl2rgb = HslToRgb(hue, sat, lum);
- fill.Color = ToHex(hsl2rgb.r) + ToHex(hsl2rgb.g) + ToHex(hsl2rgb.b);
- //defBultColor = cnvrtHslColor2Hex(hslClr); //TODO
- // console.log("hslClr: " + hslClr);
- }
- else if (PowerPointHelper.GetTextByPath(node, "a:sysClr").Value != null)
- {
- //<a:sysClr val="windowText" lastClr="000000"/> //Need to test/////////////////////////////////////////////
- var sysClr = PowerPointHelper.GetTextByPath(node, "a:sysClr/@lastClr").Value;
- if (sysClr != null)
- {
- fill.Color = sysClr;
- }
- }
- return fill;
- }
- public string GetSchemeColorFromTheme(string schemeClr, XmlNode sldMasterNode)
- {
- //<p:clrMap ...> in slide master
- // e.g. tx2="dk2" bg2="lt2" tx1="dk1" bg1="lt1" slideLayoutClrOvride
- if (slideLayoutClrOvride == null ||slideLayoutClrOvride.Count<=0)
- {
- var sldLayoutClrOvr = PowerPointHelper.GetTextByPath(sldMasterNode, "p:sldMaster/p:clrMap");
- if (sldLayoutClrOvr != null)
- {
- //获取 overrideClrMapping所有属性节点 检查测试 是否slideLayoutClrOvride 需要被覆盖
- slideLayoutClrOvride = new Dictionary<string, string>() {
- { "accent1", sldLayoutClrOvr.GetTextByPath("@accent1").Value},
- { "accent2", sldLayoutClrOvr.GetTextByPath("@accent2").Value},
- { "accent3", sldLayoutClrOvr.GetTextByPath("@accent3").Value},
- { "accent4", sldLayoutClrOvr.GetTextByPath("@accent4").Value},
- { "accent5", sldLayoutClrOvr.GetTextByPath("@accent5").Value},
- { "accent6", sldLayoutClrOvr.GetTextByPath("@accent6").Value},
- { "bg1", sldLayoutClrOvr.GetTextByPath("@bg1").Value},
- { "bg2", sldLayoutClrOvr.GetTextByPath("@bg2").Value},
- { "folHlink", sldLayoutClrOvr.GetTextByPath("@folHlink").Value},
- { "hlink", sldLayoutClrOvr.GetTextByPath("@hlink").Value},
- { "tx1", sldLayoutClrOvr.GetTextByPath("@tx1").Value},
- { "tx2", sldLayoutClrOvr.GetTextByPath("@tx2").Value}
- };
- }
- }
- //console.log(slideLayoutClrOvride);
- var schmClrName = schemeClr.Substring(2);
- if (slideLayoutClrOvride != null)
- {
- switch (schmClrName)
- {
- case "tx1":
- case "tx2":
- case "bg1":
- case "bg2":
- schemeClr = "a:" +slideLayoutClrOvride[schmClrName];
- //schemeClr = "a:" + slideLayoutClrOvride[schmClrName];
- //console.log(schmClrName+ "=> "+schemeClr);
- break;
- }
- }
- else
- {
- switch (schmClrName)
- {
- case "tx1":
- schemeClr = "a:dk1";
- break;
- case "tx2":
- schemeClr = "a:dk2";
- break;
- case "bg1":
- schemeClr = "a:lt1";
- break;
- case "bg2":
- schemeClr = "a:lt2";
- break;
- }
- }
- //var refNode = PowerPointHelper.GetTextByPathList(themeContent, "a:theme/a:themeElements/a:clrScheme/"+ schemeClr.Value);
- var color = PowerPointHelper.GetTextByPath(themeContent, "a:theme/a:themeElements/a:clrScheme/" + schemeClr + "/a:srgbClr/@val");
- if (color == null)//&& refNode != null
- {
- color = PowerPointHelper.GetTextByPath(themeContent, "a:theme/a:themeElements/a:clrScheme/" + schemeClr + "/a:sysClr/@lastClr");
- }
- //console.log(color)
- return color.Value;
- }
- private static string ToHex(dynamic n)
- {
- string hex = n.toString(16);
- while (hex.Length < 2) { hex = "0" + hex; }
- return hex;
- }
- private static dynamic HslToRgb(double hue, double sat, double light)
- {
- double t1, t2, r, g, b;
- hue /= 60;
- if (light <= 0.5)
- {
- t2 = light * (sat + 1);
- }
- else
- {
- t2 = light + sat - (light * sat);
- }
- t1 = light * 2 - t2;
- r = HueToRgb(t1, t2, hue + 2) * 255;
- g = HueToRgb(t1, t2, hue) * 255;
- b = HueToRgb(t1, t2, hue - 2) * 255;
- return new { r, g, b };
- }
- private static double HueToRgb(double t1, double t2, double hue)
- {
- if (hue < 0) hue += 6;
- if (hue >= 6) hue -= 6;
- if (hue < 1) return (t2 - t1) * hue + t1;
- else if (hue < 3) return t2;
- else if (hue < 4) return (t2 - t1) * (4 - hue) + t1;
- else return t1;
- }
- private static string GetColorName2Hex(string name)
- {
- var hex = "";
- var colorName = new List<string> { "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue", "FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod", "Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenRodYellow", "LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "RebeccaPurple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "SlateGrey", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen" };
- var colorHex = new List<string> { "f0f8ff", "faebd7", "00ffff", "7fffd4", "f0ffff", "f5f5dc", "ffe4c4", "000000", "ffebcd", "0000ff", "8a2be2", "a52a2a", "deb887", "5f9ea0", "7fff00", "d2691e", "ff7f50", "6495ed", "fff8dc", "dc143c", "00ffff", "00008b", "008b8b", "b8860b", "a9a9a9", "a9a9a9", "006400", "bdb76b", "8b008b", "556b2f", "ff8c00", "9932cc", "8b0000", "e9967a", "8fbc8f", "483d8b", "2f4f4f", "2f4f4f", "00ced1", "9400d3", "ff1493", "00bfff", "696969", "696969", "1e90ff", "b22222", "fffaf0", "228b22", "ff00ff", "dcdcdc", "f8f8ff", "ffd700", "daa520", "808080", "808080", "008000", "adff2f", "f0fff0", "ff69b4", "cd5c5c", "4b0082", "fffff0", "f0e68c", "e6e6fa", "fff0f5", "7cfc00", "fffacd", "add8e6", "f08080", "e0ffff", "fafad2", "d3d3d3", "d3d3d3", "90ee90", "ffb6c1", "ffa07a", "20b2aa", "87cefa", "778899", "778899", "b0c4de", "ffffe0", "00ff00", "32cd32", "faf0e6", "ff00ff", "800000", "66cdaa", "0000cd", "ba55d3", "9370db", "3cb371", "7b68ee", "00fa9a", "48d1cc", "c71585", "191970", "f5fffa", "ffe4e1", "ffe4b5", "ffdead", "000080", "fdf5e6", "808000", "6b8e23", "ffa500", "ff4500", "da70d6", "eee8aa", "98fb98", "afeeee", "db7093", "ffefd5", "ffdab9", "cd853f", "ffc0cb", "dda0dd", "b0e0e6", "800080", "663399", "ff0000", "bc8f8f", "4169e1", "8b4513", "fa8072", "f4a460", "2e8b57", "fff5ee", "a0522d", "c0c0c0", "87ceeb", "6a5acd", "708090", "708090", "fffafa", "00ff7f", "4682b4", "d2b48c", "008080", "d8bfd8", "ff6347", "40e0d0", "ee82ee", "f5deb3", "ffffff", "f5f5f5", "ffff00", "9acd32" };
- var findIndx = colorName.IndexOf(name);
- if (findIndx != -1)
- {
- hex = colorHex[findIndx];
- }
- return hex;
- }
- public static string GetFillType(XmlNode node)
- {
- //Need to test/////////////////////////////////////////////
- //SOLID_FILL
- //PIC_FILL
- //GRADIENT_FILL
- //PATTERN_FILL
- //NO_FILL
- var fillType = "";
- if( node.GetTextByPath( "a:noFill")!= null)
- {
- fillType = "NO_FILL";
- }
- if (node.GetTextByPath("a:solidFill") != null)
- {
- fillType = "SOLID_FILL";
- }
- if (node.GetTextByPath("a:gradFill") != null)
- {
- fillType = "GRADIENT_FILL";
- }
- if (node.GetTextByPath("a:pattFill") != null)
- {
- fillType = "PATTERN_FILL";
- }
- if (node.GetTextByPath("a:blipFill") != null)
- {
- fillType = "PIC_FILL";
- }
- return fillType;
- }
- private dynamic IndexNodes(XmlNode content)
- {
- var idTable = new Dictionary<string, Dictionary<string,XmlNode>>();
- var idxTable = new Dictionary<string, Dictionary<string, XmlNode>>();
- var typeTable = new Dictionary<string, Dictionary<string, XmlNode>>();
- var spTreeNode = content.GetTextByPath("p:cSld/p:spTree");
- Dictionary<string, Dictionary<string, string>> slideResObj = new Dictionary<string, Dictionary<string, string>>();
- var spTreeNodeChildren = spTreeNode.ChildNodes;
- foreach (XmlNode child in spTreeNodeChildren)
- {
- if (child.Name.Equals("p:nvGrpSpPr") || child.Name.Equals("p:grpSpPr"))
- {
- continue;
- }
- //var targetNode = child.ChildNodes;
-
- var targetNode = child.GetTextByPath("p:nvSpPr");
- // < p:cNvCxnSpPr >
- // < a:cxnSpLocks /> ///这个是什么意思
- // </ p:cNvCxnSpPr >
- if (targetNode == null) {
- targetNode = child.GetTextByPath("p:nvCxnSpPr");
- }
- var id = targetNode.GetTextByPath("p:cNvPr/@id");
- var idx = targetNode.GetTextByPath("p:nvPr/p:ph/@idx");
- var type = targetNode.GetTextByPath("p:nvPr/p:ph/@type");
- Dictionary<string, XmlNode> node = new Dictionary<string, XmlNode>();
- if (child.ChildNodes != null) {
- foreach (XmlNode xmlNode in child.ChildNodes) {
- node.TryAdd(xmlNode.Name, xmlNode);
- }
- }
- if (id != null)
- {
- idTable.Add(id.Value, node);
- }
- if (idx != null)
- {
- idxTable.Add(idx.Value, node);
- }
- if (type != null)
- {
- typeTable.Add(type.Value, node);
- }
- }
- return new { idTable, idxTable, typeTable };
- }
- private dynamic GetSlideSize(XmlDocument xdoc)
- {
- var sldSzNode = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/presentation.xml']/pkg:xmlData/p:presentation/p:sldSz");
- double width = double.Parse(sldSzNode.GetTextByPath("@cx").Value) * px96 / px914400;
- double height = double.Parse(sldSzNode.GetTextByPath("@cy").Value) * px96 / px914400;
- return new { width, height };
- }
- private async Task<AzureBlobModel> GetThumbnailModel(XmlDocument xdoc, string shaCode)
- {
- var thumbnailNode = xdoc.GetTextByPath("//pkg:part[@pkg:name='/docProps/thumbnail.jpeg']");
- if (thumbnailNode != null)
- {
- var contentType = thumbnailNode.GetTextByPath("@pkg:contentType");
- var thumbnail = thumbnailNode.InnerText;
- return await PowerPointHelper.SaveBase64ToBolob(thumbnail, contentType.Value, shaCode, "thumbnail", azureBlobDBRepository);
- }
- return null;
- }
- private XmlNode LoadTheme(XmlDocument xdoc)
- {
- 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");
- // "/ppt/theme/theme1.xml"
- string themeURI = "/ppt/" + themeRelationship.Value;
- return xdoc.GetTextByPath("//pkg:part[@pkg:name='" + themeURI + "']");
- }
- }
- }
|