Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

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

Open skrabbe001 opened this issue 1 year ago • 5 comments

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

Problem

I am trying to create a PowerPoint presentation in C# with Visual Studio using the Open XML SDK.

The presentation I create contains one slide with one table with 3 rows and 2 columns.

When I try to open the PowerPoint presentation created by the program, I am presented with the following error message:

PowerPoint found a problem with content in test3.pptx.
PowerPoint can attempt to repair the presentation.

If you trust the source of this presentation, click Repair.

When I click Repair, I am presented with another error message:

PowerPoint couldn't read some content in test3.pptx - Repaired and removed it.

Please check your presentation to see if the rest looks ok.

When I click Ok and check the contents of the presentation, it is empty.

QUESTION

How should I modify the program, so users can open the presentations without problems?

  • Am I using the wrong version of the Open XML SDK?
  • Is there something wrong with the code I've written?
  • Are there tools I can use to assist me in tracking down the error?
  • Other?

The version of PowerPoint I use to open the .pptx file

Microsoft® PowerPoint® for Microsoft 365 MSO (Version 2310 Build 16.0.16924.20054) 64-bit

My console project looks like this

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
  </ItemGroup>
</Project>

My main program looks like this

var builder = new OpenXMLBuilder.Test3();
builder.Doit(args);

My source code looks like this

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;

#region disable_warnings
// Remove unnecessary suppression
#pragma warning disable IDE0079
// Member 'x' does not access instance data and can be marked as static
#pragma warning disable CA1822
// Remove unused parameter 'x'
#pragma warning disable IDE0060
// 'using' statement can be simplified
#pragma warning disable IDE0063
// 'new' expression can be simplified
#pragma warning disable IDE0090
// Object initialization can be simplified
#pragma warning disable IDE0017
#endregion

namespace OpenXMLBuilder
{
    class Test3
    {
        public void Doit(string[] args)
        {
            string filepath = "test3.pptx";
            CreatePresentation(filepath);
        }

        public static void CreatePresentation(string filepath)
        {
            // Create a presentation at a specified file path.
            using (PresentationDocument presentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation))
            {
                PresentationPart presentationPart = presentationDocument.AddPresentationPart();
                presentationPart.Presentation = new Presentation();
                CreateSlide(presentationPart);
            }
        }

