Quick Reference Cheatsheet
๐จ THE BIG THREE (Never Do This!)โ
โ 1. Text Without Runโ
<!-- WRONG -->
<w:p>
<w:t>Text</w:t>
</w:p>
<!-- CORRECT -->
<w:p>
<w:r><w:t>Text</w:t></w:r>
</w:p>
โ 2. Table Inside Runโ
<!-- WRONG -->
<w:r>
<w:tbl>...</w:tbl>
</w:r>
<!-- CORRECT -->
<w:body>
<w:tbl>...</w:tbl>
</w:body>
โ 3. SDT Inside Runโ
<!-- WRONG -->
<w:r>
<w:sdt>...</w:sdt>
</w:r>
<!-- CORRECT (Block SDT) -->
<w:body>
<w:sdt>...</w:sdt>
</w:body>
<!-- OR (Inline SDT) -->
<w:p>
<w:sdt>
<w:sdtContent>
<w:r><w:t>Text</w:t></w:r>
</w:sdtContent>
</w:sdt>
</w:p>
Element Hierarchyโ
w:document
โโ w:body
โโ w:p (paragraph)
โ โโ w:pPr (properties)
โ โโ w:r (run)
โ โโ w:rPr (properties)
โ โโ w:t (text)
โ
โโ w:tbl (table)
โ โโ w:tblPr (properties)
โ โโ w:tblGrid (grid definition)
โ โโ w:tr (row)
โ โโ w:trPr (properties)
โ โโ w:tc (cell)
โ โโ w:tcPr (properties)
โ โโ w:p (REQUIRED)
โ
โโ w:sdt (content control, block-level)
โโ w:sdtPr (properties)
โโ w:sdtContent (content)
Unit Conversionsโ
| Unit | Conversion | Example |
|---|---|---|
| Twips โ Points | Divide by 20 | 1440 twips = 72 pt (1 inch) |
| Half-points โ Points | Divide by 2 | w:sz="24" = 12pt |
| EMUs โ Points | Divide by 12700 | 914400 EMUs = 72 pt (1 inch) |
| Percentage | 5000 = 100% | w:w="2500" = 50% |
| Inches โ Twips | Multiply by 1440 | 1 inch = 1440 twips |
| Inches โ Points | Multiply by 72 | 1 inch = 72 points |
Common Elementsโ
Paragraphโ
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:jc w:val="center"/>
<w:spacing w:before="120" w:after="120"/>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:sz w:val="24"/>
</w:rPr>
<w:t>Text</w:t>
</w:r>
</w:p>
Tableโ
<w:tbl>
<w:tblPr>
<w:tblW w:w="5000" w:type="pct"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="3000"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:w="3000" w:type="dxa"/>
</w:tcPr>
<w:p>...</w:p>
</w:tc>
</w:tr>
</w:tbl>
Content Control (Block)โ
<w:sdt>
<w:sdtPr>
<w:tag w:val="FieldName"/>
<w:alias w:val="Display Name"/>
</w:sdtPr>
<w:sdtContent>
<w:p>...</w:p>
</w:sdtContent>
</w:sdt>
Text Formattingโ
| Property | XML | Code |
|---|---|---|
| Bold | <w:b/> | new Bold() |
| Italic | <w:i/> | new Italic() |
| Underline | <w:u w:val="single"/> | new Underline { Val = UnderlineValues.Single } |
| Font | <w:rFonts w:ascii="Arial"/> | new RunFonts { Ascii = "Arial" } |
| Font Size | <w:sz w:val="24"/> | new FontSize { Val = "24" } (12pt) |
| Color | <w:color w:val="FF0000"/> | new Color { Val = "FF0000" } |
| Highlight | <w:highlight w:val="yellow"/> | new Highlight { Val = HighlightColorValues.Yellow } |
Paragraph Formattingโ
| Property | XML | Code |
|---|---|---|
| Alignment | <w:jc w:val="center"/> | new Justification { Val = JustificationValues.Center } |
| Before Spacing | <w:spacing w:before="240"/> | new SpacingBetweenLines { Before = "240" } |
| After Spacing | <w:spacing w:after="120"/> | new SpacingBetweenLines { After = "120" } |
| Line Spacing | <w:spacing w:line="276" w:lineRule="auto"/> | new SpacingBetweenLines { Line = "276", LineRule = LineSpacingRuleValues.Auto } |
| Left Indent | <w:ind w:left="720"/> | new Indentation { Left = "720" } |
| First Line Indent | <w:ind w:firstLine="360"/> | new Indentation { FirstLine = "360" } |
Table Propertiesโ
| Property | XML | Code |
|---|---|---|
| Width (%) | <w:tblW w:w="5000" w:type="pct"/> | new TableWidth { Width = "5000", Type = TableWidthUnitValues.Pct } |
| Width (abs) | <w:tblW w:w="5000" w:type="dxa"/> | new TableWidth { Width = "5000", Type = TableWidthUnitValues.Dxa } |
| Alignment | <w:jc w:val="center"/> | new TableJustification { Val = TableRowAlignmentValues.Center } |
| Cell Span | <w:gridSpan w:val="2"/> | new GridSpan { Val = 2 } |
| V-Merge Start | <w:vMerge w:val="restart"/> | new VerticalMerge { Val = MergedCellValues.Restart } |
| V-Merge Continue | <w:vMerge w:val="continue"/> | new VerticalMerge { Val = MergedCellValues.Continue } |
| Can't Split | <w:cantSplit/> | new CantSplit() |
Border Stylesโ
new TableBorders(
new TopBorder { Val = BorderValues.Single, Size = 4, Color = "000000" },
new BottomBorder { Val = BorderValues.Single, Size = 4, Color = "000000" },
new LeftBorder { Val = BorderValues.Single, Size = 4, Color = "000000" },
new RightBorder { Val = BorderValues.Single, Size = 4, Color = "000000" },
new InsideHorizontalBorder { Val = BorderValues.Single, Size = 4, Color = "000000" },
new InsideVerticalBorder { Val = BorderValues.Single, Size = 4, Color = "000000" }
)
Border Values:
Single- Single lineDouble- Double lineDotted- Dotted lineDashed- Dashed lineNone- No border
Size: Eighths of a point (4 = 0.5pt, 8 = 1pt)
Namespacesโ
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; // w:
using DocumentFormat.OpenXml.Drawing; // a:
using DocumentFormat.OpenXml.Drawing.Wordprocessing; // wp:
using DocumentFormat.OpenXml.Drawing.Pictures; // pic:
| Prefix | Namespace | Purpose |
|---|---|---|
w: | WordprocessingML | Core document elements |
r: | Relationships | References between parts |
wp: | Drawing WordprocessingML | Inline/anchored drawings |
pic: | Picture | Images |
a: | Drawing | Graphic properties |
v: | VML | Legacy shapes |
OOXML โ SFDT Quick Mappingโ
| OOXML | SFDT | Notes |
|---|---|---|
w:p | block with inlines | Paragraph |
w:r + w:t | inline with text | Text run |
w:tbl | table | Table |
w:tr | row | Table row |
w:tc | cell | Table cell |
w:pPr | paragraphFormat | Paragraph properties |
w:rPr | characterFormat | Character properties |
w:sz (half-pts) | fontSize (pts) | Font size: divide by 2 |
| Spacing (twips) | Spacing (pts) | Divide by 20 |
w:drawing | imageString (base64) | Image |
Common Code Patternsโ
Create Documentโ
using (var doc = WordprocessingDocument.Create(
filepath, WordprocessingDocumentType.Document))
{
var mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
var body = mainPart.Document.AppendChild(new Body());
// Add content
mainPart.Document.Save();
}
Add Paragraphโ
var para = new Paragraph();
var run = new Run();
run.AppendChild(new Text("Hello World"));
para.AppendChild(run);
body.AppendChild(para);
Add Formatted Textโ
var run = new Run();
var runProps = new RunProperties(
new Bold(),
new FontSize { Val = "28" },
new Color { Val = "FF0000" }
);
run.AppendChild(runProps);
run.AppendChild(new Text("Bold Red Text"));
Create Table Cellโ
var cell = new TableCell();
cell.AppendChild(new TableCellProperties(
new TableCellWidth { Width = "3000", Type = TableWidthUnitValues.Dxa }
));
var para = new Paragraph(new Run(new Text("Cell content")));
cell.AppendChild(para);
Validationโ
Open XML SDK Toolโ
- Download from GitHub releases
- Open DOCX file
- Click "Validate"
- Review errors
Programmatic Validationโ
using (var doc = WordprocessingDocument.Open("file.docx", false))
{
var validator = new OpenXmlValidator();
var errors = validator.Validate(doc);
foreach (var error in errors)
{
Console.WriteLine($"{error.Path}: {error.Description}");
}
}
File Structureโ
document.docx (ZIP)
โโ [Content_Types].xml
โโ _rels/
โ โโ .rels
โโ word/
โ โโ document.xml โ Main content
โ โโ styles.xml โ Styles
โ โโ numbering.xml โ Lists
โ โโ settings.xml
โ โโ fontTable.xml
โ โโ _rels/
โ โ โโ document.xml.rels โ Relationships
โ โโ media/
โ โโ image1.png
โ โโ image2.jpg
โโ docProps/
โโ app.xml
โโ core.xml
AppraiSys Specificโ
Default Stylesโ
Normal- Base paragraph styleHeading1- 16pt bold, centeredHeading2- 14pt bold, leftNormalTable- Standard tablePropertyImagesTable- Photo gallery tableImageLineSpacing1_15- For image paragraphs
Standard Measurementsโ
- Font size: 11pt (22 half-points)
- Line spacing: 1.15 (276 twips)
- Cell margins: 108 twips left/right, 0 top/bottom
- Page margins: 1 inch (1440 twips)
Content Control Tagsโ
PropertyAddressPropertyTypeSubjectPropertyInfoComparablesTablePropertyPhotos
Debugging Tipsโ
Document Won't Openโ
- Rename
.docxto.zip - Extract contents
- Check
word/document.xmlfor XML errors - Use OpenXML SDK tool
Images Missingโ
- Check
/word/media/folder exists - Check
document.xml.relshas image relationships - Verify relationship IDs match
Formatting Lostโ
- Check
styles.xmlexists - Verify style IDs match in
document.xml - Ensure
w:pStyleorw:rStylereferences valid style
Error Messagesโ
| Error | Cause | Fix |
|---|---|---|
| "Element X cannot be child of Y" | Wrong nesting | Check hierarchy rules |
| "Required element missing" | Missing required child | Add required element |
| "Invalid attribute value" | Wrong value type | Check valid values |
| "Relationship not found" | Missing .rels entry | Add relationship |
Quick Formulasโ
Points to Twips: points ร 20
Twips to Points: twips รท 20
Half-pts to Points: halfPoints รท 2
Points to Half-pts: points ร 2
EMUs to Points: emus รท 12700
Inches to Twips: inches ร 1440
Inches to Points: inches ร 72
Percentage (width): (percent รท 100) ร 5000
Resourcesโ
๐ก Print this page and keep it at your desk for quick reference!