OpenXmlSdkTs - v1.0.7
    Preparing search index...

    Class WmlPackage

    Word document package — opens, navigates, and saves .docx files.

    Extends OpenXmlPackage with Word-specific convenience methods. All parts returned by this class are typed as WmlPart, giving access to Word-specific navigation like headerParts(), styleDefinitionsPart(), and more.

    import { WmlPackage, W, XElement } from "openxmlsdkts";
    import fs from "fs";

    const buffer = fs.readFileSync("report.docx");
    const doc = await WmlPackage.open(new Blob([buffer]));
    const mainPart = await doc.mainDocumentPart();
    const xDoc = await mainPart!.getXDocument();
    const body = xDoc.root!.element(W.body);
    console.log(`Document has ${body!.elements(W.p).length} paragraphs`);

    // Add a new paragraph
    const newPara = new XElement(W.p,
    new XElement(W.r, new XElement(W.t, "Hello from OpenXmlSdkTs!")));
    body!.add(newPara);
    mainPart!.putXDocument(xDoc);

    const blob = await doc.saveToBlobAsync();

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    • Adds a new part to the package.

      Parameters

      • uri: string

        The part URI (e.g., "/word/comments.xml").

      • contentType: string

        The MIME content type. Use ContentType constants.

      • partType: PartType

        The part data type: "xml", "binary", or "base64".

      • data: unknown

        The part content (an XDocument for XML parts, or a string/Blob for binary).

      Returns OpenXmlPart

      The newly created OpenXmlPart.

      Error if a part with the given URI already exists.

      import { ContentType, XDocument, XElement, W } from "openxmlsdkts";

      const xDoc = new XDocument(new XElement(W.comments));
      pkg.addPart("/word/comments.xml", ContentType.wordprocessingComments, "xml", xDoc);
    • Returns the content type for a part identified by its URI.

      Parameters

      • uri: string

        The full part URI.

      Returns string

      The MIME content type string.

      Error if the content type cannot be determined.

    • Deletes a package-level relationship by ID.

      Parameters

      • id: string

        The relationship ID to delete.

      Returns Promise<boolean>

      true if the relationship was deleted.

      Error if the relationship is not found.

    • Saves the package as a Flat OPC XML string.

      Returns Promise<string>

      A promise resolving to the Flat OPC XML string.

      Flat OPC XML is the required format when working with Office JavaScript/TypeScript Add-ins. It is also useful for storing documents in XML databases, applying XSLT transformations, and debugging document structure.

      const flatOpc = await pkg.saveToFlatOpcAsync();
      fs.writeFileSync("document.xml", flatOpc);
    • Saves the package as a Base64-encoded string.

      Returns Promise<string>

      A promise resolving to the Base64 string.

      Useful for embedding documents in JSON payloads, data URIs, or text-based storage.

      const base64 = await pkg.saveToBase64Async();
      
    • Saves the package as a binary Blob (ZIP).

      Returns Promise<Blob>

      A promise resolving to a Blob containing the document bytes.

      const blob = await pkg.saveToBlobAsync();
      // In Node.js, convert to Buffer for file I/O:
      const buffer = Buffer.from(await blob.arrayBuffer());
      fs.writeFileSync("output.docx", buffer);
    • Returns the main document part (/word/document.xml).

      Returns Promise<WmlPart | undefined>

      The main document WmlPart, or undefined if not found.

    • Returns all content-bearing parts: the main document, headers, footers, endnotes, and footnotes.

      Returns Promise<WmlPart[]>

      An array of WmlPart instances.

      Useful for operations that need to process text across all content areas of the document.