Table of Contents

Start from Word's blank document

new WordDocument() is an empty model: no section, no styles, no document defaults. BlankDocument.Create returns what Word creates for File > New > Blank document instead, on the page you ask for, and FormatSections changes the page setup later as one undoable edit.

The code

using DocWright;
using DocWright.Core.Document;
using DocWright.Dom;
using DocWright.Dom.Editing;

// Word's File > New > Blank document, on A4 with Word's Narrow margins.
using WordDocument document = BlankDocument.Create(new BlankDocumentOptions
{
    Paper = PaperSize.A4,
    Orientation = PageOrientation.Portrait,
    Margins = PageMargins.Narrow,
});

Paragraph title = document.Sections[0].Blocks.OfType<Paragraph>().Single();
document.InsertText(DocumentPosition.StartOf(title), "Quarterly report");
document.ApplyParagraphStyle(new DocumentSpan(DocumentPosition.StartOf(title), DocumentPosition.StartOf(title)), "Title");
DocumentEdit split = document.SplitParagraph(DocumentPosition.EndOf(title));
document.InsertText(split.After, "Typed text is Calibri 11 pt, as in Word.");

SectionFormat page = document.Sections[0].Format;
Console.WriteLine($"{PaperSize.Match(page.PageWidth!.Value, page.PageHeight!.Value)}, {PageMargins.Match(page)} margins, "
    + $"styles: {string.Join(", ", document.Styles.Styles.Select(s => s.StyleId))}");

// Page setup is an edit: turn every section the span touches to Letter landscape, then take it back.
var everywhere = new DocumentSpan(DocumentPosition.StartOf(title), DocumentPosition.StartOf(title));
DocumentEdit landscape = document.FormatSections(everywhere, f => f.SetPaper(PaperSize.Letter, PageOrientation.Landscape));
Console.WriteLine($"turned: {document.Sections[0].Format.PageWidth} x {document.Sections[0].Format.PageHeight}");
landscape.Undo();
Console.WriteLine($"undone: {PaperSize.Match(document.Sections[0].Format.PageWidth!.Value, document.Sections[0].Format.PageHeight!.Value)}");

using (FileStream docx = File.Create("blank.docx"))
{
    converter.Save(document, docx);
}

Output

A4, Narrow margins, styles: Normal, DefaultParagraphFont, TableNormal, NoList, Heading1, Heading2, Heading3, Title, Subtitle, ListParagraph
turned: 15840tw x 12240tw
undone: A4
blank.pdf
An A4 page with the title Quarterly report and a line of Calibri text.

What the blank document holds

It is Word 2013–2021's Normal template, whose body face, Calibri, the open font bundle has a metric-compatible substitute for (Carlito), so a document made here breaks lines where Word does:

  • Document defaults: Calibri 11 pt, 8 pt after each paragraph, 1.08 lines.
  • Styles: Normal, Default Paragraph Font, Normal Table, No List, and the built-ins Word adds the first time you use them, so ApplyParagraphStyle finds them by id: Heading1, Heading2, Heading3, Title, Subtitle, ListParagraph.
  • Settings: default tab stop 0.5 in, compatibility mode 15.
  • One section with your page, one empty paragraph.

Fonts and colours are written as values (Calibri, Calibri Light, 2F5496), not theme references. A document built in memory is saved without a theme part, and Word resolves theme references in a package without one to its current theme (Aptos), which would change every line break.

Paper sizes and margins

Type Presets (Word's gallery order)
PaperSize Letter, Legal, Executive, A3, A4, A5, B5Jis ("B5 (JIS)"), Tabloid, Statement; all in PaperSize.Standard
PageMargins Normal (1 in), Narrow (0.5 in), Moderate (1 in top and bottom, 0.75 in sides), Wide (1 in top and bottom, 2 in sides); all in PageMargins.Standard

Find(name) looks one up by name (ordinal, case-insensitive). PaperSize.Match(width, height) and PageMargins.Match(sectionFormat) name a section's page, either way round. SectionFormat.SetPaper(paper, orientation) lays the paper on its side for landscape and writes w:orient; SetMargins(margins) sets the four margins.

Page setup as an edit

FormatSections(span, change) changes a copy of the format of every section the span touches (an empty span: the section it is in) and swaps it in as one DocumentEdit. Undo puts back the very instance that was there, and ChangedParagraphKeys names every paragraph of those sections, so a LayoutSession can follow (Edit text at a position). A span in a header, footer or note is refused: those belong to no one section.