Presentation created in C# with the Open XML SDK is corrupted and blank when opened

Vishal Pandey 41 Reputation points
2023-12-05T11:25:56.66+00:00
  private void AddSlideContent(SlidePart slidePart, string title, string text)
  {
      var shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;

      var titleShape = shapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape());
      var titleTextBody = titleShape.AppendChild(new DocumentFormat.OpenXml.Drawing.TextBody());
      var titleParagraph = titleTextBody.AppendChild(new DocumentFormat.OpenXml.Drawing.Paragraph());
      var titleRun = new DocumentFormat.OpenXml.Drawing.Run();
      titleRun.Append(new DocumentFormat.OpenXml.Drawing.Text(title));
      titleParagraph.Append(titleRun);

      var contentShape = shapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape());
      var contentTextBody = contentShape.AppendChild(new DocumentFormat.OpenXml.Drawing.TextBody());
      var contentParagraph = contentTextBody.AppendChild(new DocumentFormat.OpenXml.Drawing.Paragraph());
      var contentRun = new DocumentFormat.OpenXml.Drawing.Run();
      contentRun.Append(new DocumentFormat.OpenXml.Drawing.Text(text));
      contentParagraph.Append(contentRun);

      // Set additional properties and styles as needed
  }
  private void CreatePresentation(string content, PresentationDocument presentationDocument)
  {
      var presentationPart = presentationDocument.AddPresentationPart();
      presentationPart.Presentation = new Presentation();

      var slideContents = Regex.Split(content, @"Slide \d+:").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

      for (int i = 0; i < slideContents.Length; i++)
      {
          var slidePart = presentationPart.AddNewPart<SlidePart>("rId" + (i + 1));
          slidePart.Slide = new Slide(new CommonSlideData(new ShapeTree()));

          var slideContent = slideContents[i].Trim();
          var titleEndIndex = slideContent.IndexOf('-');
          var title = slideContent.Substring(0, titleEndIndex).Trim();
          var slideText = slideContent.Substring(titleEndIndex + 1).Trim();

          //AddSlideContent(slidePart, title, slideText);
          CreateSimpleSlide(slidePart);
      }

      presentationPart.Presentation.Save();
  }

In runtime contentParagraph.Append(contentRun); contentRun has text. but not going to ppt.
tried a simple method which also did not work.

private void CreateSimpleSlide(SlidePart slidePart)
{
    // Create a new shape tree for the slide
    var shapeTree = new DocumentFormat.OpenXml.Presentation.ShapeTree();

    // Create and append a new shape for the title
    var titleShape = shapeTree.AppendChild(new DocumentFormat.OpenXml.Presentation.Shape());
    var titleTextBody = titleShape.AppendChild(new DocumentFormat.OpenXml.Drawing.TextBody());
    var titleParagraph = titleTextBody.AppendChild(new DocumentFormat.OpenXml.Drawing.Paragraph());
    var titleRun = titleParagraph.AppendChild(new DocumentFormat.OpenXml.Drawing.Run());
    titleRun.AppendChild(new DocumentFormat.OpenXml.Drawing.Text("Slide 1"));

    // Set the slide's shape tree
    slidePart.Slide.CommonSlideData = new DocumentFormat.OpenXml.Presentation.CommonSlideData(shapeTree);

    // Save changes to the slide part
    slidePart.Slide.Save();
}
PowerPoint
PowerPoint
A family of Microsoft presentation graphics products that offer tools for creating presentations and adding graphic effects like multimedia objects and special effects with text.
251 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,626 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,709 questions
0 comments No comments
{count} votes