{"id":3042,"date":"2016-03-19T09:51:19","date_gmt":"2016-03-19T09:51:19","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=3042"},"modified":"2016-03-20T09:33:52","modified_gmt":"2016-03-20T09:33:52","slug":"how-to-insert-a-page-break-between-imported-content","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/how-to-insert-a-page-break-between-imported-content\/","title":{"rendered":"How to Insert a Page Break between Imported Content"},"content":{"rendered":"<div class=\"post-content user-defined-markup\">A question arose in the forums &#8211; when importing HTML content using altChunk, can you cause KeepLines \/ KeepNext to be set for imported paragraphs.  This is not possible.  You can control this when importing a WordprocessingML document using altChunk &#8211; you simply go into the imported document and set keepLines (section 17.3.1.14 in IS29500) \/ keepNext (section 17.3.1.15), and when Word imports that content, the resulting paragraphs have their keepLines \/ keepNext set per the imported content.  However, there is no analogous markup for HTML, so there is no way to set those child elements of the paragraph properties element.<\/p>\n<p>However, it is straightforward to insert a page break between imported chunks of content.  The following example sets up a document with two imported chunks, with a page break inserted between them.<\/p>\n<p>using&nbsp;System;<br \/>\nusing&nbsp;System.Collections.Generic;<br \/>\nusing&nbsp;System.Linq;<br \/>\nusing&nbsp;System.IO;<br \/>\nusing&nbsp;DocumentFormat.OpenXml;<br \/>\nusing&nbsp;DocumentFormat.OpenXml.Packaging;<br \/>\nusing&nbsp;DocumentFormat.OpenXml.Wordprocessing;<\/p>\n<p>class&nbsp;Program<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;void&nbsp;AppendAltChunk(WordprocessingDocument&nbsp;doc,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;altChunkId,&nbsp;string&nbsp;html)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MainDocumentPart&nbsp;mainPart&nbsp;=&nbsp;doc.MainDocumentPart;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AlternativeFormatImportPart&nbsp;chunk&nbsp;=&nbsp;mainPart.AddAlternativeFormatImportPart(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;application\/xhtml+xml&#8221;,&nbsp;altChunkId);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using&nbsp;(Stream&nbsp;chunkStream&nbsp;=&nbsp;chunk.GetStream(FileMode.Create,&nbsp;FileAccess.Write))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using&nbsp;(StreamWriter&nbsp;stringStream&nbsp;=&nbsp;new&nbsp;StreamWriter(chunkStream))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stringStream.Write(html);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AltChunk&nbsp;altChunk&nbsp;=&nbsp;new&nbsp;AltChunk();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;altChunk.Id&nbsp;=&nbsp;altChunkId;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenXmlElement&nbsp;last&nbsp;=&nbsp;doc.MainDocumentPart.Document<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Body<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Elements()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LastOrDefault(e&nbsp;=&gt;&nbsp;e&nbsp;is&nbsp;Paragraph&nbsp;||&nbsp;e&nbsp;is&nbsp;AltChunk);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(last&nbsp;==&nbsp;null)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doc.MainDocumentPart.Document.Body.InsertAt(altChunk,&nbsp;0);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last.InsertAfterSelf(altChunk);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;void&nbsp;AppendPageBreak(WordprocessingDocument&nbsp;myDoc)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MainDocumentPart&nbsp;mainPart&nbsp;=&nbsp;myDoc.MainDocumentPart;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenXmlElement&nbsp;last&nbsp;=&nbsp;myDoc.MainDocumentPart.Document<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Body<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Elements()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LastOrDefault(e&nbsp;=&gt;&nbsp;e&nbsp;is&nbsp;Paragraph&nbsp;||&nbsp;e&nbsp;is&nbsp;AltChunk);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last.InsertAfterSelf(new&nbsp;Paragraph(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Run(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;Break()&nbsp;{&nbsp;Type&nbsp;=&nbsp;BreakValues.Page&nbsp;})));<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;void&nbsp;Main(string[]&nbsp;args)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using&nbsp;(WordprocessingDocument&nbsp;doc&nbsp;=<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WordprocessingDocument.Open(&#8220;Test.docx&#8221;,&nbsp;true))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;First,&nbsp;delete&nbsp;all&nbsp;paragraphs&nbsp;in&nbsp;the&nbsp;document,&nbsp;creating&nbsp;a&nbsp;blank&nbsp;document.<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;paras&nbsp;=&nbsp;doc.MainDocumentPart<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Document<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Body<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Elements&lt;Paragraph&gt;()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;p&nbsp;in&nbsp;paras)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.Remove();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;html1&nbsp;=<br \/>\n@&#8221;&lt;html&gt;<br \/>\n&lt;head\/&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h1&gt;Html&nbsp;Heading&lt;\/h1&gt;<br \/>\n&lt;p&gt;On&nbsp;the&nbsp;Insert&nbsp;tab,&nbsp;the&nbsp;galleries&nbsp;include&nbsp;items&nbsp;that&nbsp;are&nbsp;designed&nbsp;to&nbsp;coordinate.&lt;\/p&gt;<br \/>\n&lt;p&gt;Most&nbsp;controls&nbsp;offer&nbsp;a&nbsp;choice&nbsp;of&nbsp;using&nbsp;the&nbsp;look&nbsp;from&nbsp;the&nbsp;current&nbsp;theme.&lt;\/p&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;html2&nbsp;=<br \/>\n@&#8221;&lt;html&gt;<br \/>\n&lt;head\/&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h1&gt;New&nbsp;Heading&lt;\/h1&gt;<br \/>\n&lt;p&gt;This&nbsp;content&nbsp;is&nbsp;on&nbsp;its&nbsp;own&nbsp;page.&lt;\/p&gt;<br \/>\n&lt;p&gt;This&nbsp;is&nbsp;the&nbsp;second&nbsp;paragraph.&lt;\/p&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;&#8221;;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AppendAltChunk(doc,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;AltChunkId1&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html1);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AppendPageBreak(doc);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AppendAltChunk(doc,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;AltChunkId2&#8221;,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html2);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/p>\n<div style=\"clear:both;\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A question arose in the forums &#8211; when importing HTML content using altChunk, can you cause KeepLines \/ KeepNext to be set for imported paragraphs. This is not possible. You can control this when importing a WordprocessingML document using altChunk &#8211; you simply go into the imported document and set keepLines (section 17.3.1.14 in IS29500) [&hellip;]<\/p>\n","protected":false},"author":10567,"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-3042","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3042","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\/10567"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=3042"}],"version-history":[{"count":1,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3042\/revisions"}],"predecessor-version":[{"id":3043,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3042\/revisions\/3043"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=3042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}