<w:drawing>— inline images, shapes and charts in WordprocessingML (§17.3.3).
Module docxDrawing | Source packages/front/office/ooxml/src/docx/drawing.js | Deps xml, drawingmlShape, ooxmlShared | Worker-safe yes
<w:drawing> wraps either <wp:inline> (in the text flow) or <wp:anchor> (positioned). Inline is the dominant case and is fully modelled; the anchor attributes (wrap, positioning) are preserved verbatim for round-trip fidelity. The content type is sniffed automatically from the binary signature (PNG, JPEG, GIF, BMP, WebP).
Resolve
const dr = runtime.resolve('docxDrawing');
// Returns: { parseDrawing, renderDrawing, withRelationships,
// typedFields, keepSource, image, chart, shape,
// sniffImageType, extensionFor, toEmu,
// EMU_PER_INCH, EMU_PER_CM, EMU_PER_PT, EMU_PER_PX_96,
// REL_TYPE_IMAGE,
// WP_NS, A_NS, PIC_NS }
API
| Method | Signature | Returns |
|---|---|---|
parseDrawing |
(<w:drawing>) => drawing |
Typed model. |
renderDrawing |
(drawing) => element |
<w:drawing>: the kept source of an untouched drawing, else built from the typed fields (see Notes). |
renderRunChild |
(drawing) => element |
The run child a drawing is written as: renderDrawing's element, wrapped in the mc:AlternateContent it was read from when it carries alternate and was not edited (see Notes). |
withRelationships |
({ links: Set, media: Set }, fn) => any |
Runs fn with those ids as the relationships of the part it renders (docx.write sets it around each part). |
typedFields |
(drawing) => object |
The drawing's typed fields: all but image, chart, alternate, embedRef, svgRef, chartRef, docId, _source and _read (what _read holds a copy of). |
keepSource |
(drawing, element) => drawing |
Makes element (a <w:drawing>) the drawing's source: _source set to it, _read to a copy of its current typed fields (in place). |
image |
(bytes, opts?) => drawing |
Inline image builder. |
chart |
(spec, opts?) => drawing |
Inline chart builder. |
shape |
(spec, opts?) => drawing |
Inline shape builder (geometry preset or shapeProps). |
sniffImageType |
(bytes) => string |
MIME type from the signature; 'application/octet-stream' when unrecognised. |
extensionFor |
(contentType) => 'png'|'jpg'|… |
Extension mapping. |
toEmu |
('2in'|number|…) => number |
EMU conversion. |
EMU_PER_INCH, EMU_PER_CM, EMU_PER_PT, EMU_PER_PX_96 |
number | EMU constants. |
REL_TYPE_IMAGE, WP_NS, A_NS, PIC_NS |
string | OPC / namespace bindings. |
Model
{
type: 'drawing',
mode: 'inline'|'anchor',
cx: number, cy: number, // EMU
docId?, docName?, description?, title?, // <wp:docPr>
picName?, picId?, // <pic:cNvPr> of a picture
distT?, distB?, distL?, distR?, // surrounding space
embedRef?: string, // r:id of the image
svgRef?: string, // r:id of its SVG (asvg:svgBlip)
srcRect?: { l?, t?, r?, b? }, // crop, 1/1000 of a percent (a:srcRect)
prstGeom?: string, // 'rect' by default
image?: { data?: Uint8Array, contentType?: string, rId?: string,
svg?: { data: Uint8Array, contentType?: string, rId?: string } },
anchorAttrs?: object, // verbatim
anchorChildren?: [xmlNode],
_extras?: [xmlNode],
alternate?: { requires: string, fallback: xmlNode|null }, // docx.read's alternateContent: 'choice'
_source?: xmlNode, // the <w:drawing> element as read
_read?: object // the typed fields as read
}
Examples
Inline image, 4×3 inches
const dr = runtime.resolve('docxDrawing');
const drawing = dr.image(pngBytes, {
cx: dr.toEmu('4in'), cy: dr.toEmu('3in'),
description: 'Logo'
});
const para = {
type: 'paragraph',
children: [{ type: 'run', children: [drawing] }]
};
Preset shape
dr.shape('roundRect', {
cx: '3in', cy: '1in',
fill: { color: '4472C4' },
name: 'Box'
});
Inline chart
const chartSpec = chartMod.barChart({ series: [...] });
dr.chart(chartSpec, { cx: '5in', cy: '3in' });
Notes
embedRef(andchartReffor a chart) is auto-assigned bydocx.write(a freshrIdon the relationships part of the part holding the drawing: the document, or a header, footer, footnotes, endnotes or comments part) and patched in place on the drawing node.docId(<wp:docPr id>) is made unique bydocx.writeacross the document body and its headers, footers, footnotes, endnotes and comments: a drawing keeps itsdocIdwhen no drawing before it holds the same one; a later duplicate and a drawing without one get the smallest unused id from 1, patched in place likeembedRef.docName(<wp:docPr name>) andpicName(<pic:cNvPr name>) read as the source holds them, an empty name as'', and are written back as they are; a drawing with nodocNameis written as'Picture', a picture with nopicNametakes itsdocName.image.contentTypeis sniffed when absent; the bytes are stored underpkg.parts['/word/media/imageN.<ext>'].- An SVG picture is written as Word writes it:
image.datais the PNG fallback ina:blip, andimage.svg.datathe SVG, written as/word/media/imageN.svg(image/svg+xml) and referenced by anasvg:svgBlipin the blip'sa:extLst(the Office 2016 extension{96DAC541-7B7A-43D3-8B79-37D633B846F1}): Office 2016 and later draw the SVG, older readers the PNG.image(bytes, { svg })builds one;docx.readgivessvgRefandimage.svgfor such a picture, anddocx.writeassignssvgRef(andimage.svg.rId) as it doesembedRef. - The
anchormode is preserved but its attributes are not typed — editanchorAttrs/anchorChildrendirectly. The written<wp:anchor>children follow theCT_Anchororder whatever the order ofanchorChildren:simplePos,positionH,positionV,extent,effectExtent, the wrap element,docPr,cNvGraphicFramePr,a:graphic, thenwp14:sizeRelH,wp14:sizeRelVand any other child in the order given (anmc:AlternateContenttakes the place of what it holds). - Kept source.
parseDrawingkeeps the<w:drawing>element it read in_source, and in_reada copy of the typed fields as read — all butimage,chart,embedRef,svgRef,chartRefanddocId, which the read resolves or the write assigns later. A drawing whose typed fields still equal_readis written from_source, so everything the model does not type stays (a picture's locks, blip state, line and fill,cNvPrdescription, extension list, a shape's style references, groups and canvases …); in itwp:docPr id, the picture'sa:blip r:embed, itsasvg:svgBlip r:embedand a chart'sc:chart r:idare set to the drawing'sdocId,embedRef,svgRefandchartRef. Replacing a picture'simage.datais not an edit: the source names the new picture. The drawing is built from its typed fields instead — losing what they do not hold, as before — when a typed field was edited, when a set id has no place in the source (a chart added to a picture, an SVG added) or a place no id, or when a relationship reference of the source names no relationship the written part holds: one of its hyperlinks, one of its carried relationships kept under its id (docx.write'scarry), or, for the typed ids, its pictures and charts. Called outsidedocx.write,renderDrawingholds no relationship: a source naming any other one is rebuilt. An extension may type_sourcethrough the walker'shydrateDrawinghook and write its edits back by replacing_sourceindehydrateDrawing(keepSourcesets_sourceand_readtogether; docxWalker). The opt-in dmlWpDrawing does it for the typed fields: under it an edited drawing keeps what its source holds. - Alternate content. A drawing read with
docx.read'salternateContent: 'choice'from anmc:AlternateContentcarriesalternate: theRequiresof itsmc:Choiceand itsmc:Fallbackelement (ornull).renderRunChildwrites it inside that construct again —mc:Choice Requiresaround the drawing, then the Fallback — while its typed fields equal_read(or when it has no_read); an edited drawing is written alone, as the Fallback would still show it as it was.alternateis not a typed field: changing it is not an edit. - For chart parts, the chart XML goes to
/word/charts/chartN.xmlwith a…/chartrelationship.
See also
- drawingml-shape —
spPrtyping. - drawingml-chart — chart spec.
- docx —
imageRun/chartRun/shapeRunbuilders.