Create a DOCX from text and heading styles without a template or an installed copy of Word.
dotnet add package RecursiveAI.OneDocument.DocumentsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Documents; using var doc = Document.Create(); doc.Styles.AddBuiltIn("Heading 1"); doc.Body.AddParagraph("Quarterly report") .Style = "Heading 1"; doc.Body.AddParagraph("Revenue grew this quarter."); doc.SaveAs("report.docx"); Console.WriteLine("Wrote report.docx");
dotnet run prints:
Wrote report.docxAssigning an undefined style throws StyleNotFoundException; add the built-in style before assigning its name.
Writes report.docx with a “Quarterly report” heading and a paragraph.
Unzip the example, then dotnet run.
Variations
Bold part of a paragraph.
using RecursiveAI.OneDocument.Documents; using var doc = Document.Create(); var paragraph = doc.Body.AddParagraph("Total: "); paragraph.AddRun("$300").Bold = true; doc.SaveAs("formatted.docx");
Writes formatted.docx with only “$300” in bold; it prints nothing.
Related recipes
The short answer
Use Document.Create(), add paragraphs through doc.Body, and call SaveAs with a .docx path. OneDocument writes the document itself; this example does not automate Word. Register a built-in heading style before assigning it to a paragraph.
How do I create a Word document programmatically in C#?
Start with a new document when you control both the content and its structure. The snippet at the top creates two paragraphs: a heading and ordinary body text. The heading’s visible text is “Quarterly report”; its style is Heading 1.
A new document defines only Normal. Calling doc.Styles.AddBuiltIn("Heading 1") adds the built-in definition; assigning .Style then uses that definition. Style names are matched without regard to case. The authoring documentation describes the supported built-in names and their plain, non-theme-linked font definitions.
SaveAs is the step that writes the file. Disposing the document is not a substitute for saving it. In a web application, choose an output location your process can write to, and return the resulting DOCX through your normal file-download response. The example uses the current working directory, so a terminal run leaves report.docx beside the project.
Where can I find a C# example that creates a Word DOCX document?
The download above contains a complete console project with the published package pinned to 1.0.0. There is no input document to find and no placeholder to replace: the program supplies both paragraphs itself. After running it, open report.docx to see the heading and body paragraph.
For mixed formatting, build one paragraph out of runs. The variation adds ordinary “Total: ” text followed by a bold “$300” run. Direct formatting applies only to the run you change. Bold, Italic and Underline accept true, false or null: true turns the property on, false explicitly turns it off, and null removes the direct setting so formatting can come from the style.
Use paragraph styles for recurring roles, such as headings, and run formatting for a local change inside a paragraph. Giving every heading a manually bold run would not assign the Heading 1 paragraph style. To add structured rows and cells rather than text aligned with spaces, use the linked table recipe.
Can I create a Word DOCX document in C# for free?
Yes. OneDocument’s published package is free and open source under MIT or Apache-2.0. The example needs .NET 10 and the Documents package, not a Word installation or a paid service.
This task creates a new document from C# values. If you already have a designed Word file with placeholders, fill a Word template instead. That keeps the starting layout in the input document rather than rebuilding it one paragraph at a time.