You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.3KB

  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml.Presentation;
  4. using A = DocumentFormat.OpenXml.Drawing;
  5. namespace Service;
  6. public class SlideData
  7. {
  8. public string Title { get; set; } = "";
  9. public string Content { get; set; }="";
  10. }
  11. public class PowerPointGenerator
  12. {
  13. public static void CreatePpt(string filePath, List<SlideData> slides)
  14. {
  15. if (File.Exists(filePath)) File.Delete(filePath);
  16. using (PresentationDocument presentationDoc =
  17. PresentationDocument.Create(filePath, PresentationDocumentType.Presentation))
  18. {
  19. // Partie principale de la présentation
  20. PresentationPart presentationPart = presentationDoc.AddPresentationPart();
  21. presentationPart.Presentation = new Presentation();
  22. // SlideMaster de base
  23. SlideMasterPart slideMasterPart = presentationPart.AddNewPart<SlideMasterPart>("rId1");
  24. slideMasterPart.SlideMaster = new SlideMaster(new CommonSlideData(new ShapeTree()));
  25. SlideLayoutPart layoutPart = slideMasterPart.AddNewPart<SlideLayoutPart>("rId2");
  26. layoutPart.SlideLayout = new SlideLayout(new CommonSlideData(new ShapeTree()));
  27. presentationPart.Presentation.SlideMasterIdList = new SlideMasterIdList(
  28. new SlideMasterId() { Id = 1U, RelationshipId = "rId1" });
  29. // Liste des slides
  30. SlideIdList slideIdList = new SlideIdList();
  31. presentationPart.Presentation.SlideIdList = slideIdList;
  32. uint slideId = 256;
  33. foreach (var slideData in slides)
  34. {
  35. SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
  36. slidePart.Slide = new Slide(new CommonSlideData(new ShapeTree()));
  37. var shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;
  38. // Titre
  39. var titleShape = CreateTextShape(1U, "Title", slideData.Title, 200, 100);
  40. shapeTree.Append(titleShape);
  41. // Contenu
  42. var contentShape = CreateTextShape(2U, "Content", slideData.Content, 200, 200);
  43. shapeTree.Append(contentShape);
  44. SlideId newSlideId = new SlideId()
  45. {
  46. Id = slideId++,
  47. RelationshipId = presentationPart.GetIdOfPart(slidePart)
  48. };
  49. slideIdList.Append(newSlideId);
  50. }
  51. presentationPart.Presentation.Save();
  52. }
  53. }
  54. private static Shape CreateTextShape(uint id, string name, string text, int x, int y)
  55. {
  56. return new Shape(
  57. new NonVisualShapeProperties(
  58. new NonVisualDrawingProperties() { Id = id, Name = name },
  59. new NonVisualShapeDrawingProperties(),
  60. new ApplicationNonVisualDrawingProperties()
  61. ),
  62. new ShapeProperties(
  63. new A.Transform2D(
  64. new A.Offset() { X = x * 9525, Y = y * 9525 }, // position en EMUs
  65. new A.Extents() { Cx = 6000000, Cy = 2000000 } // taille approximative
  66. )
  67. ),
  68. new TextBody(
  69. new A.BodyProperties(),
  70. new A.ListStyle(),
  71. new A.Paragraph(new A.Run(new A.Text(text)))
  72. )
  73. );
  74. }
  75. }