{"id":2051,"date":"2016-02-04T02:04:34","date_gmt":"2016-02-04T02:04:34","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2051"},"modified":"2016-02-04T02:04:34","modified_gmt":"2016-02-04T02:04:34","slug":"using-a-template-document-with-the-open-xml-sdk-for-javascript","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/using-a-template-document-with-the-open-xml-sdk-for-javascript\/","title":{"rendered":"Using a Template Document with the Open XML SDK for JavaScript"},"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>When you write an Open XML program, you rarely (maybe never) want to create a document from scratch. &nbsp;Instead, you want to start with a template document and then add content to it as you need to. &nbsp;This applies whether you are creating word-processing documents, spreadsheets, or presentations. &nbsp;There are two approaches to accessing a template document when using the Open XML SDK for JavaScript.<\/p>\n<ul>\n<li>You can store an Open XML document as base64 encoded ASCII in a JavaScript string variable.<\/li>\n<li>You can use AJAX to retrieve the template document from your web server.<\/li>\n<\/ul>\n<p>This screen-cast walks through both approaches.<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/openxmldeveloper.org\/WebResource.axd?d=R2dYwQKSjh_kn3Km7sfplH25xRaAJInOyc3WcnK_HpI8ge5nbU65S4Iq1EdiBSdI5Yt9kNU080089mM89ts1oalqo3WYHmie6JAudwZqmejrrmphOPrLznSMpimCg9EV3CgVph-893Tg32DdSz7qbMsnCxmykVWYFskfS7WHUL6hmKg90&#038;t=634211876060000000\"><\/script><\/p>\n<div id=\"video_2ba4ea36-a93a-4e29-9b6b-10d56f74f51e\"><noscript><embed src=\"http:\/\/www.youtube.com\/v\/pTuNwfJ_09Q&#038;rel=1\" type=\"application\/x-shockwave-flash\" wmode=\"transparent\" width=\"640\" height=\"385\"><\/embed><\/noscript><\/div>\n<p><script type=\"text\/javascript\">\ncs_setInnerHtml('video_2ba4ea36-a93a-4e29-9b6b-10d56f74f51e','<embed src=\\\"http:\\\/\\\/www.youtube.com\\\/v\\\/pTuNwfJ_09Q&#038;rel=1\\\" type=\\\"application\\\/x-shockwave-flash\\\" wmode=\\\"transparent\\\" width=\\\"640\\\" height=\\\"385\\\"><\\\/embed>');\n<\/script><\/p>\n<p>In the video, I discuss two small PowerShell scripts. &nbsp;One is a modification that I make to my profile.ps1 that enables converting any binary file to base64 encoded ASCII. &nbsp;The other is a small function that takes a DOCX, XLSX, or PPTX, and generates a JavaScript literal string expression that you can paste directly into your JavaScript application.<\/p>\n<p>Here are the Convert-ToB64 and Convert-FromB64 functions:<\/p>\n<pre class=\"prettyprint\">$sz8 = @&quot;\r\nusing System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing System.Linq;\r\n\r\npublic class B64\r\n{\r\n    public static string ConvertToB64(string fileName)\r\n    {\r\n        byte[] ba = System.IO.File.ReadAllBytes(fileName);\r\n        string base64String = (System.Convert.ToBase64String(ba))\r\n            .Select\r\n            (\r\n                (c, i) =&gt; new\r\n                {\r\n                    Character = c,\r\n                    Chunk = i \/ 76\r\n                }\r\n            )\r\n            .GroupBy(c =&gt; c.Chunk)\r\n            .Aggregate(\r\n                new StringBuilder(),\r\n                (s, i) =&gt;\r\n                    s.Append(\r\n                        i.Aggregate(\r\n                            new StringBuilder(),\r\n                            (seed, it) =&gt; seed.Append(it.Character),\r\n                            sb =&gt; sb.ToString()\r\n                        )\r\n                    )\r\n                    .Append(Environment.NewLine),\r\n                s =&gt;\r\n                {\r\n                    s.Length -= Environment.NewLine.Length;\r\n                    return s.ToString();\r\n                }\r\n            );\r\n\r\n        return base64String;\r\n    }\r\n\r\n    public static void ConvertFromB64(string fileName, string b64)\r\n    {\r\n        string b64b = b64.Replace(&quot;\\r\\n&quot;, &quot;&quot;);\r\n        byte[] ba = System.Convert.FromBase64String(b64b);\r\n        System.IO.File.WriteAllBytes(fileName, ba);\r\n    }\r\n}\r\n&quot;@;\r\n\r\nAdd-Type -TypeDefinition $sz8\r\n\r\nfunction Convert-ToB64 {\r\n    param(\r\n\t\t[string]$Path\r\n\t)\r\n    $f = (Resolve-Path $Path).ToString();\r\n    [B64]::ConvertToB64($f)\r\n}\r\n\r\nfunction Convert-FromB64 {\r\n    param(\r\n\t\t[string]$Path,\r\n        [string]$b64\r\n\t)\r\n    $f = New-Item $Path -ItemType file\r\n    [B64]::ConvertFromB64($f.FullName, $b64)\r\n}\r\n<\/pre>\n<p>Here is the small PowerShell function that uses the above functionality to convert a DOCX to a JavaScript string literal:<\/p>\n<pre class=\"prettyprint\">function ConvertDocxToJavaScriptLiteral {\r\n    param (\r\n        $path\r\n    )\r\n    $f = Resolve-Path $path\r\n    $a = Convert-ToB64 $f\r\n    $b = $a -split &quot;\\r\\n&quot;\r\n    $count = $b.Count\r\n    $c = $b | Select-Object -First $($count - 1) | % { &quot;`&quot;$_`&quot; +&quot; }\r\n    $d = $b | Select-Object -Last 1 | % { &quot;`&quot;$_`&quot;&quot; }\r\n    $c + $d | clip\r\n}<\/pre>\n<p>You use it as follows:<\/p>\n<pre class=\"prettyprint\">ConvertDocxToJavaScriptLiteral .\\Test01.docx<\/pre>\n<p>This converts the DOCX to the JavaScript literals, and places the code on the clipboard, ready for you to paste into your JavaScript program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Return to theOpen XML SDK for JavaScriptDeveloper CenterWhen you write an Open XML program, you rarely (maybe never) want to create a document from scratch. &nbsp;Instead, you want to start with a template document and then add content to it as you need to. &nbsp;This applies whether you are creating word-processing documents, spreadsheets, or presentations. [&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-2051","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2051","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=2051"}],"version-history":[{"count":1,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2051\/revisions"}],"predecessor-version":[{"id":2052,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2051\/revisions\/2052"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}