Turn atlas.docx from the zip into PNGs, one per page, without a PDF.
dotnet add package RecursiveAI.OneDocument.RenderingRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Documents; using RecursiveAI.OneDocument.Rendering; using var doc = Document.Open("atlas.docx"); var options = new PageExportOptions { Pages = PageSelection.Parse("2-3"), Dpi = 150, }; PageExporter.ExportPages(doc, PageImageFormat.Png, page => File.WriteAllBytes($"page-{page.Number}.png", page.Data), options);
Writes page-2.png and page-3.png.
Unzip the example, then dotnet run.
The full program
The sample has three US Letter pages, separated by explicit page breaks: “Atlas: overview,” “Atlas: north route” and “Atlas: south route.” The program prints each file’s name after writing it. At 150 DPI the PNGs are 1275 × 1650 pixels.
using RecursiveAI.OneDocument.Documents; using RecursiveAI.OneDocument.Rendering; using var doc = Document.Open("atlas.docx"); var options = new PageExportOptions { Pages = PageSelection.Parse("2-3"), Dpi = 150, }; PageExporter.ExportPages(doc, PageImageFormat.Png, page => { var name = $"page-{page.Number}.png"; File.WriteAllBytes(name, page.Data); Console.WriteLine(name); }, options);
dotnet run prints:
page-2.png page-3.png
Page numbers start at 1. The end of a range is clipped to the last page, but a range starting past the last page throws ArgumentOutOfRangeException.
Variations
Export every page as a PNG 800 pixels wide. Replace the options declaration in either example. The height follows the page’s proportions.
var options = new PageExportOptions { Pages = PageSelection.All, WidthPixels = 800, };
The full program now prints:
page-1.png page-2.png page-3.png
Related recipes
The short answer
To convert a Word DOCX to one PNG per page in C#, open the document and call PageExporter.ExportPages with PageImageFormat.Png. Set Pages to the range you want and Dpi to the desired resolution. The callback gets the original page number and the encoded bytes for that page. There’s no intermediate PDF and no Word automation.
How do I convert a Word file to PNG without a PDF?
OneDocument lays out the DOCX to determine where text, pictures and other objects go on the page. It then draws each selected page and encodes it directly as PNG. Don’t use SaveAs("page.png") when you want several files: the image writer behind that call writes only the first page for PNG. Use ExportPages and give each output a different name using page.Number.
An export selects pages from the layout, not from a list of page images already stored in the DOCX. If a font is substituted, line breaks and page breaks can differ from those on a computer with the original font. For documents where that matters, look at PageExportReport.Notes for substitutions and layout notes. See the font selection rules for how fonts are resolved.
How do I convert Word pages to high-resolution PNGs?
Use Dpi when you want a set number of pixels per inch of paper. At 150 DPI an 8.5 × 11 inch page is 1275 × 1650 pixels, as in the example. At 300 DPI both dimensions double, so there are four times as many pixels to draw. The default for PNG is 144 DPI. The exporter accepts 1 to 2400 DPI and caps each page at 100 million pixels, so the usable DPI also depends on paper size.
When you need a fixed width for thumbnails, set WidthPixels instead. Supply both WidthPixels and HeightPixels to fit a page inside a box without changing its proportions. The page area is white by default. See page export options for background and encoder settings.