mgw854
mgw854

Reputation: 677

Generating PowerPoint files in C# with OpenXML SDK; Validates, but opens with errors

I've recently begun digging into the OpenXML SDK in hopes of automatically generating a PowerPoint presentation. I found the entire process painful, and have spent the better part of the weekend and the last two days generating a library to sit over the raw SDK calls. Everything seems to be working fine during the generation process, and I can validate the code as correct in the Open XML SDK Productivity Tool. Opening the file in PowerPoint, however, causes a dialog box to appear asking me to fix the file before opening it.

All of this code is refactored from what I reflected out of a file created in PowerPoint. Running a difference against those packages in the Productivity Tool, I find that my slide layouts (except for the first one) are in /ppt/slideLayouts/slideLayouts/, whereas the correct presentation has them in /ppt/slideLayouts/. I've triple checked my generating code, and it adds the SlideLayout to the PresentationPart, not a SlideLayoutPart. It's especially odd, because the first layout is in the correct location, and the subsequent ones are not.

I've read through the MSDN documentation and the Open XML e-book by Wouter van Vugt. I've compared my code against the reflected code, and it appears correct to me (although I could be missing a lot of things considering how new I am to Open XML and the complexity of PresentationML). Does anyone have any insight into what may be happening here?

Upvotes: 1

Views: 2120

Answers (1)

mgw854
mgw854

Reputation: 677

After much trial and error, I finally found the problem. For every slide in the presentation, I followed these steps:

  • Add SlidePart to PresentationPart
  • Add SlideLayoutPart to SlidePart
  • Add SlideMasterPart to SlideLayoutPart
  • Add ThemePart to SlideMasterPart
  • If this is the first time the slide master has been used, add circular references to the layouts

There must be a bug with the OpenXML SDK, because adding the SlideLayoutPart to the SlideMasterPart first causes the path issues I outlined in my question. This is why the first layout always worked correctly (it was added first to the slide). I've arranged the code now so that the first four bullets exist in one loop, then another loop runs through all the masters to find all the associated layouts, and only links the ones that are used.

No validation errors, and PowerPoint opens the file perfectly on the first try!

Upvotes: 2

Related Questions