简体   繁体   English

使用OpenXML SDK在C#中生成PowerPoint文件; 验证,但会打开但有错误

[英]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. 我最近开始深入研究OpenXML SDK,希望自动生成PowerPoint演示文稿。 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. 我发现整个过程都很痛苦,并且花了大半个周末和最后两天生成一个库来讨论原始的SDK调用。 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. 在生成过程中,一切似乎都运行正常,我可以在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. 但是,在PowerPoint中打开文件会导致出现一个对话框,要求我在打开文件之前修复该文件。

All of this code is refactored from what I reflected out of a file created in PowerPoint. 所有这些代码都是从我在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/ . 在Productivity Tool中对这些包运行差异,我发现我的幻灯片布局(第一个除外)位于/ppt/slideLayouts/slideLayouts/ ,而正确的表示将它们放在/ppt/slideLayouts/ I've triple checked my generating code, and it adds the SlideLayout to the PresentationPart , not a SlideLayoutPart . 我已经对生成代码进行了三重检查,并将SlideLayout添加到PresentationPart ,而不是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. 我已经阅读了Wouter van Vugt撰写的MSDN文档和Open XML电子书。 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). 我将我的代码与反映的代码进行了比较,看起来对我来说是正确的(尽管考虑到我对Open XML的新方式和PresentationML的复杂性,我可能会遗漏很多东西)。 Does anyone have any insight into what may be happening here? 有没有人对这里可能发生的事情有任何见解?

After much trial and error, I finally found the problem. 经过多次反复试验,我终于找到了问题所在。 For every slide in the presentation, I followed these steps: 对于演示文稿中的每张幻灯片,我都遵循以下步骤:

  • Add SlidePart to PresentationPart SlidePart添加到PresentationPart
  • Add SlideLayoutPart to SlidePart SlideLayoutPart添加到SlidePart
  • Add SlideMasterPart to SlideLayoutPart SlideMasterPart添加到SlideLayoutPart
  • Add ThemePart to SlideMasterPart ThemePart添加到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. OpenXML SDK必定存在错误,因为首先将SlideLayoutPart添加到SlideMasterPart会导致我在问题中概述的路径问题。 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! 没有验证错误,PowerPoint第一次尝试就完美地打开了文件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM