Recursive AI

DOCX in C#: Read, create, and edit

Use OneDocument to read, create, and edit DOCX in C# without Word.

OneDocument is a free, open-source C# document library from Recursive AI, licensed under MIT or Apache-2.0. NuGet · GitHub

Install · .NET 10
dotnet new console
dotnet add package RecursiveAI.OneDocument.Documents --version 1.0.0

OneDocument.Documents 1.0.0 targets .NET 10 (net10.0) only.

Program.cs · .NET 10
using RecursiveAI.OneDocument;
using RecursiveAI.OneDocument.Documents;
 
using (var draft = Document.Create())
{
    draft.Body.AddParagraph("Gate inspection: pending");
    draft.SaveAs("draft.docx");
}
using (var doc = Document.Open("draft.docx"))
{
    doc.Replace("pending", "complete");
    doc.SaveAs("updated.docx");
}
Console.WriteLine(Office.ExtractText("draft.docx").Trim());
Console.WriteLine(Office.ExtractText("updated.docx").Trim());
Output from dotnet run
Gate inspection: pending
Gate inspection: complete

The program creates a draft, edits a copy, and reads both saved files back as text. It does not start Word or use the rendering package.

Which OneDocument package handles DOCX?

PackageChoose it when
DocumentsYou want to read, create, or edit a DOCX through paragraphs and styles. Start here for the example above.
CoreYou need shared APIs or direct access to file containers and XML. Documents already brings in Core.
SchemaYou want typed views of XML elements such as WordprocessingML, rather than the high-level document model. Documents already brings in Schema.
RenderingYou need to lay out pages or export a PDF or page image.

DOCX stores document parts such as the main text and styles in a ZIP container. Opening or editing those parts isn’t the same as laying out pages. If you only need the words or want to change a heading and save another DOCX, you don’t need a renderer. See the package list at 1.0.0 for the full split.

Pick the API by what you need back

  • Text or structure from an existing file: Office.ExtractText gives you text for an index or preview. Document.Open gives you structure, so you can distinguish a heading by its style instead of guessing from its text. See read a Word document.
  • A new Word file: start with Document.Create for content you generate in code. Start with an existing template when the document already has the styles and structure you want. See create a Word document for the blank-file path.
  • An edited Word file: use Document.Open and SaveAs to write a separate copy while you check the result. Use the find and replace recipe to handle phrases split across differently formatted runs and inspect any matches left unchanged.