OpenXmlSdkTs - v1.0.7
    Preparing search index...

    Class OpenXmlPart

    Represents a single part within an Open XML package.

    A part is one file inside the ZIP archive that makes up an Open XML document. Parts can contain XML (document content, styles, relationships) or binary data (images, embedded objects).

    Use getXDocument() to read and putXDocument() to write XML content. Navigate to related parts via the relationship methods.

    For format-specific convenience methods, see the subclasses WmlPart, SmlPart, and PmlPart.

    const mainPart = await doc.mainDocumentPart();
    const xDoc = await mainPart!.getXDocument();
    const body = xDoc.root!.element(W.body);
    console.log(`${body!.elements(W.p).length} paragraphs`);

    // Navigate to related parts
    const stylesPart = await mainPart!.getPartByRelationshipType(RelationshipType.styles);

    Hierarchy (View Summary)

    Index

    Methods

    • Returns the URI of this part within the package (e.g., "/word/document.xml").

      Returns string

    • Returns the MIME content type of this part (e.g., "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml").

      Returns string | null

    • Sets the MIME content type of this part.

      Parameters

      • ct: string

      Returns void

    • Returns the URI of the .rels file associated with this part.

      Returns string

    • Returns the first part targeted by a 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 relationship to this part.

      Parameters

      • id: string

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

      • type: string

        The relationship type URI. Use RelationshipType constants.

      • target: string

        The target URI (relative to this part).

      • targetMode: string = "Internal"

        "Internal" (default) or "External".

      Returns Promise<OpenXmlRelationship>

      The newly created OpenXmlRelationship.

    • Deletes a relationship from this part.

      Parameters

      • id: string

        The relationship ID to delete.

      Returns Promise<boolean>

      true if the relationship was deleted.

      Error if the relationship is not found.

    • Returns the XML content of this part as an XDocument.

      Returns Promise<XDocument>

      A promise resolving to the part's XDocument.

      The XDocument is parsed on first access and cached for subsequent calls. Modifications to the returned XDocument are reflected in the part. Call putXDocument to replace the XML content entirely.

      Error if the part is not an XML part.

      const xDoc = await mainPart.getXDocument();
      const body = xDoc.root!.element(W.body);
      const paragraphs = body!.elements(W.p);
    • Replaces the XML content of this part.

      Parameters

      Returns void

      Error if xDoc is null or undefined.

      const xDoc = await part.getXDocument();
      // ... modify xDoc ...
      part.putXDocument(xDoc);
    • Finds a part by following a relationship ID from this part.

      Parameters

      • rId: string

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

      Returns Promise<OpenXmlPart | undefined>