Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

OfficeIMO – Microsoft Word .NET Library

OfficeIMO - Microsoft Word .NET Library

OfficeIMO is available as nuget from the the gallery and it's preferred way of using it.

nuget downloads nuget version license nuget downloads

If you would like to contact me you can do so via Twitter or LinkedIn.

twitter blog linked

What it's all about

This is a small project (under development) that allows to create Microsoft Word documents (.docx) using .NET. Underneath it uses OpenXML SDK but heavily simplifies it. It was created because working with OpenXML is way too hard for me, and time consuming. I originally created it for using within PowerShell module called PSWriteOffice, but thought it may be useful for others to use in the .NET community.

I used to use DocX library (which I co-authored, before it was taken over by Xceed) to create Microsoft Word documents, but it only supports .NET Framework, and their newest community license makes the project unusuable.

As I am not really a developer, and I hardly know what I'm doing if you know how to help out - please do.

  • If you see bad practice, please open and issue/submit PR.
  • If you know how to do something in OpenXML that could help this project - please open an issue/submit PR
  • If you see something that could work better - please open and issue/submit PR
  • If you see something that I totally made a fool of myself - please open an issue/submit PR
  • If you see something that works not the way I think it works - please open an issue/submit PR

I hope you get the drift? If it's bad - open an issue/fix it! I don't know what I'm doing! The main thing is - it has to work with .NET Framework 4.7.2, .NET Standard 2.0 and so on.

This project is under development and as such there's a lot of things that can and will change, especially if some people help out.

Platform Status Code Coverage .NET
Windows .NET 4.7.2, NET 4.8, .NET 5.0, .NET 6.0, .NET Standard 2.0, .NET Standard 2.1
Linux .NET 5.0, .NET 6.0, .NET Standard 2.0, .NET Standard 2.1, .NET Core 3.1
MacOs .NET 5.0, .NET 6.0, .NET Standard 2.0, .NET Standard 2.1, .NET Core 3.1

Features

Here's a list of features currently supported (and probably a lot I forgot) and those that are planned. It's not a closed list, more of TODO, and I'm sure there's more:

  • ☑️ Word basics
    • ☑️ Create
    • ☑️ Load
    • ☑️ Save (autoopen on save as an option)
    • ◼️ SaveAs (not working correcly in edge cases)
  • ☑️ Word properties
    • ☑️ Reading basic and custom properties
    • ☑️ Setting basic and custom properties
  • ☑️ Sections
    • ☑️ Add Paragraphs
    • ☑️ Add Headers and Footers (Odd/Even/First)
    • ◼️ Remove Headers and Footers (Odd/Even/First)
    • ◼️ Remove Paragraphs
    • ◼️ Remove Sections
  • ☑️ Headers and Footers in document (not including sections)
    • ☑️ Add Default, Odd, Even, First
    • ◼️ Remove Default, Odd, Even, First
  • ☑️ Paragraphs/Text and make it bold, underlined, colored and so on
  • ☑️ Paragraphs and change alignment
  • ☑️ Tables
    • ☑️ Add rows and columns
    • ☑️ Add cells
    • ☑️ Add cell properties
    • ☑️ Remove rows
    • ☑️ Remove cells
    • ☑️ Others
      • ☑️ Merge cells (vertically, horizontally)
      • ◼️ Split cells (vertically)
      • ☑️ Split cells (horizontally)
  • ☑️ Images/Pictures (limited support - jpg only / inline type only)
    • ☑️ Add images from file to Word
    • ☑️ Save image from Word to File
    • ◼️ Other image types
    • ◼️ Other location types
  • ☑️ Hyperlinks
    • ◼️ Add HyperLink
    • ☑️ Read HyperLink
    • ◼️ Remove HyperLink
    • ☑️ Change HyperLink
  • ☑️ PageBreaks
    • ☑️ Add PageBreak
    • ☑️ Read PageBreak
    • ☑️ Remove PageBreak
    • ☑️ Change PageBreak
  • ☑️ Bookmarks
    • ☑️ Add Bookmark
    • ☑️ Read Bookmark
    • ☑️ Remove Bookmark
    • ☑️ Change Bookmark
  • ◼️ Comments
    • ☑️ Add comments
    • ☑️ Read comments
    • ◼️ Remove comments
    • ◼️ Track comments
  • ☑️ Fields
    • ◼️ Add Field
    • ☑️ Read Field
    • ◼️ Remove Field
    • ☑️ Change Field
  • ◼️ Shapes
  • ◼️ Charts
  • ◼️ Lists
    • ☑️ Add lists
    • ◼️ Remove lists
  • ◼️ Table of contents
    • ☑️ Add TOC
  • ☑️ Borders
  • ☑️ Background
  • ◼️ Watermarks
    • ☑️ Add watermark
    • ◼️ Remove watermark

