{"id":2068,"date":"2016-02-04T04:00:45","date_gmt":"2016-02-04T04:00:45","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2068"},"modified":"2016-02-04T04:04:30","modified_gmt":"2016-02-04T04:04:30","slug":"openxmlpackage_addpart","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/openxmlpackage_addpart\/","title":{"rendered":"OpenXmlPackage.addPart(uri, contentType, partType, data)"},"content":{"rendered":"<p><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/open-xml-sdk-for-javascript\/\">Return to the<br \/>Open XML SDK for JavaScript<br \/>Developer Center<\/a><\/span>Adds the specified part to the package. Note that after adding the part, you must separately create relationships to the part.<\/p>\n<h1>Syntax<\/h1>\n<pre>\r\n    openXmlDoc.addPart(uri, contentType, partType, data)\r\n<\/pre>\n<h1>Arguments<\/h1>\n<pre>\r\n    uri: The path to the part in the zip package.\r\n    \r\n    contentType: The content type of the new part. openXml.contentTypes contains a list of most commonly used content types.\r\n    \r\n    partType: A string that contains either \"xml\" or \"base64\". Use \"base64\" to add binary parts such as images.\r\n    \r\n    data: There are three options for this argument.\r\n    - You can pass a string that contains valid XML.\r\n    - Use \"base64\" to add binary parts usch as images.\r\n    - If you are creating an XML part, you can also pass an XDocument or XElement object.\r\n<\/pre>\n<h1>Usage<\/h1>\n<pre>\r\n    var newCommentPart = doc.addPart(\"\/comments.xml\",\r\n        openXml.contentTypes.wordprocessingComments, \"xml\", comments);\r\n    mainPart.addRelationship(\"rId9999\",\r\n        openXml.relationshipTypes.wordprocessingComments,\r\n        \"..\/comments.xml\", \"Internal\");\r\n<\/pre>\n<h1>Example<\/h1>\n<pre>\r\nvar doc = new openXml.OpenXmlPackage(blankDocument_base64);\r\nvar mainPart = doc.mainDocumentPart();\r\nvar xDoc = mainPart.getXDocument();\r\nvar firstPara = xDoc.descendants(W.p).firstOrDefault();\r\nvar newFirstPara = new XElement(W.p,\r\n    firstPara.attributes(),\r\n    new XElement(W.commentRangeStart,\r\n        new XAttribute(W.id, \"1\")),\r\n    new XElement(W.r,\r\n        new XElement(W.t, \"This is a paragraph that contains a comment.\")),\r\n    new XElement(W.r,\r\n        new XElement(W.commentReference,\r\n            new XAttribute(W.id, \"1\"))),\r\n    new XElement(W.commentRangeEnd,\r\n        new XAttribute(W.id, \"1\")));\r\nfirstPara.replaceWith(newFirstPara);\r\nvar comments = new XElement(W.comments,\r\n    new XAttribute(XNamespace.xmlns + \"w\", wNs.namespaceName),\r\n    new XElement(W.comment,\r\n        new XAttribute(W.id, \"1\"),\r\n        new XElement(W.p,\r\n            new XElement(W.r,\r\n                new XElement(W.t, \"A comment\")))));\r\nvar newCommentPart = doc.addPart(\"\/comments.xml\",\r\n    openXml.contentTypes.wordprocessingComments,\r\n    \"xml\", comments);\r\nmainPart.addRelationship(\"rId9999\",\r\n    openXml.relationshipTypes.wordprocessingComments,\r\n    \"..\/comments.xml\", \"Internal\");\r\nreturn {\r\n    returnedDocument: doc.saveToBase64(),\r\n    defaultDocumentName: \"DocumentWithComment.docx\",\r\n    output: [\"This example added a comment to the document.\",\r\n        \"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.\"]\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Return to theOpen XML SDK for JavaScriptDeveloper CenterAdds 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2068","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2068"}],"version-history":[{"count":4,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2068\/revisions"}],"predecessor-version":[{"id":2073,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2068\/revisions\/2073"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}