ソースを参照

完善PPT背景填充,图片填充方式的拉伸数据的解析。

CrazyIter 5 年 前
コミット
85951cc45d

+ 7 - 3
TEAMModelOS.SDK/Helper/Common/ColorHelper/ColorConverter.cs

@@ -11,8 +11,12 @@ namespace TEAMModelOS.SDK.Helper.Common.ColorHelper
         {
 
             Color c = ColorTranslator.FromHtml("#" + s);
-
-            return SetLuminanceMod(c, luminance);
+            try {
+                return SetLuminanceMod(c, luminance);
+            } catch (Exception e) {
+                return s;
+            }
+            
         }
         public string SetLuminanceOff(string s, double luminance)
         {
@@ -387,7 +391,7 @@ namespace TEAMModelOS.SDK.Helper.Common.ColorHelper
                     b = clr[2];
                 }
             }
-
+            
             return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
 
         }

+ 13 - 5
TEAMModelOS.Service/Models/PowerPoint/Pptx.cs

@@ -5,18 +5,26 @@ using System.Text;
 namespace HiTeachCC.Model.PowerPoint
 {
     public class Pptx
-    {   
-        //宽度
-        public double Width { get; set; }
-        //高度
-        public double Height { get; set; }
+    {
+        public Pptx() {
+            slides = new List<Slide>();
+            size = new PptSize();
+        }
+       public PptSize size { get; set; } 
         //缩略图
         public string Thumbnail { get; set; }
         //PPT幻灯片集合
         public List<string> HiXmls { get; set; }
+        public  List<Slide> slides { get; set; }
         public string PptUrl { get; set; }
         public string Sha1Code { get; set; }
         //页数
         public int PageSize { get; set; }
     }
+    public class PptSize {
+        //宽度
+        public double Width { get; set; }
+        //高度
+        public double Height { get; set; }
+    }
 }

+ 19 - 16
TEAMModelOS.Service/Services/PowerPoint/Implement/PowerPointHelper.cs

@@ -837,15 +837,15 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             TEAMModelOS.SDK.Helper.Common.ColorHelper.ColorConverter converter = new TEAMModelOS.SDK.Helper.Common.ColorHelper.ColorConverter();
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(nodeXml);
-            XmlNode lumMod = doc.SelectSingleNode("//a:lumMod/@val", xmlnsManager);
-            if (lumMod != null)
+            XmlNode tint = doc.SelectSingleNode("//a:tint/@val", xmlnsManager);
+            if (tint != null)
             {
-                colorHex = converter.SetLuminanceMod(colorHex, double.Parse(lumMod.Value));
+                colorHex = converter.SetTint(colorHex, double.Parse(tint.Value));
             }
-            XmlNode lumOff = doc.SelectSingleNode("//a:lumOff/@val", xmlnsManager);
-            if (lumOff != null)
+            XmlNode hueMod = doc.SelectSingleNode("//a:hueMod/@val", xmlnsManager);
+            if (hueMod != null)
             {
-                colorHex = converter.SetLuminanceOff(colorHex, double.Parse(lumOff.Value));
+                colorHex = converter.SetHueMod(colorHex, double.Parse(hueMod.Value));
             }
             XmlNode satMod = doc.SelectSingleNode("//a:satMod/@val", xmlnsManager);
             if (satMod != null)
@@ -857,16 +857,23 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             {
                 colorHex = converter.SetSaturationOff(colorHex, double.Parse(satOff.Value));
             }
+            XmlNode lumMod = doc.SelectSingleNode("//a:lumMod/@val", xmlnsManager);
+            if (lumMod != null)
+            {
+                colorHex = converter.SetLuminanceMod(colorHex, double.Parse(lumMod.Value));
+            }
+            XmlNode lumOff = doc.SelectSingleNode("//a:lumOff/@val", xmlnsManager);
+            if (lumOff != null)
+            {
+                colorHex = converter.SetLuminanceOff(colorHex, double.Parse(lumOff.Value));
+            }
+          
             XmlNode hueOff = doc.SelectSingleNode("//a:hueOff/@val", xmlnsManager);
             if (hueOff != null)
             {
                 colorHex = converter.SetHueOff(colorHex, double.Parse(hueOff.Value));
             }
-            XmlNode hueMod = doc.SelectSingleNode("//a:hueMod/@val", xmlnsManager);
-            if (hueMod != null)
-            {
-                colorHex = converter.SetHueMod(colorHex, double.Parse(hueMod.Value));
-            }
+         
             XmlNode alphaOff = doc.SelectSingleNode("//a:alphaOff/@val", xmlnsManager);
             if (alphaOff != null)
             {
@@ -878,11 +885,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             {
                 colorHex = converter.SetShade(colorHex, double.Parse(shade.Value));
             }
-            XmlNode tint = doc.SelectSingleNode("//a:tint/@val", xmlnsManager);
-            if (tint != null)
-            {
-                colorHex = converter.SetTint(colorHex, double.Parse(tint.Value));
-            }
+           
             XmlNode alpha = doc.SelectSingleNode("//a:alpha/@val", xmlnsManager);
             if (alpha != null)
             {

+ 1 - 1
TEAMModelOS.Service/Services/PowerPoint/Implement/PowerPointService.cs

@@ -210,7 +210,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                             pageSize++;
                         }
                     }
-                    Pptx info = new Pptx { HiXmls = slides, Width = x, Height = y, PageSize = pageSize, Thumbnail = url, Sha1Code = shaCode };
+                    Pptx info = new Pptx { HiXmls = slides,size=new PptSize { Width=x,Height=y}, PageSize = pageSize, Thumbnail = url, Sha1Code = shaCode };
                     AzureBlobModel model = await azureBlobDBRepository.UploadFileByFolder(file.OpenReadStream(), shaCode, shaCode + ".pptx", "pptx", false);
                     info.PptUrl = model.BlobUrl;
                     resdict.Add("pptx", info);

+ 152 - 26
TEAMModelOS.Service/Services/PowerPoint/Implement/ShapeGenerator.cs

@@ -93,20 +93,24 @@ namespace HiTeachCC.Service.PowerPoint.Implement
         /// </summary>
         /// <param name="presentationFile"></param>
         /// <returns></returns>
-        public async Task< Dictionary<string, object>> ProcessPPTX(XmlDocument xdoc, string shaCode)
+        public async Task<Pptx> ProcessPPTX(XmlDocument xdoc, string shaCode)
         {
+            Pptx pptx = new Pptx();
           //  List<Dictionary<string, object>> post_ary = new List<Dictionary<string, object>>();
             Dictionary<string, object> post_ary = new Dictionary<string, object>();
             var dateBefore = DateTimeOffset.Now.UtcTicks;
             AzureBlobModel thumbnailModel = await GetThumbnailModel(xdoc, shaCode);
-            post_ary.Add(   "pptx-thumb", thumbnailModel  );
+           // post_ary.Add(   "pptx-thumb", thumbnailModel  );
+
+            pptx.Thumbnail = thumbnailModel != null ? thumbnailModel.BlobUrl : "";
             //获取全部的/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 = ShapeHelper.GetSlideSize(xdoc);
-            post_ary.Add( "slideSize", slideSize  );
+            pptx.size = new PptSize { Width = slideSize.width, Height = slideSize.height };
+         //   post_ary.Add( "slideSize", slideSize  );
             //加载当前PPT使用到的PPT节点
             var themeContent = LoadTheme(xdoc);
             //获取所有的表格样式节点
@@ -116,19 +120,25 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             List<Slide> slides = new List<Slide>();
             for (int i = 0; i < numOfSlides; i++)
             {
-                string filename = slideNodes[i].GetTextByPath("@pkg:name").Value;
+               
+                var node = xdoc.GetTextByPath("//pkg:part[@pkg:name='/ppt/slides/slide" + (i + 1) + ".xml']");
+                string filename = node.GetTextByPath("@pkg:name").Value;
                 //处理当前页的幻灯片
-                Slide slide = await ProcessSingleSlide(xdoc, slideNodes[i], filename, i, slideSize, themeContent);
+                Slide slide = await ProcessSingleSlide(xdoc, node, filename, i, slideSize, themeContent);
                 slides.Add(slide);
               //  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( "slide", slides );
+
+            pptx.slides = slides;
+            pptx.Sha1Code = shaCode;
+            pptx.PageSize = slides.Count;
+            //post_ary.Add( "slide", slides );
             //post_ary.Add(  "globalCSS", GenGlobalCSS()  );
-            var dateAfter = DateTimeOffset.Now.UtcTicks;
+           // var dateAfter = DateTimeOffset.Now.UtcTicks;
            // post_ary.Add(  "ExecutionTime", dateAfter - dateBefore );
-            return post_ary;
+            return pptx;
         }
 
         public Dictionary<string, string> GenGlobalCSS()
@@ -354,7 +364,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             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;'>";
             Group group = new Group();
-            group.Position = new Position { X = x - chx, Y = y - chy, Cx = cx - chcx, Cy = cy - chcy, Rot = rotate };
+            group.Position = new Position { X =System.Math.Round(  x - chx,degree), Y = System.Math.Round(y - chy,degree), Cx = System.Math.Round(cx - chcx,degree), Cy = System.Math.Round(cy - chcy,degree), Rot = System.Math.Round(rotate,degree) };
             group.Type = "Group";
             group.Index = order++;
             // Procsee all child nodes
@@ -511,7 +521,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                 txtRotate = rotate;
             }
             Position position = ShapeHelper.GetPosition(slideXfrmNode, slideLayoutXfrmNode, slideMasterXfrmNode);
-            position.Rot = rotate;
+            position.Rot = System.Math.Round(rotate,degree);
             position.FlipH = isFlipH;
             position.FlipV = isFlipV;
             var grndFillFlg = false;
@@ -1498,7 +1508,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             if (txtBrdrNode != null)
             {
                 var txBrd = ShapeHelper.GetBorder(node, true, "text", slideLayoutClrOvride, slideMasterContent, themeContent);
-                var brdSize = txBrd.Width * (4 / 3) + "px";
+                var brdSize = System.Math.Round(txBrd.Width * (4 / 3),degree) + "px";
                 var brdClr = txBrd.Color;
                 textBordr = "-" + brdSize + " 0 " + brdClr + ", 0 " + brdSize + " " + brdClr + ", " + brdSize + " 0 " + brdClr + ", 0 -" + brdSize + " " + brdClr + ";";
             }
@@ -1608,8 +1618,8 @@ namespace HiTeachCC.Service.PowerPoint.Implement
         /// <returns></returns>
         public async Task<Fill> GetSlideBackgroundFill(XmlNode slideContent, int index, WarpObj 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 bgPr = slideContent.GetTextByPath("pkg:xmlData/p:sld/p:cSld/p:bg/p:bgPr");
+            var bgRef = slideContent.GetTextByPath("pkg:xmlData/p:sld/p:cSld/p:bg/p:bgRef");
             if (bgPr != null)
             {
                 var bgFillTyp = ShapeHelper.GetFillType(bgPr);
@@ -1704,8 +1714,8 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             }
             else
             {
-                bgPr = warpObj.slideLayoutContent.GetTextByPath("p:sldLayout/p:cSld/p:bg/p:bgPr");
-                bgRef = warpObj.slideLayoutContent.GetTextByPath("p:sldLayout/p:cSld/p:bg/p:bgRef");
+                bgPr = warpObj.slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout/p:cSld/p:bg/p:bgPr");
+                bgRef = warpObj.slideLayoutContent.GetTextByPath("pkg:xmlData/p:sldLayout/p:cSld/p:bg/p:bgRef");
                 if (bgPr != null)
                 {
                     var bgFillTyp = ShapeHelper.GetFillType(bgPr);
@@ -1713,7 +1723,8 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                     {
                         var sldFill = bgPr.GetTextByPath("a:solidFill");
                         var bgColor = ShapeHelper.GetSolidFill(sldFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
-                        if (sldFill!=null) {
+                        if (sldFill != null)
+                        {
                             bgColor = PowerPointHelper.ColorToning(sldFill.OuterXml, bgColor);
                         }
 
@@ -1725,7 +1736,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                     }
                     else if (bgFillTyp == "PIC_FILL")
                     {
-                        return await GetPicFill("slideBg", bgPr.GetTextByPath("a:blipFill"), warpObj);
+                        return await GetPicFill("layoutBg", bgPr.GetTextByPath("a:blipFill"), warpObj);
 
                     }
                 }
@@ -1764,7 +1775,8 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                                         {
                                             phClr = ShapeHelper.GetSolidFill(nodeList[trueIdx - 1], slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
                                         }
-                                        if (nodeList[trueIdx - 1] != null) {
+                                        if (nodeList[trueIdx - 1] != null)
+                                        {
                                             phClr = PowerPointHelper.ColorToning(nodeList[trueIdx - 1].Value, phClr);
                                         }
                                         return new Fill { Type = 1, Color = phClr };
@@ -1786,6 +1798,93 @@ namespace HiTeachCC.Service.PowerPoint.Implement
                         }
                     }
                 }
+                else
+                {
+                    bgPr = warpObj.slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster/p:cSld/p:bg/p:bgPr");
+                    bgRef = warpObj.slideMasterContent.GetTextByPath("pkg:xmlData/p:sldMaster/p:cSld/p:bg/p:bgRef");
+                    if (bgPr != null)
+                    {
+                        var bgFillTyp = ShapeHelper.GetFillType(bgPr);
+                        if (bgFillTyp == "SOLID_FILL")
+                        {
+                            var sldFill = bgPr.GetTextByPath("a:solidFill");
+                            var bgColor = ShapeHelper.GetSolidFill(sldFill, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
+                            if (sldFill != null)
+                            {
+                                bgColor = PowerPointHelper.ColorToning(sldFill.OuterXml, bgColor);
+                            }
+
+                            return new Fill { Type = 1, Color = bgColor };
+                        }
+                        else if (bgFillTyp == "GRADIENT_FILL")
+                        {
+                            return ShapeHelper.GetBgGradientFill(bgPr, null, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
+                        }
+                        else if (bgFillTyp == "PIC_FILL")
+                        {
+                            return await GetPicFill("masterBg", bgPr.GetTextByPath("a:blipFill"), warpObj);
+
+                        }
+                    }
+                    else if (bgRef != null)
+                    {
+                        string phClr = ShapeHelper.GetSolidFill(bgRef, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
+                        phClr = PowerPointHelper.ColorToning(bgRef.OuterXml, phClr);
+                        var idx = bgRef.GetTextByPath("@idx");
+                        if (idx != null)
+                        {
+                            int value = int.Parse(idx.Value);
+                            if (value == 0 || value == 1000)
+                            {
+                                return new Fill { Type = 0 };
+                                //no background
+                            }
+                            else if (value > 0 && value < 1000)
+                            {
+                                //fillStyleLst in themeContent
+                                //themeContent["a:fmtScheme"]["a:fillStyleLst"]
+                                //bgcolor = "background: red;";
+                            }
+                            else if (value > 1000)
+                            {
+                                var trueIdx = value - 1000;
+                                var bgFillLst = warpObj.themeContent.GetTextByPath("a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst");
+                                if (bgFillLst != null)
+                                {
+                                    XmlNodeList nodeList = bgFillLst.ChildNodes;
+                                    if (trueIdx < nodeList.Count)
+                                    {
+                                        string type = ShapeHelper.GetFillType(nodeList[trueIdx - 1]);
+                                        if (type.Equals("SOLID_FILL"))
+                                        {
+                                            if (string.IsNullOrEmpty(phClr))
+                                            {
+                                                phClr = ShapeHelper.GetSolidFill(nodeList[trueIdx - 1], slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
+                                            }
+                                            if (nodeList[trueIdx - 1] != null)
+                                            {
+                                                phClr = PowerPointHelper.ColorToning(nodeList[trueIdx - 1].Value, phClr);
+                                            }
+                                            return new Fill { Type = 1, Color = phClr };
+                                        }
+                                        else if (type.Equals("GRADIENT_FILL"))
+                                        {
+                                            return ShapeHelper.GetBgGradientFill(nodeList[trueIdx - 1], phClr, slideLayoutClrOvride, warpObj.slideMasterContent, warpObj.themeContent);
+                                        }
+                                        else if (type.Equals("PIC_FILL"))
+                                        {
+                                            return await GetPicFill("slideBg", nodeList[trueIdx - 1].GetTextByPath("a:blipFill"), warpObj);
+                                        }
+                                        else
+                                        {
+                                            return new Fill { Type = 0 };
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
             }
             return null;
         }
@@ -1818,19 +1917,46 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             {
                 return null;
             }
-            //var imgExt = imgPath["type"];
-            //if (imgExt == "xml")
-            //{
-            //    return null;
-            //}
+           
+                //var imgExt = imgPath["type"];
+                //if (imgExt == "xml")
+                //{
+                //    return null;
+                //}
 
-            var imgData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + imgPath["target"] + "']");
+                var imgData = warpObj.zip.GetTextByPath("//pkg:part[@pkg:name='" + imgPath["target"] + "']");
 
             if (imgData != null)
             {
                 AzureBlobModel model = await SaveBase64ToBolob(imgData.InnerText, imgData.GetTextByPath("@pkg:contentType").Value,
                     fileShaCode + "/imgs", null);
-                return new Fill { Type = 3, Image = model.BlobUrl };
+                XmlNode fillRect_node = node.GetTextByPath("a:stretch/a:fillRect");
+                Fill fill = new Fill { Type = 3, Image = model.BlobUrl };
+                if (fillRect_node != null)
+                {
+                    FillStyle fillStyle = new FillStyle();
+                    XmlNode left = node.GetTextByPath("a:stretch/a:fillRect/@l");
+                    XmlNode top = node.GetTextByPath("a:stretch/a:fillRect/@t");
+                    XmlNode right = node.GetTextByPath("a:stretch/a:fillRect/@r");
+                    XmlNode bottom = node.GetTextByPath("a:stretch/a:fillRect/@b");
+                    if (left != null) {
+                        fillStyle.Left = int.Parse(left.Value);
+                    }
+                    if (top != null)
+                    {
+                        fillStyle.Top = int.Parse(top.Value);
+                    }
+                    if (right != null)
+                    {
+                        fillStyle.Right = int.Parse(right.Value);
+                    }
+                    if (bottom != null)
+                    {
+                        fillStyle.Bottom = int.Parse(bottom.Value);
+                    }
+                    fill.FillStyle = fillStyle;
+                }
+                return fill;
             }
             else { return null; }
 
@@ -2085,7 +2211,7 @@ namespace HiTeachCC.Service.PowerPoint.Implement
             media.Border = border;
             Position position = ShapeHelper.GetPosition(xfrmNode, slideLayoutXfrmNode, slideMasterXfrmNode);
             media.Position = position;
-            position.Rot = rotate;
+            position.Rot = System.Math.Round(rotate,degree);
             var rtrnData = "";
             //image
             var rid = node.GetTextByPath("p:blipFill/a:blip/@r:embed");

+ 4 - 4
TEAMModelOS.Service/Services/PowerPoint/Implement/ShapeHelper.cs

@@ -540,10 +540,10 @@ namespace TEAMModelOS.Service.Services.PowerPoint.Implement
             {
                 off = PowerPointHelper.GetTextByPath(slideMasterXfrmNode, "a:off");
             }
-            x = double.Parse(off.GetTextByPath("@x").Value) * px96 / px914400;
-            y = double.Parse(off.GetTextByPath("@y").Value) * px96 / px914400;
-            w = double.Parse(ext.GetTextByPath("@cx").Value) * px96 / px914400;
-            h = double.Parse(ext.GetTextByPath("@cy").Value) * px96 / px914400;
+            x =System.Math.Round(double.Parse(off.GetTextByPath("@x").Value) * px96 / px914400,3);
+            y =   System.Math.Round(double.Parse(off.GetTextByPath("@y").Value) * px96 / px914400,3);
+            w =   System.Math.Round(double.Parse(ext.GetTextByPath("@cx").Value) * px96 / px914400,3);
+            h = System.Math.Round(double.Parse(ext.GetTextByPath("@cy").Value) * px96 / px914400,3);
             return new Position()
             {
                 Cx = w,