Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

Creating OpenXml files with the OpenXml SDK

Posted on 27 May 2023

Some time ago I wrote a blog post on Creating OpenXml files with Delphi. It showed how to open and create Word files using Delphi by using only a Zip and an Xml component.

There, I showed the structure of an OpenXml file: a zip file with a folder structure and a bunch of xml files:

In this blog post, we will explore the process of opening, inspecting, and creating OpenXml files using Delphi. We'll also discuss the benefits of using the OpenXml SDK for high-level file manipulation.

Inspecting an OpenXml file

To open this kind of file in C#, we can use the Packaging API. This API allows us to access and analyze the file's parts without directly dealing with the complexities of zips and xmls. As seen in the example we can get the parts of the file with this code:

using System.IO.Packaging;

using var package = Package.Open(fileName, FileMode.Open, FileAccess.Read);
package.GetParts().Select(p => p.ContentType).OrderBy(p => p).ToList().ForEach(p => Console.WriteLine(p));
C#

You have to add the package System.IO.Packaging to the project. Once you do that, you will be able to retrieve and display the parts of the OpenXml file. The output will provide insights into the file's structure, such as the main part being "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml.", which indicates we are analyzing a Word file.

To retrieve the relationships within the file, you can use the code snippet below:

using System.IO.Packaging;

using var package = Package.Open(fileName, FileMode.Open, FileAccess.Read);
package.GetRelationships().ToList()
    .ForEach(r => Console.WriteLine(
        $"{r.Id} - {r.SourceUri} - {r.TargetUri} - {r.TargetMode} - {r.RelationshipType}")); 
C#

Additionally, you can obtain the main and properties files using the code below:

using System.IO.Packaging;

using var package = Package.Open(fileName, FileMode.Open, FileAccess.Read);
package.GetParts().Where(p => p.ContentType.Contains("main+xml")).ToList()
    .ForEach(p => Console.WriteLine(p.Uri));
package.GetParts().Where(p => p.ContentType.Contains("core-properties")).ToList()
    .ForEach(p => Console.WriteLine(p.Uri));
package.GetParts().Where(p => p.ContentType.Contains("extended-properties")).ToList()
    .ForEach(p => Console.WriteLine(p.Uri));
C#

Once we have done that, we can open the file and get its contents, such as core properties and extended properties.

using System.IO.Packaging;
using System.Xml;

using var package = Package.Open(fileName, FileMode.Open, FileAccess.Read);
package.GetParts().Where(p => p.ContentType.Contains("core-properties")).ToList()
    .ForEach(p => WriteXmlToConsole(package.GetPart(p.Uri).GetStream());
package.GetParts().Where(p => p.ContentType.Contains("extended-properties")).ToList()
    .ForEach(p => WriteXmlToConsole(package.GetPart(p.Uri).GetStream()));


// Write formatted XML to console
void WriteXmlToConsole(Stream stream)
{
    var doc = new XmlDocument();
    doc.Load(stream);
    var settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "  ",
        NewLineChars = "\r\n",
        NewLineHandling = NewLineHandling.Replace
    };
    using var writer = XmlWriter.Create(Console.OpenStandardOutput(), settings);
    doc.Save(writer);
}
C#

Creating an OpenXml file

Instead of using the low-level Packaging API, we can leverage the OpenXml SDK, which provides a higher-level API for OpenXml file manipulation. You can find the source code and documentation at https://github.com/OfficeDev/Open-XML-SDK.

With the OpenXML SDK, you don't need to manipulate packages, relationships, or properties directly. Instead, you have new classes that allow you to work with Office files directly. For example, you have WordprocessingDocument, SpreadsheetDocument, and PresentationDocument classes for working with documents, spreadsheets, or presentations. The following code snippet creates a Word file with a text sentence:

using System.Xml;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

void CreateDoc(string filepath, string message)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        MainDocumentPart mainPart = doc.AddMainDocumentPart();

        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text(message));
        para.AppendChild(new Run());
    }
}
C#

You need to add the DocumentFormat.OpenXml package to the project.

If you call it using CreateDoc("hello.docx","Hello World!"); you will get

Adding more information to the file

While the previous example created a simple file, you can extend the code to include more complex information. As an illustration, let's create a file that lists all the fonts installed on the system:

void CreateDocWithAllFonts(string filepath)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        MainDocumentPart mainPart = doc.AddMainDocumentPart();

        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());

        // Get all fonts available in the system
        var fonts = System.Drawing.FontFamily.Families.Select(f => f.Name).ToList();

        foreach (var font in fonts)
        {
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text(font));
            run.RunProperties = new RunProperties(new RunFonts() { Ascii = font });
            para.AppendChild(new Run());
        }
    }
}
C#

To execute this code, you'll need to add the System.Drawing.Common package to your project. The resulting file will contain a list of all the installed fonts.

Conclusions

As you can see, manipulating OpenXml files in C# is made easier with the OpenXml SDK. By using the provided classes, you can work directly with the file's data without worrying about its internal structure. However, if you require more granular control, you can still utilize the low-level Packaging API to work with the raw data. This flexibility enables effortless creation and modification of OpenXml files while maintaining the ability to handle specific details when needed.

All the source code for this project is at https://github.com/bsonnino/OpenXmlCSharp

2 thoughts on “Creating OpenXml files with the OpenXml SDK”

  1. mArina says:
    26 July 2023 at 23:27

    Thanks, Bruno!

    Great tutorial, and exactly what I need right now.

    BTW, why does everyone only show how to CREATE, but no one shows how to READ docx-files?

    For example, I also need to read out all the paragraphs in a loop without losing their Range – how can I do this without OLE?

    Reply
    1. sonnino@gmail.com says:
      27 July 2023 at 08:33

      That’s very easy: in the first part of the article, I showed how to open a docx file. You can open the document.xml file and read it – it will have all the paragraphs in the document and you will be able to do what you want with them.
      I would recommend you to open a word document with a zip manager and the open the document.xml file and take a look at its contents

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About