using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; using D = DocumentFormat.OpenXml.Drawing; using System.IO.Packaging; using System.IO; using TEAMModelOS.SDK.Helper.Common.JsonHelper; using ClearSlideLibrary.Dom; using OpenXmlPowerTools; using System.Drawing; using DocumentFormat.OpenXml.Office2010.Drawing; namespace TEAMModelOS.Test.PPTX { public class PPTXConvert { public static void GetSlideTitles(string presentationFile, string store) { // Open the presentation as read-only. using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false)) { GetSlideTitles(presentationDocument, store); } } public static void GetSlideTitles(PresentationDocument presentationDocument, string store) { if (presentationDocument == null) { throw new ArgumentNullException("presentationDocument"); } // Get a PresentationPart object from the PresentationDocument object. PresentationPart presentationPart = presentationDocument.PresentationPart; if (presentationPart != null && presentationPart.Presentation != null) { // Get a Presentation object from the PresentationPart object. Presentation presentation = presentationPart.Presentation; if (presentation.SlideIdList != null) { // Get the title of each slide in the slide order. foreach (var slideId in presentation.SlideIdList.Elements()) { Console.WriteLine(slideId.Id + ""); SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart; // Get the slide title. GetSlide(slidePart, store); // An empty title can also be added. } } } } // Get the title string of the slide. public static void GetSlide(SlidePart slidePart, string store) { if (slidePart == null) { throw new ArgumentNullException("presentationDocument"); } // Declare a paragraph separator. string titleSeparator = null; if (slidePart.Slide != null) { // 获取有文本内容的元素 var textsShapes = from shape in slidePart.Slide.Descendants() select shape; // 获取公式 var textMaths = from shape in slidePart.Slide.Descendants() select shape.Parent.Parent; // 获取图片的元素 var pictures = from shape in slidePart.Slide.Descendants() select shape; //获取预设的几何模型 var presetGeometry = from shape in slidePart.Slide.Descendants()where string.IsNullOrEmpty(shape.Parent.Parent.InnerText.Trim()) select shape.Parent.Parent; List a = new List(); foreach (var s in pictures) { BlipFill blipFill = s.BlipFill; var imageRid = blipFill.Blip.Embed.Value; IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault(); ImagePart imagePart = (ImagePart)idParie.OpenXmlPart; var contentType = imagePart.ContentType; using (var stream = imagePart.GetStream()) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); stream.Close(); string base64 = System.Convert.ToBase64String(buffer); base64 = "data:" + contentType + ";base64," + base64; a.Add(base64); } } StringBuilder titleText = new StringBuilder(); int index = 1; List pptShapes = new List(); foreach (var shape in pictures) { Console.WriteLine(index++); ShapeProperties properties = shape.ShapeProperties; PPTElement pptShape = null; if (properties != null && properties.Transform2D != null) { pptShape = new PPTElement { offx = properties.Transform2D.Offset.X, offy = properties.Transform2D.Offset.Y, extx = properties.Transform2D.Extents.Cx, exty = properties.Transform2D.Extents.Cy, }; } Console.WriteLine(shape.CloneNode(false).XName.LocalName); // pptShape.text = shape.TextBody; pptShapes.Add(pptShape); } foreach (var shape in textsShapes) { // Get the text in each paragraph in this shape. foreach (var paragraph in shape.TextBody.Descendants()) { // Add a line break. titleText.Append(titleSeparator); foreach (var text in paragraph.Descendants()) { titleText.Append(text.Text); } titleSeparator = "\n"; } } if (titleText.Length == 0) return; LinkedList texts = new LinkedList(); foreach (var paragraph in slidePart.Slide.Descendants()) { StringBuilder allText = new StringBuilder(); foreach (var text in paragraph.Descendants()) { allText.Append(text.Text); } if (allText.Length > 0) { if (allText.ToString() == titleText.ToString()) ; else texts.AddLast(allText.ToString()); } } if (texts.Count > 0) { System.IO.StreamWriter file = new System.IO.StreamWriter(store, true); file.Write("{\"Title\":\"" + titleText.ToString() + "\","); file.Write("\"Content\":\""); string inter = ""; foreach (var text in texts) { file.Write(inter + text); inter = ","; } file.WriteLine("\"}"); file.Close(); } } return; } // Determines whether the shape is a title shape. private static bool IsTitleShape(Shape shape) { var placeholderShape = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.GetFirstChild(); if (placeholderShape != null && placeholderShape.Type != null && placeholderShape.Type.HasValue) { switch ((PlaceholderValues)placeholderShape.Type) { // Any title shape. case PlaceholderValues.Title: return true; // A centered title. case PlaceholderValues.CenteredTitle: return true; default: return false; } } return false; } public static void FixPowerpoint(string fileName) { //Opening the package associated with file Console.WriteLine(fileName); using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { //Uri of the printer settings part var binPartUri = new Uri("/ppt/printerSettings/printerSettings1.bin", UriKind.Relative); if (wdPackage.PartExists(binPartUri)) { //Uri of the presentation part which contains the relationship var presPartUri = new Uri("/ppt/presentation.xml", UriKind.RelativeOrAbsolute); var presPart = wdPackage.GetPart(presPartUri); //Getting the relationship from the URI var presentationPartRels = presPart.GetRelationships().Where(a => a.RelationshipType.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); if (presentationPartRels != null) { //Delete the relationship presPart.DeleteRelationship(presentationPartRels.Id); } //Delete the part wdPackage.DeletePart(binPartUri); } wdPackage.Close(); } } } }