123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- 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<SlideId>())
- {
- 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<Shape>() select shape;
- // 获取公式
- var textMaths = from shape in slidePart.Slide.Descendants<TextMath>() select shape.Parent.Parent;
- // 获取图片的元素
- var pictures = from shape in slidePart.Slide.Descendants<Picture>() select shape;
- //获取预设的几何模型
- var presetGeometry = from shape in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>()where string.IsNullOrEmpty(shape.Parent.Parent.InnerText.Trim()) select shape.Parent.Parent;
- List<string> a = new List<string>();
- 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<PPTElement> pptShapes = new List<PPTElement>();
- 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<D.Paragraph>())
- {
- // Add a line break.
- titleText.Append(titleSeparator);
- foreach (var text in paragraph.Descendants<D.Text>())
- {
- titleText.Append(text.Text);
- }
- titleSeparator = "\n";
- }
- }
- if (titleText.Length == 0)
- return;
- LinkedList<string> texts = new LinkedList<string>();
- foreach (var paragraph in slidePart.Slide.Descendants<D.Paragraph>())
- {
- StringBuilder allText = new StringBuilder();
- foreach (var text in paragraph.Descendants<D.Text>())
- {
- 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<PlaceholderShape>();
- 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();
- }
- }
- }
- }
|