        private static void CreateSlide(PresentationPart presentationPart)
        {
            // Create the SlidePart.
            SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
            slidePart.Slide = new Slide(new CommonSlideData(new ShapeTree()));

            // Declare and instantiate the table
            D.Table table = new D.Table();

            // Define the columns
            D.TableGrid tableGrid = new D.TableGrid();
            tableGrid.Append(new D.GridColumn() { Width = 3124200 });
            tableGrid.Append(new D.GridColumn() { Width = 3124200 });

            // Append the TableGrid to the table
            table.Append(tableGrid);

            // Create the rows and cells
            for (int i = 0; i < 3; i++) // 3 rows
            {
                D.TableRow row = new D.TableRow() { Height = 370840 };
                for (int j = 0; j < 2; j++) // 2 columns
                {
                    D.TableCell cell = new D.TableCell();
                    D.TextBody body = new D.TextBody(new D.BodyProperties(),
                                                         new D.ListStyle(),
                                                         new D.Paragraph(new D.Run(new D.Text($"Cell {i + 1},{j + 1}"))));
                    cell.Append(body);
                    cell.Append(new D.TableCellProperties());
                    row.Append(cell);
                }
                table.Append(row);
            }

            // Create a GraphicFrame to hold the table
            GraphicFrame graphicFrame = new GraphicFrame();
            graphicFrame.NonVisualGraphicFrameProperties = new NonVisualGraphicFrameProperties(
                new NonVisualDrawingProperties() { Id = 1026, Name = "Table" },
                new NonVisualGraphicFrameDrawingProperties(),
                new ApplicationNonVisualDrawingProperties());
            graphicFrame.Transform = new Transform(new D.Offset() { X = 0L, Y = 0L }, new D.Extents() { Cx = 0L, Cy = 0L });
            graphicFrame.Graphic = new D.Graphic(new D.GraphicData(table)
            {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/table"
            });
            // Sanity check
            if (slidePart.Slide.CommonSlideData == null)
                throw new InvalidOperationException("CreateSlide: CommonSlideData is null");
            if (slidePart.Slide.CommonSlideData.ShapeTree == null)
                throw new InvalidOperationException("CreateSlide: ShapeTree is null");
            // Append the GraphicFrame to the SlidePart
            slidePart.Slide.CommonSlideData.ShapeTree.AppendChild(graphicFrame);
            // Save the slide part
            slidePart.Slide.Save();

            // Create slide master
            SlideMasterPart slideMasterPart = presentationPart.AddNewPart<SlideMasterPart>();
            slideMasterPart.SlideMaster = new SlideMaster(new CommonSlideData(new ShapeTree()));
            slideMasterPart.SlideMaster.Save();

            // Create slide layout
            SlideLayoutPart slideLayoutPart = slideMasterPart.AddNewPart<SlideLayoutPart>();
            slideLayoutPart.SlideLayout = new SlideLayout(new CommonSlideData(new ShapeTree()));
            slideLayoutPart.SlideLayout.Save();
            // Create unique id for the slide
            presentationPart.Presentation.SlideIdList = new SlideIdList(new SlideId()
            {
                Id = 256U,
                RelationshipId = presentationPart.GetIdOfPart(slidePart)
            });
            // Set the size
            presentationPart.Presentation.SlideSize = new SlideSize() { Cx = 9144000, Cy = 6858000 };

            // Save the presentation
            presentationPart.Presentation.Save();
        }
    } // class
} // namespace

Desktop Info

  • OS: Windows 10.0.19044
  • Office version: 2310 Build 16.0.16924.20054 64-bit
  • .NET Target: .NET 6.0
  • DocumentFormat.OpenXml Version: 2.20.0

skrabbe001 avatar Nov 10 '23 11:11 skrabbe001

Hi @skrabbe001,

  • Am I using the wrong version of the Open XML SDK?

    • No, you're using 2.20.0, which is the latest release
  • Is there something wrong with the code I've written?

    • I'm not sure specifically what's wrong with your implementation, but there is a snippets package for Visual Studio that has samples for creating PowerPoint presentation, Word documents, Excel spreadsheets, and others like inserting a cell to an Excel spreadsheet. Here's the link if you want to go straight to the create a PowerPoint presentation, here's the direct link.

    • If you have more how-to questions, the best place to post them is stackoverflow.com using the [openxml-sdk] tag. That will get you the most visibility for your questions to the community.

  • Are there tools I can use to assist me in tracking down the error?

  • Other?

    • Thanks for using the Open XML SDK 💯

mikeebowen avatar Nov 10 '23 16:11 mikeebowen

I tried using the snippets for creating a PowerPoint document with the OOXMLSnippets.

  • The document produced is now not corrupted, but the slide that is created is blank. How do I find out what was wrong with the previous version?
  • I tried inserting the simplest table (which is the only code added to the snippet) and the resulting presentation it is still blank.
  • I am having trouble finding good documentation and examples.
  • I haven't tried the Open XML SDK 2.5 Productivity Tool, because that is version 2.5 not 2.2, which you said was the official version to use.
  • I haven't tried the Open-Xml-PowerTools - it looks like an additional level of abstraction. What I want, before I do anything else, is to know if I can produce a "hello world" powerpoint presentation with the Open XML SDK, i.e. a presentation with 1 slide and a small table.

