{"id":1875,"date":"2016-02-03T10:20:07","date_gmt":"2016-02-03T10:20:07","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=1875"},"modified":"2016-03-18T10:42:14","modified_gmt":"2016-03-18T10:42:14","slug":"advanced-use-of-documentbuilder","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/advanced-use-of-documentbuilder\/","title":{"rendered":"Advanced use of DocumentBuilder"},"content":{"rendered":"<p><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/documentbuilder-developer-center\/\">Return to the<br \/>DocumentBuilder<br \/>Developer Center<\/a><\/span>The default approach to working with DocumentBuilder 2.0 enables you to take bits and pieces of multiple documents and assemble them together into a new document.  However, there is an interesting scenario that this approach does not handle.  You may want to import a document into a cell in a table, into a text box, or into a content control.  You can do this with DocumentBuilder 2.0.  The following video shows how:<\/p>\n<p><iframe loading=\"lazy\" title=\"BlockLevelDocumentBuilder.wmv\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/_Z7IfEw6Yvs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>This was the content that I presented in a web-cast on DocumentBuilder 2.0.<\/p>\n<p>Here is the code I presented<\/p>\n<pre>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Xml.Linq;\r\nusing DocumentFormat.OpenXml.Packaging;\r\nusing DocumentFormat.OpenXml.Validation;\r\nusing OpenXmlPowerTools;\r\n\r\nclass Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        WmlDocument doc1 = new WmlDocument(@\"..\\..\\Template.docx\");\r\n        using (MemoryStream mem = new MemoryStream())\r\n        {\r\n            mem.Write(doc1.DocumentByteArray, 0,\r\n                doc1.DocumentByteArray.Length);\r\n            using (WordprocessingDocument doc =\r\n                WordprocessingDocument.Open(mem, true))\r\n            {\r\n                XDocument xDoc = doc\r\n                    .MainDocumentPart\r\n                    .GetXDocument();\r\n                XElement frontMatterPara = xDoc\r\n                    .Root\r\n                    .Descendants(W.txbxContent)\r\n                    .Elements(W.p)\r\n                    .FirstOrDefault();\r\n                frontMatterPara.ReplaceWith(\r\n                    new XElement(PtOpenXml.Insert,\r\n                        new XAttribute(\"Id\", \"Front\")));\r\n                XElement tbl = xDoc\r\n                    .Root\r\n                    .Element(W.body)\r\n                    .Elements(W.tbl)\r\n                    .FirstOrDefault();\r\n                XElement firstCell = tbl\r\n                    .Descendants(W.tr)\r\n                    .First()\r\n                    .Descendants(W.p)\r\n                    .First();\r\n                firstCell.ReplaceWith(\r\n                    new XElement(PtOpenXml.Insert,\r\n                        new XAttribute(\"Id\", \"Liz\")));\r\n                XElement secondCell = tbl\r\n                    .Descendants(W.tr)\r\n                    .Skip(1)\r\n                    .First()\r\n                    .Descendants(W.p)\r\n                    .First();\r\n                secondCell.ReplaceWith(\r\n                    new XElement(PtOpenXml.Insert,\r\n                        new XAttribute(\"Id\", \"Eric\")));\r\n                doc.MainDocumentPart.PutXDocument();\r\n            }\r\n            doc1.DocumentByteArray = mem.ToArray();\r\n        }\r\n\r\n        string outFileName = \"Out.docx\";\r\n        File.Delete(outFileName);\r\n        List<Source> sources = new List<Source>()\r\n            {\r\n                new Source(doc1, true),\r\n                new Source(new WmlDocument(@\"..\\..\\Insert-01.docx\"), \"Liz\"),\r\n                new Source(new WmlDocument(@\"..\\..\\Insert-02.docx\"), \"Eric\"),\r\n                new Source(new WmlDocument(@\"..\\..\\FrontMatter.docx\"), \"Front\"),\r\n            };\r\n        DocumentBuilder.BuildDocument(sources, outFileName);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Return to theDocumentBuilderDeveloper CenterThe default approach to working with DocumentBuilder 2.0 enables you to take bits and pieces of multiple documents and assemble them together into a new document. However, there is an interesting scenario that this approach does not handle. You may want to import a document into a cell in a table, into [&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-1875","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/1875","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=1875"}],"version-history":[{"count":4,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/1875\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/1875\/revisions\/2647"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=1875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}