OpenXmlSdkTs - v1.0.7
    Preparing search index...

    Class OpenXmlPackage

    Base class for all Open XML document packages.

    An Open XML package is a ZIP archive (or equivalent Flat OPC / Base64 representation) containing parts (XML documents, images, and other binary data) linked by relationships.

    OpenXmlPackage provides format-agnostic opening and saving, part management, and relationship navigation. For format-specific convenience methods, use the subclasses WmlPackage, SmlPackage, or PmlPackage.

    The static open() method auto-detects the input format: binary Blob, Base64 string, or Flat OPC XML string.

    import { OpenXmlPackage } from "openxmlsdkts";
    import fs from "fs";

    const buffer = fs.readFileSync("document.docx");
    const pkg = await OpenXmlPackage.open(new Blob([buffer]));
    const parts = pkg.getParts();
    console.log(`Package contains ${parts.length} parts`);

    const blob = await pkg.saveToBlobAsync();

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    • Opens an Open XML document from any supported format.

      Parameters

      • document: string | Blob

        The document to open.

      Returns Promise<OpenXmlPackage>

      A promise resolving to an OpenXmlPackage instance.

      Auto-detects the input format: binary Blob, Base64 string, or Flat OPC XML string. For format-specific packages, use WmlPackage.open, SmlPackage.open, or PmlPackage.open instead.

      // Open from binary Blob
      const buffer = fs.readFileSync("document.docx");
      const pkg = await OpenXmlPackage.open(new Blob([buffer]));

      // Open from Base64 string
      const base64 = fs.readFileSync("document.txt", "utf-8");
      const pkg2 = await OpenXmlPackage.open(base64);

      // Open from Flat OPC XML string
      const flatOpc = fs.readFileSync("document.xml", "utf-8");
      const pkg3 = await OpenXmlPackage.open(flatOpc);
    • 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.

    • Returns parts that are targets of package-level relationships of the given type.

      Parameters

      • relationshipType: string

        The relationship type URI. Use RelationshipType constants.

      Returns Promise<OpenXmlPart[]>

      An array of matching OpenXmlPart instances.

      import { RelationshipType } from "openxmlsdkts";

      const themeParts = await pkg.getPartsByRelationshipType(RelationshipType.theme);
    • Returns parts whose content type matches the given value.

      Parameters

      • contentType: string

        The MIME content type. Use ContentType constants.

      Returns Promise<OpenXmlPart[]>

      An array of matching OpenXmlPart instances.

      import { ContentType } from "openxmlsdkts";

      const themeParts = await pkg.getPartsByContentType(ContentType.theme);
    • Returns the first part targeted by a package-level relationship of the given type.

      Parameters

      • relationshipType: string

        The relationship type URI. Use RelationshipType constants.

      Returns Promise<OpenXmlPart | undefined>

      The first matching OpenXmlPart, or undefined.

    • Adds a package-level relationship.

      Parameters

      • id: string

        The relationship ID (e.g., "rId10").

      • type: string

        The relationship type URI. Use RelationshipType constants.

      • target: string

        The target URI.

      • targetMode: string = "Internal"

        "Internal" (default) or "External".

      Returns Promise<OpenXmlRelationship>

      The newly created OpenXmlRelationship.

    • 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.

    • Deletes a relationship from a specific part.

      Parameters

      • part: OpenXmlPart

        The source part.

      • 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);