Features (oneliners):

This list of features is for times when you want to quickly fix something rather than playing with full features. This features are available as part of WordHelpers class.

  • ☑️ Remove Headers and Footers from a file

Examples

Basic Document with few document properties and paragraph

This short example show how to create Word Document with just one paragraph with Text and few document properties.

string filePath = @"C:\Support\GitHub\PSWriteOffice\Examples\Documents\BasicDocument.docx";

using (WordDocument document = WordDocument.Create(filePath)) {
    document.Title = "This is my title";
    document.Creator = "Przemysław Kłys";
    document.Keywords = "word, docx, test";

    var paragraph = document.AddParagraph("Basic paragraph");
    paragraph.ParagraphAlignment = JustificationValues.Center;
    paragraph.Color = SixLabors.ImageSharp.Color.Red.ToHexColor();

    document.Save(true);
}

Basic Document with Headers/Footers (first, odd, even)

This short example shows how to add headers and footers to Word Document.

using (WordDocument document = WordDocument.Create(filePath)) {
    document.Sections[0].PageOrientation = PageOrientationValues.Landscape;
    document.AddParagraph("Test Section0");
    document.AddHeadersAndFooters();
    document.DifferentFirstPage = true;
    document.DifferentOddAndEvenPages = true;

    document.Sections[0].Header.First.AddParagraph().SetText("Test Section 0 - First Header");
    document.Sections[0].Header.Default.AddParagraph().SetText("Test Section 0 - Header");
    document.Sections[0].Header.Even.AddParagraph().SetText("Test Section 0 - Even");

    document.AddPageBreak();
    document.AddPageBreak();
    document.AddPageBreak();
    document.AddPageBreak();

    var section1 = document.AddSection();
    section1.PageOrientation = PageOrientationValues.Portrait;
    section1.AddParagraph("Test Section1");
    section1.AddHeadersAndFooters();
    section1.Header.Default.AddParagraph().SetText("Test Section 1 - Header");
    section1.DifferentFirstPage = true;
    section1.Header.First.AddParagraph().SetText("Test Section 1 - First Header");

    document.AddPageBreak();
    document.AddPageBreak();
    document.AddPageBreak();
    document.AddPageBreak();

    var section2 = document.AddSection();
    section2.AddParagraph("Test Section2");
    section2.PageOrientation = PageOrientationValues.Landscape;
    section2.AddHeadersAndFooters();
    section2.Header.Default.AddParagraph().SetText("Test Section 2 - Header");

    document.AddParagraph("Test Section2 - Paragraph 1");

    var section3 = document.AddSection();
    section3.AddParagraph("Test Section3");
    section3.AddHeadersAndFooters();
    section3.Header.Default.AddParagraph().SetText("Test Section 3 - Header");

    Console.WriteLine("Section 0 - Text 0: " + document.Sections[0].Paragraphs[0].Text);
    Console.WriteLine("Section 1 - Text 0: " + document.Sections[1].Paragraphs[0].Text);
    Console.WriteLine("Section 2 - Text 0: " + document.Sections[2].Paragraphs[0].Text);
    Console.WriteLine("Section 2 - Text 1: " + document.Sections[2].Paragraphs[1].Text);
    Console.WriteLine("Section 3 - Text 0: " + document.Sections[3].Paragraphs[0].Text);

    Console.WriteLine("Section 0 - Text 0: " + document.Sections[0].Header.Default.Paragraphs[0].Text);
    Console.WriteLine("Section 1 - Text 0: " + document.Sections[1].Header.Default.Paragraphs[0].Text);
    Console.WriteLine("Section 2 - Text 0: " + document.Sections[2].Header.Default.Paragraphs[0].Text);
    Console.WriteLine("Section 3 - Text 0: " + document.Sections[3].Header.Default.Paragraphs[0].Text);
    document.Save(true);
}