Sections
In Microsoft Word, documents can be divided into sections and you can set a different page size and page orientation (but also other settings like header and footer) for every section. To divide a document into sections, insert a section break (which ends the previous section and starts a new section).
A single page cannot have two or more sections because every section has a different page type (page size and page orientation) so a section break (which ends the previous section and starts a new section) is also a page break. Just like regular page breaks, items should be placed between section breaks (or regular page breaks) and section breaks should not be placed inside items (see the chapter on top level items for a detailed explanation).
When designing a template, you set in what section the page the top level item starts is in. When creating a new document, if a new page is started when pasting a top level item that is in a different section than the last page then a section break is inserted and the same section page size and page orientation as the new page section are set. So the page the top level item starts is the same page type (page size and page orientation) in the created document and the template no matter what order the top level item is pasted.
Sections Exercise
Open sections.docx in the DocxFactory/exercises/templates/ directory.
Open the Bookmark dialog box to view the items in the template. There are 2 top level items: Page1 and Page2 (see picture below).
Insert a section break between the two items.
First place the cursor at the beginning of the second item. Then select Next Page, under Section Breaks, in the Breaks drop down menu, in the Page Layout ribbon (see picture below).
As you can see, a section break is also a page break.
Change the section page orientation to landscape.
First make sure the cursor is on the second page. Then select Landscape, in the Orientation drop down menu, in the Page Layout ribbon (see picture below).
Compile the template.
Create the .DOCX file.
Copy and run the code below.
#include "dfw.h" #include <stdio.h> #include <time.h> int main() { time_t l_start = clock(); dfw_load("/opt/DocxFactory/exercises/templates/sections.dfw"); if (dfw_getErrorFlag()) goto ERROR; dfw_paste("Page2"); dfw_paste("Page1"); dfw_paste("Page2"); dfw_paste("Page1"); dfw_save("/tmp/sections.docx"); if (dfw_getErrorFlag()) goto ERROR; printf("Completed (in %.3f seconds).\n", (double) (clock() - l_start) / CLOCKS_PER_SEC); return 0; ERROR: printf("%s\n", dfw_getErrorWhat()); return 1; } |
Open the created .DOCX file.
As you can see, the page the top level item starts is the same page type (page size and page orientation) in the created document and the template no matter what order the top level item is pasted (see picture below).