Outputting PowerPoint Files (*.pptx) Using OpenXML SDK in C#
Introduction This article is a continuation of Parsing Markdown and Performing Custom Output in C# (Using markdig). The tool mentioned in the above article, which converts Markdown to PPTX, creates PPTX files using the OpenXML SDK. I initially struggled greatly with using this OpenXML SDK, so this article will focus on that part. The tool that converts Markdown to PPTX is available on GitHub. GitHub: ayumax/MDToPPTX What is OpenXML? It’s a file format used since Office 2007, consisting of XML files bundled into a zip archive. Therefore, by changing the file extension to .zip and then unzipping it, you can view the XML files inside. Unlike the previous binary format, it allows creation even in environments where Office is not installed. Development Environment Here’s the setup: Windows 10 Visual Studio 2017 C# .NET Standard 2.0 Class library About OpenXML SDK I referred to the following official website for the OpenXML description method: https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk However, understanding it solely from the explanations on the above site likely takes considerable time. Therefore, if you plan to use the OpenXML SDK from now on, I recommend the following method. Open XML SDK Productivity Tool https://www.microsoft.com/en-us/download/details.aspx?id=30425 Using this tool, you can inspect the internal structure of a specified OpenXML format file (like xlsx or pptx). What’s also helpful for tool creation is the feature that displays the necessary C# code to create the loaded file in the right-hand pane. So, by creating a file with the content you want to implement, loading it, and examining the C# code, the possibility of creating something functional, even without 100% understanding, increases significantly. I used this to find the code I needed by looking at small differences. Example: How to find out how to make text bold Place only one text element in a newly created pptx file and check the C# code. Copy the above pptx file, make the text part Bold, and check the C# code. Compare 1. and 2. Explanation of PowerPoint’s OpenXML Class Structure Basically, using the method described above with the Open XML SDK Productivity Tool allows you to create the functionality you want, so the discussion could end here. However, I will write down the class structure I investigated below, partly as a personal memo. The class structure of OpenXML files is quite large, so I’m focusing only on the important parts. Overall Structure First, the overall structure. To create a PPTX, you’ll mostly interact with SlidePart and SlideMasterPart at the bottom of the diagram. The division of responsibilities is roughly: SlideMasterPart for overall slide settings (like slide size), and SlidePart for individual slides. ThemePart is manipulated when changing the visual design. Although not shown in the diagram, if you use slide layouts, you’ll also touch SlideLayout. (The feature I created uses the Blank style sheet for all slides except the first title sheet, so I haven’t touched slide layouts.) Slide Next is the slide. Since it corresponds to a single slide in PowerPoint, the number of SlidePart class instances equals the number of pages. The diagram above extracts only the classes that become important when implementing the following features: ...