An Open Source cross-platform .NET library providing an easy way to create Microsoft Word (DocX) documents.
$ dotnet add package OfficeIMO.WordOfficeIMO.Word is a cross‑platform .NET library for creating and editing Microsoft Word (.docx) documents on top of Open XML.
OfficeIMO.WordQuick starts and runnable samples live in OfficeIMO.Examples/Word/*.
dotnet add package OfficeIMO.Word
using OfficeIMO.Word;
using var doc = WordDocument.Create("example.docx");
var p = doc.AddParagraph("Hello OfficeIMO.Word");
p.SetBold();
doc.Sections[0].Headers.Default.AddParagraph("Header");
doc.Sections[0].Footers.Default.AddParagraph("Page ");
doc.Sections[0].Footers.Default.AddPageNumber();
doc.Save();
var p = doc.AddParagraph("Title");
p.SetBold();
p = doc.AddParagraph("Body text");
p.AddText(" with italic").SetItalic();
p.AddText(" and code").SetFontFamily("Consolas");
var t = doc.AddTable(3, 3);
t[1,1].Text = "Header 1"; t[1,2].Text = "Header 2"; t[1,3].Text = "Header 3";
t.HeaderRow = true; t.Style = WordTableStyle.TableGrid;
t.MergeCells(2,1, 2,3); // row 2, col 1..3
var imgP = doc.AddParagraph();
imgP.AddImage("logo.png", width: 96, height: 32);
var sec = doc.Sections[0];
sec.Headers.Default.AddParagraph("Report");
var f = sec.Footers.Default;
f.AddParagraph().AddText("Page ");
f.AddPageNumber();
doc.AddParagraph().AddHyperLink("OpenAI", new Uri("https://openai.com/"));
var para = doc.AddParagraph();
var field = para.AddField(WordFieldType.Date, wordFieldFormat: WordFieldFormat.ShortDate);
var para2 = doc.AddParagraph();
// Simple MERGEFIELD with a custom format
para2.AddField(WordFieldType.MergeField, customFormat: "CustomerName");
// Advanced builder for complex instructions/switches
var builder = new WordFieldBuilder(WordFieldType.MergeField)
.CustomFormat("OrderTotal")
.Format(WordFieldFormat.Numeric);
para2.AddField(builder);
var r = doc.AddParagraph("See note").AddText(" ");
r.AddFootNote("This is a footnote.");
var sdtPara = doc.AddParagraph("Name: ");
var dd = sdtPara.AddDropDownList(new[]{"Alpha","Beta","Gamma"});
var shp = doc.AddShape(ShapeTypeValues.Rectangle, 150, 50);
shp.FillColorHex = "#E7FFE7"; shp.StrokeColorHex = "#008000";
var ch = doc.AddChart(ChartType.Bar, 400, 250);
ch.AddSeries("S1", new[]{1,3,2});
ch.AddLegend(LegendPositionValues.Right);
ch.SetXAxisTitle("Categories");
ch.SetYAxisTitle("Values");
doc.SetTextWatermark("CONFIDENTIAL", opacity: 0.15);
doc.ProtectDocument(enforce: true, password: "secret");
// Add headings (styles must map to heading levels)
doc.AddParagraph("Chapter 1").SetStyle("Heading1");
doc.AddParagraph("Section 1.1").SetStyle("Heading2");
// Insert TOC near the top (field will update on open)
doc.Paragraphs[0].AddField(WordFieldType.TOC);
// HTML
using OfficeIMO.Word.Html;
var html = WordHtmlConverter.ToHtml(doc);
var doc2 = WordHtmlConverter.FromHtml("<h1>Hi</h1><p>Generated</p>");
// Markdown
using OfficeIMO.Word.Markdown;
var md = WordMarkdownConverter.ToMarkdown(doc);
var doc3 = WordMarkdownConverter.FromMarkdown("# Title\nBody");
// PDF
using OfficeIMO.Word.Pdf;
doc.SaveAsPdf("out.pdf");
using OfficeIMO.Word.Markdown; // also uses OfficeIMO.Markdown under the hood
// Full HTML document or embeddable fragment produced via Markdown pipeline
var htmlDoc = doc.ToHtmlViaMarkdown();
var htmlFrag = doc.ToHtmlFragmentViaMarkdown();
// Save to file (supports external CSS sidecar via HtmlOptions)
doc.SaveAsHtmlViaMarkdown("report.html");
Common shortcuts for composing content:
H1..H6(string) — adds styled heading paragraphsP(string) — adds a plain paragraphUl(Action<ListBuilder>) — bulleted list; supports .Item, .ItemLink, .ItemTaskOl(Action<ListBuilder>) — numbered list; supports .Item, .ItemLinkParagraph(pb => pb.Text/Link/Bold/Italic/Underline/Strike/Code/InlineImage(...)) — inline helpersTable(tb => tb.Headers(...).Row(...).Rows(...)) — convenience wrappersExample
using OfficeIMO.Word.Fluent;
var fluent = new WordFluentDocument(doc)
.H1("Report")
.P("All‑in‑one DNS/TLS report.")
.Ul(ul => ul.Item("SPF/DKIM/DMARC").ItemLink("Docs", "https://evotec.xyz"))
.Ol(ol => ol.Item("Step one").Item("Step two"))
.Paragraph(p => p.Bold("Note:").Text(" Works with Markdown too."));
Explore
OfficeIMO.Examples/Word/*for complete scenarios.
OfficeIMO.Word.Html (AngleSharp) — convert to/from HTMLOfficeIMO.Word.Markdown — convert to/from Markdown using OfficeIMO.MarkdownOfficeIMO.Word.Pdf (QuestPDF/SkiaSharp) — export to PDFNote: Converters are in active development and will be released to NuGet once they meet quality and test coverage goals. Until then, they ship in‑repo for early evaluation.