Here is the code that produces an empty presentation (which it shouldn't) created with the OOXMLSnippets, where I added a simple table.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
//using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;

#region disable_warnings
// Remove unnecessary suppression
#pragma warning disable IDE0079
// Member 'x' does not access instance data and can be marked as static
#pragma warning disable CA1822
// Remove unused parameter 'x'
#pragma warning disable IDE0060
// 'using' statement can be simplified
#pragma warning disable IDE0063
// 'new' expression can be simplified
#pragma warning disable IDE0090
// Object initialization can be simplified
#pragma warning disable IDE0017
#endregion

namespace OpenXMLBuilder
{
    class Test4
    {
        public void Doit(string[] args)
        {
            string filepath = "test4.pptx";
            using (var presentation = OpenXmlSdkUtils.PowerPointUtils.CreatePresentation(filepath))
            {
                presentation.Save();
            }
        }

    }
}

namespace OpenXmlSdkUtils
{
    public class PowerPointUtils
    {
        public static PresentationDocument CreatePresentation(string filepath)
        {
            // Create a presentation at a specified file path. The presentation document type is pptx by default.
            PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
            PresentationPart presentationPart = presentationDoc.AddPresentationPart();
            presentationPart.Presentation = new Presentation();

            CreatePresentationParts(presentationPart);

            return presentationDoc;
        }

        private static void CreatePresentationParts(PresentationPart presentationPart)
        {
            var SlideMasterId = "rId1";
            var SlideId = "rId2";
            var ThemeId = "rId5";

            SlideMasterIdList slideMasterIdList1 = new SlideMasterIdList(new SlideMasterId() { Id = (UInt32Value)2147483648U, RelationshipId = SlideMasterId });
            SlideIdList slideIdList1 = new SlideIdList(new SlideId() { Id = (UInt32Value)256U, RelationshipId = SlideId });
            SlideSize slideSize1 = new SlideSize() { Cx = 9144000, Cy = 6858000, Type = SlideSizeValues.Screen4x3 };
            NotesSize notesSize1 = new NotesSize() { Cx = 6858000, Cy = 9144000 };
            DefaultTextStyle defaultTextStyle1 = new DefaultTextStyle();

            presentationPart.Presentation.Append(slideMasterIdList1, slideIdList1, slideSize1, notesSize1, defaultTextStyle1);

            SlidePart slidePart1;
            SlideLayoutPart slideLayoutPart1;
            SlideMasterPart slideMasterPart1;
            ThemePart themePart1;

            slidePart1 = CreateSlidePart(presentationPart, SlideId);
            slideLayoutPart1 = CreateSlideLayoutPart(slidePart1, SlideMasterId);
            slideMasterPart1 = CreateSlideMasterPart(slideLayoutPart1, SlideMasterId);
            themePart1 = CreateTheme(slideMasterPart1, ThemeId);

            slideMasterPart1.AddPart(slideLayoutPart1, SlideMasterId);
            presentationPart.AddPart(slideMasterPart1, SlideMasterId);
            presentationPart.AddPart(themePart1, ThemeId);
        }

        private static GraphicFrame CreateTable()
        {
            // Declare and instantiate the table
            D.Table table = new D.Table();

            // Define the columns
            D.TableGrid tableGrid = new D.TableGrid();
            tableGrid.Append(new D.GridColumn() { Width = 3124200 });
            tableGrid.Append(new D.GridColumn() { Width = 3124200 });

            // Append the TableGrid to the table
            table.Append(tableGrid);

            // Create the rows and cells
            for (int i = 0; i < 3; i++) // 3 rows
            {
                D.TableRow row = new D.TableRow() { Height = 370840 };
                for (int j = 0; j < 2; j++) // 2 columns
                {
                    D.TableCell cell = new D.TableCell();
                    D.TextBody body = new D.TextBody(new D.BodyProperties(),
                                                         new D.ListStyle(),
                                                         new D.Paragraph(new D.Run(new D.Text($"Cell {i + 1},{j + 1}"))));
                    cell.Append(body);
                    cell.Append(new D.TableCellProperties());
                    row.Append(cell);
                }
                table.Append(row);
            }

            // Create a GraphicFrame to hold the table
            GraphicFrame frame = new GraphicFrame();
            frame.NonVisualGraphicFrameProperties = new NonVisualGraphicFrameProperties(
                new NonVisualDrawingProperties() { Id = 1026, Name = "Table" },
                new NonVisualGraphicFrameDrawingProperties(),
                new ApplicationNonVisualDrawingProperties());
            frame.Transform = new Transform(new D.Offset() { X = 0L, Y = 0L }, new D.Extents() { Cx = 0L, Cy = 0L });
            frame.Graphic = new D.Graphic(new D.GraphicData(table)
            {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/table"
            });

            return frame;
        }

        private static SlidePart CreateSlidePart(PresentationPart presentationPart, string SlideId)
        {
            SlidePart slidePart1 = presentationPart.AddNewPart<SlidePart>(SlideId);
            slidePart1.Slide = new Slide(
                    new CommonSlideData(
                        new ShapeTree(
                            new P.NonVisualGroupShapeProperties(
                                new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
                                new P.NonVisualGroupShapeDrawingProperties(),
                                new ApplicationNonVisualDrawingProperties()),
                            new GroupShapeProperties(new D.TransformGroup()),
                            new P.Shape(
                                new P.NonVisualShapeProperties(
                                    new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Title 1" },
                                    new P.NonVisualShapeDrawingProperties(new D.ShapeLocks() { NoGrouping = true }),
                                    new ApplicationNonVisualDrawingProperties(new PlaceholderShape())),
                                new P.ShapeProperties(),
                                new P.TextBody(
                                    new D.BodyProperties(),
                                    new D.ListStyle(),
                                    new D.Paragraph(new D.EndParagraphRunProperties() { Language = "en-US" }))))),
                    new ColorMapOverride(new D.MasterColorMapping()));


            // Sanity check
            if (slidePart1.Slide.CommonSlideData == null)
                throw new InvalidOperationException("CreateSlide: CommonSlideData is null");
            if (slidePart1.Slide.CommonSlideData.ShapeTree == null)
                throw new InvalidOperationException("CreateSlide: ShapeTree is null");
            // Append the GraphicFrame to the SlidePart
            var frame = CreateTable();
            slidePart1.Slide.CommonSlideData.ShapeTree.AppendChild(frame);
            // Save the slide part
            slidePart1.Slide.Save();

            return slidePart1;
        }

        private static SlideLayoutPart CreateSlideLayoutPart(SlidePart slidePart1, string SlideMasterId)
        {
            SlideLayoutPart slideLayoutPart1 = slidePart1.AddNewPart<SlideLayoutPart>(SlideMasterId);
            SlideLayout slideLayout = new SlideLayout(
            new CommonSlideData(new ShapeTree(
              new P.NonVisualGroupShapeProperties(
              new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
              new P.NonVisualGroupShapeDrawingProperties(),
              new ApplicationNonVisualDrawingProperties()),
              new GroupShapeProperties(new D.TransformGroup()),
              new P.Shape(
              new P.NonVisualShapeProperties(
                new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "" },
                new P.NonVisualShapeDrawingProperties(new D.ShapeLocks() { NoGrouping = true }),
                new ApplicationNonVisualDrawingProperties(new PlaceholderShape())),
              new P.ShapeProperties(),
              new P.TextBody(
                new D.BodyProperties(),
                new D.ListStyle(),
                new D.Paragraph(new D.EndParagraphRunProperties()))))),
            new ColorMapOverride(new D.MasterColorMapping()));
            slideLayoutPart1.SlideLayout = slideLayout;
            return slideLayoutPart1;
        }

        private static SlideMasterPart CreateSlideMasterPart(SlideLayoutPart slideLayoutPart1, string SlideMasterId)
        {
            SlideMasterPart slideMasterPart1 = slideLayoutPart1.AddNewPart<SlideMasterPart>(SlideMasterId);
            SlideMaster slideMaster = new SlideMaster(
            new CommonSlideData(new ShapeTree(
              new P.NonVisualGroupShapeProperties(
              new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
              new P.NonVisualGroupShapeDrawingProperties(),
              new ApplicationNonVisualDrawingProperties()),
              new GroupShapeProperties(new D.TransformGroup()),
              new P.Shape(
              new P.NonVisualShapeProperties(
                new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Title Placeholder 1" },
                new P.NonVisualShapeDrawingProperties(new D.ShapeLocks() { NoGrouping = true }),
                new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })),
              new P.ShapeProperties(),
              new P.TextBody(
                new D.BodyProperties(),
                new D.ListStyle(),
                new D.Paragraph())))),
            new P.ColorMap() { Background1 = D.ColorSchemeIndexValues.Light1, Text1 = D.ColorSchemeIndexValues.Dark1, Background2 = D.ColorSchemeIndexValues.Light2, Text2 = D.ColorSchemeIndexValues.Dark2, Accent1 = D.ColorSchemeIndexValues.Accent1, Accent2 = D.ColorSchemeIndexValues.Accent2, Accent3 = D.ColorSchemeIndexValues.Accent3, Accent4 = D.ColorSchemeIndexValues.Accent4, Accent5 = D.ColorSchemeIndexValues.Accent5, Accent6 = D.ColorSchemeIndexValues.Accent6, Hyperlink = D.ColorSchemeIndexValues.Hyperlink, FollowedHyperlink = D.ColorSchemeIndexValues.FollowedHyperlink },
            new SlideLayoutIdList(new SlideLayoutId() { Id = (UInt32Value)2147483649U, RelationshipId = SlideMasterId }),
            new TextStyles(new TitleStyle(), new BodyStyle(), new OtherStyle()));
            slideMasterPart1.SlideMaster = slideMaster;

            return slideMasterPart1;
        }

        private static ThemePart CreateTheme(SlideMasterPart slideMasterPart1, string ThemeId)
        {
            ThemePart themePart1 = slideMasterPart1.AddNewPart<ThemePart>(ThemeId);
            D.Theme theme1 = new D.Theme() { Name = "Office Theme" };

            D.ThemeElements themeElements1 = new D.ThemeElements(
            new D.ColorScheme(
              new D.Dark1Color(new D.SystemColor() { Val = D.SystemColorValues.WindowText, LastColor = "000000" }),
              new D.Light1Color(new D.SystemColor() { Val = D.SystemColorValues.Window, LastColor = "FFFFFF" }),
              new D.Dark2Color(new D.RgbColorModelHex() { Val = "1F497D" }),
              new D.Light2Color(new D.RgbColorModelHex() { Val = "EEECE1" }),
              new D.Accent1Color(new D.RgbColorModelHex() { Val = "4F81BD" }),
              new D.Accent2Color(new D.RgbColorModelHex() { Val = "C0504D" }),
              new D.Accent3Color(new D.RgbColorModelHex() { Val = "9BBB59" }),
              new D.Accent4Color(new D.RgbColorModelHex() { Val = "8064A2" }),
              new D.Accent5Color(new D.RgbColorModelHex() { Val = "4BACC6" }),
              new D.Accent6Color(new D.RgbColorModelHex() { Val = "F79646" }),
              new D.Hyperlink(new D.RgbColorModelHex() { Val = "0000FF" }),
              new D.FollowedHyperlinkColor(new D.RgbColorModelHex() { Val = "800080" }))
            { Name = "Office" },
              new D.FontScheme(
              new D.MajorFont(
              new D.LatinFont() { Typeface = "Calibri" },
              new D.EastAsianFont() { Typeface = "" },
              new D.ComplexScriptFont() { Typeface = "" }),
              new D.MinorFont(
              new D.LatinFont() { Typeface = "Calibri" },
              new D.EastAsianFont() { Typeface = "" },
              new D.ComplexScriptFont() { Typeface = "" }))
              { Name = "Office" },
              new D.FormatScheme(
              new D.FillStyleList(
              new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }),
              new D.GradientFill(
                new D.GradientStopList(
                new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 50000 },
                  new D.SaturationModulation() { Val = 300000 })
                { Val = D.SchemeColorValues.PhColor })
                { Position = 0 },
                new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 37000 },
                 new D.SaturationModulation() { Val = 300000 })
                { Val = D.SchemeColorValues.PhColor })
                { Position = 35000 },
                new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 15000 },
                 new D.SaturationModulation() { Val = 350000 })
                { Val = D.SchemeColorValues.PhColor })
                { Position = 100000 }
                ),
                new D.LinearGradientFill() { Angle = 16200000, Scaled = true }),
              new D.NoFill(),
              new D.PatternFill(),
              new D.GroupFill()),
              new D.LineStyleList(
              new D.Outline(
                new D.SolidFill(
                new D.SchemeColor(
                  new D.Shade() { Val = 95000 },
                  new D.SaturationModulation() { Val = 105000 })
                { Val = D.SchemeColorValues.PhColor }),
                new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
              {
                  Width = 9525,
                  CapType = D.LineCapValues.Flat,
                  CompoundLineType = D.CompoundLineValues.Single,
                  Alignment = D.PenAlignmentValues.Center
              },
              new D.Outline(
                new D.SolidFill(
                new D.SchemeColor(
                  new D.Shade() { Val = 95000 },
                  new D.SaturationModulation() { Val = 105000 })
                { Val = D.SchemeColorValues.PhColor }),
                new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
              {
                  Width = 9525,
                  CapType = D.LineCapValues.Flat,
                  CompoundLineType = D.CompoundLineValues.Single,
                  Alignment = D.PenAlignmentValues.Center
              },
              new D.Outline(
                new D.SolidFill(
                new D.SchemeColor(
                  new D.Shade() { Val = 95000 },
                  new D.SaturationModulation() { Val = 105000 })
                { Val = D.SchemeColorValues.PhColor }),
                new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
              {
                  Width = 9525,
                  CapType = D.LineCapValues.Flat,
                  CompoundLineType = D.CompoundLineValues.Single,
                  Alignment = D.PenAlignmentValues.Center
              }),
              new D.EffectStyleList(
              new D.EffectStyle(
                new D.EffectList(
                new D.OuterShadow(
                  new D.RgbColorModelHex(
                  new D.Alpha() { Val = 38000 })
                  { Val = "000000" })
                { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })),
              new D.EffectStyle(
                new D.EffectList(
                new D.OuterShadow(
                  new D.RgbColorModelHex(
                  new D.Alpha() { Val = 38000 })
                  { Val = "000000" })
                { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })),
              new D.EffectStyle(
                new D.EffectList(
                new D.OuterShadow(
                  new D.RgbColorModelHex(
                  new D.Alpha() { Val = 38000 })
                  { Val = "000000" })
                { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false }))),
              new D.BackgroundFillStyleList(
              new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }),
              new D.GradientFill(
                new D.GradientStopList(
                new D.GradientStop(
                  new D.SchemeColor(new D.Tint() { Val = 50000 },
                    new D.SaturationModulation() { Val = 300000 })
                  { Val = D.SchemeColorValues.PhColor })
                { Position = 0 },
                new D.GradientStop(
                  new D.SchemeColor(new D.Tint() { Val = 50000 },
                    new D.SaturationModulation() { Val = 300000 })
                  { Val = D.SchemeColorValues.PhColor })
                { Position = 0 },
                new D.GradientStop(
                  new D.SchemeColor(new D.Tint() { Val = 50000 },
                    new D.SaturationModulation() { Val = 300000 })
                  { Val = D.SchemeColorValues.PhColor })
                { Position = 0 }),
                new D.LinearGradientFill() { Angle = 16200000, Scaled = true }),
              new D.GradientFill(
                new D.GradientStopList(
                new D.GradientStop(
                  new D.SchemeColor(new D.Tint() { Val = 50000 },
                    new D.SaturationModulation() { Val = 300000 })
                  { Val = D.SchemeColorValues.PhColor })
                { Position = 0 },
                new D.GradientStop(
                  new D.SchemeColor(new D.Tint() { Val = 50000 },
                    new D.SaturationModulation() { Val = 300000 })
                  { Val = D.SchemeColorValues.PhColor })
                { Position = 0 }),
                new D.LinearGradientFill() { Angle = 16200000, Scaled = true })))
              { Name = "Office" });

            theme1.Append(themeElements1);
            theme1.Append(new D.ObjectDefaults());
            theme1.Append(new D.ExtraColorSchemeList());

            themePart1.Theme = theme1;
            return themePart1;
        }
    }
} // namespace

