PPTXConvert.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DocumentFormat.OpenXml.Packaging;
  7. using DocumentFormat.OpenXml.Presentation;
  8. using D = DocumentFormat.OpenXml.Drawing;
  9. using System.IO.Packaging;
  10. using System.IO;
  11. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  12. using ClearSlideLibrary.Dom;
  13. using OpenXmlPowerTools;
  14. using System.Drawing;
  15. using DocumentFormat.OpenXml.Office2010.Drawing;
  16. namespace TEAMModelOS.Test.PPTX
  17. {
  18. public class PPTXConvert
  19. {
  20. public static void GetSlideTitles(string presentationFile, string store)
  21. {
  22. // Open the presentation as read-only.
  23. using (PresentationDocument presentationDocument =
  24. PresentationDocument.Open(presentationFile, false))
  25. {
  26. GetSlideTitles(presentationDocument, store);
  27. }
  28. }
  29. public static void GetSlideTitles(PresentationDocument presentationDocument, string store)
  30. {
  31. if (presentationDocument == null)
  32. {
  33. throw new ArgumentNullException("presentationDocument");
  34. }
  35. // Get a PresentationPart object from the PresentationDocument object.
  36. PresentationPart presentationPart = presentationDocument.PresentationPart;
  37. if (presentationPart != null &&
  38. presentationPart.Presentation != null)
  39. {
  40. // Get a Presentation object from the PresentationPart object.
  41. Presentation presentation = presentationPart.Presentation;
  42. if (presentation.SlideIdList != null)
  43. {
  44. // Get the title of each slide in the slide order.
  45. foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
  46. {
  47. Console.WriteLine(slideId.Id + "");
  48. SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
  49. // Get the slide title.
  50. GetSlide(slidePart, store);
  51. // An empty title can also be added.
  52. }
  53. }
  54. }
  55. }
  56. // Get the title string of the slide.
  57. public static void GetSlide(SlidePart slidePart, string store)
  58. {
  59. if (slidePart == null)
  60. {
  61. throw new ArgumentNullException("presentationDocument");
  62. }
  63. // Declare a paragraph separator.
  64. string titleSeparator = null;
  65. if (slidePart.Slide != null)
  66. {
  67. // 获取有文本内容的元素
  68. var textsShapes = from shape in slidePart.Slide.Descendants<Shape>() select shape;
  69. // 获取公式
  70. var textMaths = from shape in slidePart.Slide.Descendants<TextMath>() select shape.Parent.Parent;
  71. // 获取图片的元素
  72. var pictures = from shape in slidePart.Slide.Descendants<Picture>() select shape;
  73. //获取预设的几何模型
  74. var presetGeometry = from shape in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.PresetGeometry>()where string.IsNullOrEmpty(shape.Parent.Parent.InnerText.Trim()) select shape.Parent.Parent;
  75. List<string> a = new List<string>();
  76. foreach (var s in pictures)
  77. {
  78. BlipFill blipFill = s.BlipFill;
  79. var imageRid = blipFill.Blip.Embed.Value;
  80. IdPartPair idParie = slidePart.Parts.Where(x => x.RelationshipId == imageRid).FirstOrDefault();
  81. ImagePart imagePart = (ImagePart)idParie.OpenXmlPart;
  82. var contentType = imagePart.ContentType;
  83. using (var stream = imagePart.GetStream())
  84. {
  85. byte[] buffer = new byte[stream.Length];
  86. stream.Read(buffer, 0, buffer.Length);
  87. stream.Close();
  88. string base64 = System.Convert.ToBase64String(buffer);
  89. base64 = "data:" + contentType + ";base64," + base64;
  90. a.Add(base64);
  91. }
  92. }
  93. StringBuilder titleText = new StringBuilder();
  94. int index = 1;
  95. List<PPTElement> pptShapes = new List<PPTElement>();
  96. foreach (var shape in pictures)
  97. {
  98. Console.WriteLine(index++);
  99. ShapeProperties properties = shape.ShapeProperties;
  100. PPTElement pptShape = null;
  101. if (properties != null && properties.Transform2D != null)
  102. {
  103. pptShape = new PPTElement
  104. {
  105. offx = properties.Transform2D.Offset.X,
  106. offy = properties.Transform2D.Offset.Y,
  107. extx = properties.Transform2D.Extents.Cx,
  108. exty = properties.Transform2D.Extents.Cy,
  109. };
  110. }
  111. Console.WriteLine(shape.CloneNode(false).XName.LocalName);
  112. // pptShape.text = shape.TextBody;
  113. pptShapes.Add(pptShape);
  114. }
  115. foreach (var shape in textsShapes)
  116. {
  117. // Get the text in each paragraph in this shape.
  118. foreach (var paragraph in shape.TextBody.Descendants<D.Paragraph>())
  119. {
  120. // Add a line break.
  121. titleText.Append(titleSeparator);
  122. foreach (var text in paragraph.Descendants<D.Text>())
  123. {
  124. titleText.Append(text.Text);
  125. }
  126. titleSeparator = "\n";
  127. }
  128. }
  129. if (titleText.Length == 0)
  130. return;
  131. LinkedList<string> texts = new LinkedList<string>();
  132. foreach (var paragraph in slidePart.Slide.Descendants<D.Paragraph>())
  133. {
  134. StringBuilder allText = new StringBuilder();
  135. foreach (var text in paragraph.Descendants<D.Text>())
  136. {
  137. allText.Append(text.Text);
  138. }
  139. if (allText.Length > 0)
  140. {
  141. if (allText.ToString() == titleText.ToString()) ;
  142. else texts.AddLast(allText.ToString());
  143. }
  144. }
  145. if (texts.Count > 0)
  146. {
  147. System.IO.StreamWriter file = new System.IO.StreamWriter(store, true);
  148. file.Write("{\"Title\":\"" + titleText.ToString() + "\",");
  149. file.Write("\"Content\":\"");
  150. string inter = "";
  151. foreach (var text in texts)
  152. {
  153. file.Write(inter + text);
  154. inter = ",";
  155. }
  156. file.WriteLine("\"}");
  157. file.Close();
  158. }
  159. }
  160. return;
  161. }
  162. // Determines whether the shape is a title shape.
  163. private static bool IsTitleShape(Shape shape)
  164. {
  165. var placeholderShape = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.GetFirstChild<PlaceholderShape>();
  166. if (placeholderShape != null && placeholderShape.Type != null && placeholderShape.Type.HasValue)
  167. {
  168. switch ((PlaceholderValues)placeholderShape.Type)
  169. {
  170. // Any title shape.
  171. case PlaceholderValues.Title:
  172. return true;
  173. // A centered title.
  174. case PlaceholderValues.CenteredTitle:
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. return false;
  181. }
  182. public static void FixPowerpoint(string fileName)
  183. {
  184. //Opening the package associated with file
  185. Console.WriteLine(fileName);
  186. using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
  187. {
  188. //Uri of the printer settings part
  189. var binPartUri = new Uri("/ppt/printerSettings/printerSettings1.bin", UriKind.Relative);
  190. if (wdPackage.PartExists(binPartUri))
  191. {
  192. //Uri of the presentation part which contains the relationship
  193. var presPartUri = new Uri("/ppt/presentation.xml", UriKind.RelativeOrAbsolute);
  194. var presPart = wdPackage.GetPart(presPartUri);
  195. //Getting the relationship from the URI
  196. var presentationPartRels =
  197. presPart.GetRelationships().Where(a => a.RelationshipType.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
  198. StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
  199. if (presentationPartRels != null)
  200. {
  201. //Delete the relationship
  202. presPart.DeleteRelationship(presentationPartRels.Id);
  203. }
  204. //Delete the part
  205. wdPackage.DeletePart(binPartUri);
  206. }
  207. wdPackage.Close();
  208. }
  209. }
  210. }
  211. }