|
@@ -0,0 +1,180 @@
|
|
|
+using DocumentFormat.OpenXml;
|
|
|
+using DocumentFormat.OpenXml.Packaging;
|
|
|
+using DocumentFormat.OpenXml.Presentation;
|
|
|
+using HiTeachCC.Model.PowerPoint;
|
|
|
+using HiTeachCC.Service.Core.Implement;
|
|
|
+using HiTeachCC.Service.PowerPoint.Interface;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using ColorMap = DocumentFormat.OpenXml.Presentation.ColorMap;
|
|
|
+using Theme = DocumentFormat.OpenXml.Drawing.Theme;
|
|
|
+
|
|
|
+namespace HiTeachCC.Service.PowerPoint.Implement
|
|
|
+{
|
|
|
+ public class PowerPointService : BaseService, IPowerPointService
|
|
|
+ {
|
|
|
+ const double inchpixel = 96.00, inchpt = 72.00, pxBase = 914400.00, ptBase = 12700;
|
|
|
+ const double rotBase = 60000.00;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 加载PPTX文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="presentationFile"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Pptx LoadPresentation(Stream presentationStream)
|
|
|
+ {
|
|
|
+ using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationStream, false))
|
|
|
+ {
|
|
|
+ if (presentationDocument == null)
|
|
|
+ {
|
|
|
+ throw new ArgumentNullException("presentationDocument");
|
|
|
+ }
|
|
|
+ // Get a PresentationPart object from the PresentationDocument object.
|
|
|
+
|
|
|
+ var thumbnailPart = presentationDocument.ThumbnailPart;
|
|
|
+ var contentType = thumbnailPart.ContentType;
|
|
|
+ string base64 = "";
|
|
|
+ using (var stream = thumbnailPart.GetStream())
|
|
|
+ {
|
|
|
+ byte[] buffer = new byte[stream.Length];
|
|
|
+ stream.Read(buffer, 0, buffer.Length);
|
|
|
+ stream.Close();
|
|
|
+ base64 = System.Convert.ToBase64String(buffer);
|
|
|
+ base64 = "data:" + contentType + ";base64," + base64;
|
|
|
+ }
|
|
|
+ PresentationPart presentationPart = presentationDocument.PresentationPart;
|
|
|
+ if (presentationPart != null && presentationPart.Presentation != null)
|
|
|
+ {
|
|
|
+ // Get a Presentation object from the PresentationPart object.
|
|
|
+ Presentation presentation = presentationPart.Presentation;
|
|
|
+ int pageSize = 0;
|
|
|
+ var slideMasterParts = presentationPart.SlideMasterParts;
|
|
|
+ ColorMap colorMap = null;
|
|
|
+ Theme theme = null;
|
|
|
+ foreach (var slideMasterPart in slideMasterParts)
|
|
|
+ {
|
|
|
+ if (colorMap != null && theme != null)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ colorMap = slideMasterPart.SlideMaster.ColorMap;
|
|
|
+ theme = slideMasterPart.ThemePart.Theme;
|
|
|
+ }
|
|
|
+ Dictionary<string,string> colors= DoColorMap(colorMap, theme);
|
|
|
+ //获取PPT 一页大小
|
|
|
+ double x = presentation.SlideSize.Cx * inchpixel / pxBase;
|
|
|
+ double y = presentation.SlideSize.Cy * inchpixel / pxBase;
|
|
|
+ List<HiTeachCC.Model.PowerPoint.Slide> slides = new List<HiTeachCC.Model.PowerPoint.Slide>();
|
|
|
+ if (presentation.SlideIdList != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ // presentation.SlideIdList.OrderBy(x=>x.);
|
|
|
+ // Get the title of each slide in the slide order.
|
|
|
+ // 获取的是几页PPT数量
|
|
|
+ foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
|
|
|
+ {
|
|
|
+ HiTeachCC.Model.PowerPoint.Slide slide = new HiTeachCC.Model.PowerPoint.Slide();
|
|
|
+ // 获取这一页 PPT 的id
|
|
|
+ string id = slideId.RelationshipId;
|
|
|
+ SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
|
|
|
+ //获取当前页 PPT 的所有元素
|
|
|
+ // slide.Items = GetSlideElement(slidePart, theme, colorMap);
|
|
|
+ //slide.Xml = slidePart.Slide.OuterXml;
|
|
|
+ slides.Add(slide);
|
|
|
+ pageSize++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Pptx info = new Pptx { Slides = slides, Width = x, Height = y, PageSize = pageSize, Thumbnail = base64 };
|
|
|
+ return info;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public HiTeachCC.Model.PowerPoint.Slide GetSlideElement(SlidePart slidePart, Dictionary<string, string> colors) {
|
|
|
+ var shapeTrees = from shap in slidePart.Slide.Descendants<ShapeTree>() select shap;
|
|
|
+ if (shapeTrees.Count() > 0 && shapeTrees.First().ChildElements.Count > 0)
|
|
|
+ {
|
|
|
+ OpenXmlElementList openXmlElements = shapeTrees.First().ChildElements;
|
|
|
+ int index = 0;
|
|
|
+ foreach (OpenXmlElement element in openXmlElements)
|
|
|
+ {
|
|
|
+ ProcessNodesInSlide(element ,slidePart, colors, index);
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessNodesInSlide(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index ) {
|
|
|
+ if (element is DocumentFormat.OpenXml.Presentation.Shape sp)//p:sp
|
|
|
+ {
|
|
|
+ // pptElement = ShapeConvert(shape, theme, colorMap, index);
|
|
|
+ ProcessSpNode(sp, slidePart,colors, index);
|
|
|
+ }
|
|
|
+ else if (element is DocumentFormat.OpenXml.Presentation.Picture pic)//p:pic
|
|
|
+ {
|
|
|
+ ProcessPicNode(pic, slidePart, colors, index);
|
|
|
+ // pptElement = PictureConvert(picture, theme, colorMap, slidePart, index);
|
|
|
+ }
|
|
|
+ else if (element is DocumentFormat.OpenXml.AlternateContent mc)//mc:alternatecontent
|
|
|
+ {
|
|
|
+ ProcessMcNode(mc, slidePart, colors, index);
|
|
|
+ /// pptElement = AlternateContentConvert(content, theme, colorMap, slidePart, index);
|
|
|
+ }
|
|
|
+ else if(element is DocumentFormat.OpenXml.Presentation.GraphicFrame graphicFrame)//p:graphicFrame
|
|
|
+ {
|
|
|
+ ProcessGraphicFrameNode(graphicFrame, slidePart, colors, index);
|
|
|
+ /// pptElement = GraphicFrameConvert(graphicFrame, theme, colorMap, index);
|
|
|
+ }
|
|
|
+ else if (element is DocumentFormat.OpenXml.Presentation.GroupShape grpSp)//p:grpSp
|
|
|
+ {
|
|
|
+ ProcessGrpSpNode(grpSp, slidePart, colors, index);
|
|
|
+ /// pptElement = GroupShapeConvert(groupShape, theme, colorMap, index);
|
|
|
+ }
|
|
|
+ else if (element is DocumentFormat.OpenXml.Presentation.ConnectionShape cxnSp) // p:cxnSp
|
|
|
+ {
|
|
|
+ ProcessCxnSpNode(cxnSp, slidePart, colors, index);
|
|
|
+ /// pptElement = ConnectionShapeConvert(connectionShape, theme, colorMap, index);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessSpNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessPicNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessMcNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessGraphicFrameNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessGrpSpNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index)
|
|
|
+ {
|
|
|
+ var shapes = from shap in element.Descendants<DocumentFormat.OpenXml.Presentation.Shape>() select shap;
|
|
|
+ foreach (var shape in shapes) {
|
|
|
+ string s = ProcessNodesInSlide(shape, slidePart, colors, index);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public string ProcessCxnSpNode(OpenXmlElement element, SlidePart slidePart, Dictionary<string, string> colors, int index)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public Dictionary<string, string> DoColorMap(ColorMap colorMap , Theme theme) {
|
|
|
+ string[] colors = { "accent1", "accent2", "accent3", "accent4", "accent5", "accent6",
|
|
|
+ "lt1", "lt2", "tx1", "tx2", "dk1","dk2","hlink","folHlink" };
|
|
|
+ DocumentFormat.OpenXml.Drawing.ColorScheme colorScheme = theme.ThemeElements.ColorScheme;
|
|
|
+ Dictionary<string, string> pairs = new Dictionary<string, string>();
|
|
|
+ foreach (var col in colors) {
|
|
|
+ pairs.Add(col, PowerPointHelper.ColorForThemeClr(col, colorScheme, colorMap));
|
|
|
+ }
|
|
|
+ return pairs;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|