dotnet add package \ RecursiveAI.OneDocument.Rendering
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument; Office.Convert("report.docx", "report.pdf");
Writes report.pdf.
Download this example (.zip): unzip, then dotnet run.
The full program
The download creates report.docx itself, converts it, and prints the page count and the fonts used in its place. Nothing needs downloading separately. LayoutEngine.Layout and FontNotes are optional diagnostics; they are not needed to convert the file.
using RecursiveAI.OneDocument; using RecursiveAI.OneDocument.Documents; using RecursiveAI.OneDocument.Layout; using (Document made = Document.Create()) { made.Body.AddParagraph("Site visit report"); made.Body.AddParagraph("Replace the stairwell light by Friday."); made.SaveAs("report.docx"); } Office.Convert("report.docx", "report.pdf"); using Document doc = Document.Open("report.docx"); var layout = LayoutEngine.Layout(doc); Console.WriteLine($"Wrote report.pdf ({layout.Pages.Count} page)"); foreach (var note in layout.FontNotes) Console.WriteLine($"Font: {note.Requested} -> {note.Actual} ({note.Kind})");
dotnet run prints:
Wrote report.pdf (1 page) Font: Times New Roman -> Liberation Serif (MetricCompatible)
The sample substitutes Liberation Serif for Times New Roman; it does not reproduce the original font’s letter shapes. The layout docs call this match “same advance widths by design.” A missing input file raises FileNotFoundException; supply an existing DOCX when using the snippet at the top.
Variations
Convert a document you already opened. With the same package references:
using RecursiveAI.OneDocument.Documents; using Document doc = Document.Open("report.docx"); doc.SaveAs("copy.pdf");
Writes copy.pdf from the same document model.
Related recipes
- Read an Excel file in C#
- Create an Excel XLSX file in C#
- Read a Word DOCX document in C#
- Convert Excel to PDF in C#
The short answer
To convert DOCX to PDF in C#, install OneDocument’s Rendering package and call Office.Convert with the input and output paths. The .pdf extension selects the PDF writer. Rendering brings in the Documents dependency that reads the Word file, then lays out its content and writes pages. The download pins Rendering to version 1.0.0 and generates its own input, so its first run needs no existing Word document.
Conversion is more than copying text. The renderer has to measure each line, position paragraphs and tables, and choose where pages end. OneDocument does its own layout. It does not ask an installed copy of Word to export the file. That distinction matters most when fonts or document features affect pagination.
How do I turn a DOCX into a PDF?
Use the snippet at the top for an existing file. For a document you have already opened or edited, use Document.SaveAs("copy.pdf"), as in the variation. The layout documentation says the two routes go through the same rendering path. Referencing the Rendering package registers its formats at build time; there is no registration call to add to this program.
Keep the input and output paths distinct. In the full program, report.docx remains the editable source and report.pdf is the rendered copy. The page count and font notes are printed from a separate layout of that same source; they are not text extracted from the resulting PDF. The output deliberately reports those details instead of a file size, which is not a useful test of how a page looks.
Can I convert DOCX to PDF in C# for free?
Yes. OneDocument is free and open source under MIT or Apache-2.0. The published packages target .NET 10. Use the pinned packages in the download if you want to reproduce this example rather than taking an untested later version.
You do not need Word installed to run this code. The document-reading and rendering code runs in .NET, with no native dependencies. This is a local library API, not an upload to a conversion service. Those properties do not imply that every Word feature has identical output: compare a representative document before using a converted file for a layout-sensitive workflow.
How do I convert DOCX to PDF without Word?
OneDocument supplies the layout engine and default open fonts instead of using Word. This sample requests Times New Roman and uses Liberation Serif. The font substitution table calls that match MetricCompatible: “same advance widths by design.” It is still a different typeface. An Approximate match can have different widths and change line breaks; a Generic match means no known stand-in was found.
The full program lists LayoutDocument.FontNotes, one note per substituted family pair. For your own files, read those notes rather than assuming that a successful conversion used every original font. You can supply fonts through FontLibrary.AddFile or AddDirectory and pass that library in layout options; embedded document fonts are also used by default. Make sure you have the right to use any fonts you supply.
Fonts are not the only source of differences. The layout also exposes notes for skipped content and unsettled fields. Its documentation lists the supported behavior and deviations. Review those notes and inspect the rendered pages for the kinds of documents you actually convert, especially when precise page breaks are part of the requirement.