And here is the main program

var builder = new OpenXMLBuilder.Test4();
builder.Doit(args);

skrabbe001 avatar Nov 13 '23 14:11 skrabbe001

Hello, I'm having the same issue. Is there any update to this?

I've noted that in my case the Target is different between Powerpoint and OpenXML. OpenXML set the Target to /ppt/slides/slide2.xml and PowerPoint to slides/slide2.xml

presentation.rels.xml (OpenXML)

<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tags" Target="tags/tag1.xml" Id="rId3" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" Target="tableStyles.xml" Id="rId7" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml" Id="rId2" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" Target="slideMasters/slideMaster1.xml" Id="rId1" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml" Id="rId6" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps" Target="viewProps.xml" Id="rId5" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" Target="presProps.xml" Id="rId4" />
  <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="/ppt/slides/slide2.xml" Id="rId259" />
</Relationships>

presentation.rels.xml (after repaired with Powerpoint)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" Target="tableStyles.xml"/>
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide2.xml"/>
  <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/>
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" Target="slideMasters/slideMaster1.xml"/>
  <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps" Target="viewProps.xml"/>
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" Target="presProps.xml"/>
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tags" Target="tags/tag1.xml"/>
</Relationships>

faerbersteve avatar Dec 07 '23 14:12 faerbersteve

