OpenXmlPackage.addPart(uri, contentType, partType, data)

Return to the
Open XML SDK for JavaScript
Developer Center
Adds the specified part to the package. Note that after adding the part, you must separately create relationships to the part.

Syntax

    openXmlDoc.addPart(uri, contentType, partType, data)

Arguments

    uri: The path to the part in the zip package.
    
    contentType: The content type of the new part. openXml.contentTypes contains a list of most commonly used content types.
    
    partType: A string that contains either "xml" or "base64". Use "base64" to add binary parts such as images.
    
    data: There are three options for this argument.
    - You can pass a string that contains valid XML.
    - Use "base64" to add binary parts usch as images.
    - If you are creating an XML part, you can also pass an XDocument or XElement object.

Usage

    var newCommentPart = doc.addPart("/comments.xml",
        openXml.contentTypes.wordprocessingComments, "xml", comments);
    mainPart.addRelationship("rId9999",
        openXml.relationshipTypes.wordprocessingComments,
        "../comments.xml", "Internal");

Example

var doc = new openXml.OpenXmlPackage(blankDocument_base64);
var mainPart = doc.mainDocumentPart();
var xDoc = mainPart.getXDocument();
var firstPara = xDoc.descendants(W.p).firstOrDefault();
var newFirstPara = new XElement(W.p,
    firstPara.attributes(),
    new XElement(W.commentRangeStart,
        new XAttribute(W.id, "1")),
    new XElement(W.r,
        new XElement(W.t, "This is a paragraph that contains a comment.")),
    new XElement(W.r,
        new XElement(W.commentReference,
            new XAttribute(W.id, "1"))),
    new XElement(W.commentRangeEnd,
        new XAttribute(W.id, "1")));
firstPara.replaceWith(newFirstPara);
var comments = new XElement(W.comments,
    new XAttribute(XNamespace.xmlns + "w", wNs.namespaceName),
    new XElement(W.comment,
        new XAttribute(W.id, "1"),
        new XElement(W.p,
            new XElement(W.r,
                new XElement(W.t, "A comment")))));
var newCommentPart = doc.addPart("/comments.xml",
    openXml.contentTypes.wordprocessingComments,
    "xml", comments);
mainPart.addRelationship("rId9999",
    openXml.relationshipTypes.wordprocessingComments,
    "../comments.xml", "Internal");
return {
    returnedDocument: doc.saveToBase64(),
    defaultDocumentName: "DocumentWithComment.docx",
    output: ["This example added a comment to the document.",
        "Click the 'Save' button to save the modified document to a local drive.  If you do not see a 'Save' button, you do not have Flash available, and can't save the document."]
};