docx
    Preparing search index...

    Class Body

    Represents the document body in a WordprocessingML document.

    The body element is the container for all block-level content in the document. This includes paragraphs, tables, and section properties that define page layout.

    The body supports multiple sections, where each section (except the last one) must have its section properties stored in a paragraph's properties at the end of that section. The last section's properties are stored as a direct child of the body element.

    Reference: http://officeopenxml.com/WPdocument.php

    <xsd:complexType name="CT_Body">
    <xsd:sequence>
    <xsd:group ref="EG_BlockLevelElts" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="sectPr" minOccurs="0" maxOccurs="1" type="CT_SectPr"/>
    </xsd:sequence>
    </xsd:complexType>
    // Body is typically created internally by the Document class
    const doc = new Document({});
    const body = doc.Body;

    // Add content to the body via Document.add()
    doc.add(new Paragraph("Content in first section"));

    // Add a new section
    body.addSection({
    page: {
    size: { width: 12240, height: 15840 },
    },
    });

    // Content after addSection belongs to the new section
    doc.add(new Paragraph("Content in second section"));

    Hierarchy (View Summary)

    Index
    • get writtenAs(): readonly BaseXmlComponent[] | undefined
      Internal

      The components written in this one's place, one after another, when it can't be written as a single element, such as a run with another run in its children. Undefined when it is written as itself.

      Returns readonly BaseXmlComponent[] | undefined

    • Adds new section properties to the document body.

      Creates a new section by moving the previous section's properties into a paragraph at the end of that section, and then adding the new section as the current section.

      According to the OOXML specification:

      • Section properties for all sections except the last must be stored in a paragraph's properties (pPr/sectPr) at the end of each section
      • The last section's properties are stored as a direct child of the body element (w:body/w:sectPr)

      Parameters

      Returns void

    • Finds the section properties that govern a top-level child of the body.

      A section's properties are stored after its content (either in the closing paragraph of the section or, for the last section, at the end of the body), so the governing section is the first one found at or after the child. When no child is given (or it is not a direct child of the body), the first section is returned.

      Parameters

      • Optionalchild: XmlComponent

        A direct child of the body (paragraph, table, etc.)

      Returns SectionProperties | undefined

      The governing section properties, or undefined if the body has no sections

    • Prepares the body element for XML serialization.

      Ensures that the last section's properties are placed as a direct child of the body element, as required by the OOXML specification. Once the body is written, its tables of contents are filled in from its headings, and, when the body has a page number estimator, its page references are given their page numbers. Its SEQ fields are given their numbers after the tables of contents are filled in, as Word leaves a heading's SEQ number out of its entry.

      Parameters

      • context: IContext

        The XML serialization context

      Returns IXmlableObject | undefined

      The prepared XML object or undefined

    • Adds a block-level component to the body.

      This method is used internally by the Document class to add paragraphs, tables, and other block-level elements to the document body.

      Parameters

      • component: XmlComponent

        The XML component to add (paragraph, table, etc.)

      Returns void