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: ...

2018-08-08 · 6 min · 1156 words · ayumax

Parsing Markdown and Performing Custom Output in C# (Using markdig)

Introduction I recently created a tool to convert Markdown files into PowerPoint format (*.pptx). I’ll summarize the technical notes from that process here. This article focuses on Markdown parsing. The created tool is available on GitHub. https://github.com/ayumax/MDToPPTX Also, for the article about the pptx creation tool itself, please refer to this article. Development Environment Considering future integration into a tool using Xamarin, I created the project as a .NET Standard 2.0 C# library. The overall environment is as follows: Windows 10 Visual Studio 2017 C# .NET Standard 2.0 Class library How to Parse Markdown Initially, I considered parsing it myself using regular expressions or something similar. However, the Markdown format has surprisingly many variations, so I searched for a convenient library and found the following. markdig GitHub: lunet-io/markdig This library is available on GitHub under the BSD-Clause 2 license and can be obtained via NuGet. If you just want to convert Markdown to HTML, you can use it as is. It’s also designed so that you can create your own output part for formats other than HTML. Standard Usage of markdig To convert Markdown to HTML, only the following description is needed (excerpt from GitHub’s README.md): var result = Markdown.ToHtml("This is a text with some *emphasis*"); Console.WriteLine(result); // prints: <p>This is a text with some <em>emphasis</em></p> Using markdig as a Parser My goal this time was to parse Markdown and output a PowerPoint file, so I investigated whether I could use only markdig’s parser functionality and create custom output. How markdig’s Processing Works To find out how to perform custom output using markdig, I examined how the preset HtmlRenderer is implemented. The diagram below simplifies this. Tracing the inheritance of HtmlRenderer leads to RendererBase, which defines the Render() method used during conversion and the ObjectRenderers property for handling Markdown blocks and inlines. ...

2018-08-07 · 5 min · 995 words · ayumax

MDToPPTX Image Handling

Long Time No Update It’s been quite a while since I last wrote an article about MDToPPTX. I’m scheduled to give a talk about creating this tool for my first-ever LT (Lightning Talk), so I’m reviewing the code again. GitHub: ayumax/MDToPPTX Image Implementation At the time of the previous article, I had tentatively implemented images assuming 1px to 1mm, but… the implementation was strange and incorrect. With that, the width of a 1000px image was being converted to 1cm. (The physical width (cm) was set by dividing the pixel count by 1000). ...

2018-08-05 · 2 min · 257 words · ayumax

Created a Tool to Convert Markdown to PowerPoint Files

MDToPPTX The beta version of MDToPPTX, a tool I’ve been creating to convert Markdown files to PowerPoint files (*.pptx), is now ready. It’s written in C#, and the library part is built with .NET Standard 2.0. So, although I haven’t tested it, it should also work with .NET Core and Xamarin. GitHub: ayuma0913/MDToPPTX Using this, you can create PowerPoint files from Markdown. markdig For Markdown parsing, I used markdig. Thanks to this, writing the code was very easy. ...

2018-06-06 · 3 min · 480 words · ayumax

PPTX Creation Feature: Image Insertion and Bullet Points

Issues from the Previous Version In the previous article (http://ayumax.hatenablog.com/entry/2018/05/06/231645), there were the following issues: Cannot freely change the position of text No image insertion feature All text becomes bullet points So, this time I addressed these issues. GitHub: ayuma0913/MDToPPTX Text Position Setting Created a new PPTXTextArea class, allowing Position(x,y) and Size(width, height) to be set. In the PowerPoint world, settings seem to be in cm, so I made it possible to input values in cm (internal conversion is performed). ...

2018-05-13 · 2 min · 352 words · ayumax

PPTX Creation Feature - Tentative Version

Progress Regarding the development of the C# library that reads Markdown and creates PowerPoint files, I’ve managed to implement the minimum required features for now. GitHub: ayuma0913/MDToPPTX The current features are as follows: Slide size is fixed at 4x3 (I want to support 16:9 as well) The first slide is always a title page Slides from the second one onwards can be added freely It's possible to specify the slide layout for added slides Only text can be added to slides; images and shapes are not supported Writing code like the following creates a *.pptx file. ...

2018-05-06 · 2 min · 273 words · ayumax

I Want to Create PowerPoint Files from Markdown

I Want to Create PPTX Files Lately, I have too many things I want to do and I’m working on various projects in parallel. Among them, for some reason, the desire to create PowerPoint documents from Markdown is strong, and I’m putting the most effort into it. First, I Looked for Existing Tools I haven’t searched for super niche tools, but it feels like there isn’t anything I want to use right away. There are many tools that can convert to PDF, but few for PowerPoint, right? ...

2018-05-05 · 2 min · 370 words · ayumax