Hi @skrabbe001,

There are just a few small issues with your code. When you create the graphic frame, you need to be more specific from some classes as they could be DocumentFormat.OpenXml.Presentation or DocumentFormat.OpenXml.Drawing, so add the P prefix you created. Also, D.Offset.X and D.Offset.Y must be greater than 0 or the table is created outside of the visible area of the slide. It should look like this:

// Create a GraphicFrame to hold the table
P.GraphicFrame frame = new P.GraphicFrame();
frame.NonVisualGraphicFrameProperties = new P.NonVisualGraphicFrameProperties(
    new P.NonVisualDrawingProperties() { Id = 1026, Name = "Table" },
    new P.NonVisualGraphicFrameDrawingProperties(),
    new ApplicationNonVisualDrawingProperties());
frame.Transform = new Transform(new D.Offset() { X = 1, Y = 1 }, new D.Extents() { Cx = 0L, Cy = 0L });
frame.Graphic = new D.Graphic(new D.GraphicData(table)
{
    Uri = "http://schemas.openxmlformats.org/drawingml/2006/table"
});

And your CreateTable method needs to also prefix its return value with P. It should look like this:

private static P.GraphicFrame CreateTable()
{
    // Code...
}

Make these changes and it will add the table. I hope that helps!

mikeebowen avatar Feb 22 '24 21:02 mikeebowen

@faerbersteve,

The file path is a red herring. As long as the path is correct in the rels PowerPoint will interpret it correctly. See, my comment above for the issue with the table code. If you have other how-to questions, please post to the StackOverflow openxml-sdk tag or if you think there is an issue with the SDK itself, please create an issue.

mikeebowen avatar Feb 22 '24 21:02 mikeebowen

Closing this as resolved.

mikeebowen avatar May 08 '24 16:05 mikeebowen