Top Level Items
Top level items are items that are not inside any other item. When a top level item is pasted it starts in a new page.
Top Level Items Exercise
The following exercises in this tutorial walk you through creating a Bank Account Activity Report.
Open top_level_items.docx in the DocxFactory/exercises/templates/ directory (see picture below).
The recommended way to divide your layout into parts, in Microsoft Word, is using tables without borders. The table only purpose is layout and is completely hidden.
To see the table, click inside the table and the Design and Layout ribbons will show. In the Layout ribbon, select View Gridlines (see picture below).
To create an item, first highlight the area of the item.
Highlight the entire report (see picture below).
Then insert a bookmark for the highlighted area with the name of the item.
In the Insert ribbon, press the Bookmark button to open the Bookmark dialog box. In the Bookmark name, type “Account” and press the Add button (see picture below).
Note: A table cell cannot be marked as an item so it will not be possible to create a table with rows that have a different number of cells. A table row is the smallest part of a table that can be marked as an item. If a table cell is marked as an item then the area is expanded to mark the whole table row. Future versions will support marking columns for horizontal pasting.
Note: In most cases, you’ll find it easier if you create items that map closely to your data structure. For example: if you have a Bank Account table with the Bank Account’s data, you can create a Bank Account item with the Bank Account’s fields.
A top level item should start after the previous page break (or the start of the document) and end right before the next page break (or the end of the document) where the next top level item starts (see picture below).
For 2 reasons: 1. The start of a new page at the start of the top level item in the template is the start of a new page when the top level item is pasted in the new document. 2. Everything in the template should be in an item.
Even though you can: not put everything in items, put more than one top level item between page breaks, put page breaks in the middle of an item or not put any items in the template this is not the intended use and although the template will still compile and the program will still run you may not get the results you intended.
Before you can use the template you must compile the .DOCX file to a .DFW file - (D)ocx (F)actory (W)ordprocessing template file.
Copy and run the code below.
#include "dfwc.h" #include <stdio.h> #include <time.h> int main() { time_t l_start = clock(); dfwc_compile( "/opt/DocxFactory/exercises/templates/top_level_items.docx", "/opt/DocxFactory/exercises/templates/top_level_items.dfw"); if (dfwc_getErrorFlag()) goto ERROR; printf("Completed (in %.3f seconds).\n", (double) (clock() - l_start) / CLOCKS_PER_SEC); return 0; ERROR: printf("%s\n", dfwc_getErrorWhat()); return 1; } |
The code compiles the template file. If the template is successfully compiled then the time it took to compile is displayed. If an exception is thrown then the description of the exception is displayed.
Note: The code in the exercises uses the Linux installation directories for running on Linux. To run the code on Windows, use the Windows installation directories.
Note: You can get the source .DOCX file from the compiled .DFW file, in case the .DOCX file is lost. The .DFW file is actually a .ZIP file. You can open the .ZIP file (on Windows you can add a .ZIP extension to the end of the file name and open the .ZIP file with the Windows Explorer) and find the .DOCX file inside in the root directory with a # prefix.
The code introduces the dfwc.h include file, its dfwc_compile function and error handling functions (see details below).
dfwc.h Include File
The DocxFactory Word Processing Compiler Include File.
Header File:
#include "dfwc.h"
Functions:
dfwc_compile |
|
---|---|
dfwc_setTempDir |
|
dfwc_getTempDir |
|
dfwc_getWorkDir |
|
dfwc_getErrorWhat |
|
dfwc_getErrorFlag |
|
dfwc_compile Function
Compiles a .DOCX file to a .DFW file.
Declaration:
unsigned char dfwc_compile(
const char* p_sourceFile, const char* p_targetFile);
Parameters:
p_sourceFile - The source .DOCX file.
p_targetFile – The target .DFW file.
Notes:
Relative paths are resolved relative to the current working directory. For example: “dir/template.docx” is resolved to “[working directory]/dir/template.docx”.
The target file must end with a “.dfw” file extension. If it does not then a “.dfw” file extension is added to the end of the file name.
If the target file is blank then the source file is used.
dfwc_getErrorFlag Function
If an error occurred in the last function call then returns true.
Declaration:
unsigned char dfwc_getErrorFlag();
dfwc_getErrorWhat Function
If an error occurred in the last function call then returns the error description.
Declaration:
const char* dfwc_getErrorWhat();
Create a new document from the template we created, paste the top level item and save 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/top_level_items.dfw"); if (dfw_getErrorFlag()) goto ERROR; for (int i = 0; i < 3; i++) { dfw_paste("Account"); } dfw_save("/tmp/top_level_items.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; } |
The code introduces the dfw.h include file and its dfw_load, dfw_paste and dfw_save functions (see details below).
dfw.h Include File
The DocxFactory Word Processing Merger Include File.
Header File:
#include "dfw.h"
Functions:
dfw_load |
|
---|---|
dfw_save |
|
dfw_print |
|
dfw_close |
|
dfw_setClipboardValueByStr |
|
dfw_setClipboardValueByDouble |
|
dfw_setChartValueByStrStr |
|
dfw_setChartValueByStrDouble |
|
dfw_setChartValueByDoubleDouble |
|
dfw_paste |
|
dfw_merge |
|
dfw_getItems |
|
dfw_getFields |
|
dfw_getItemFields |
|
dfw_getItemParent |
|
dfw_setLocale |
|
dfw_setCodePage |
|
dfw_setNumFracSep |
|
dfw_setNumThSep |
|
dfw_setDateFormat |
|
dfw_setYearOffset |
|
dfw_setFirstWeekDay |
|
dfw_getLocale |
|
dfw_getCodePage |
|
dfw_getNumFracSep |
|
dfw_getNumThSep |
|
dfw_getDateFormat |
|
dfw_getYearOffset |
|
dfw_getFirstWeekDay |
|
dfw_setTempDir |
|
dfw_getTempDir |
|
dfw_getWorkDir |
|
dfw_printFile |
|
dfw_getErrorWhat |
|
dfw_getErrorFlag |
|
dfw_load Function
Creates a new document and cuts all the template items into the DocxFactory clipboard.
Declaration:
unsigned char dfw_load(const string& p_templateFile);
Parameters:
p_templateFile - The template .DFW file.
Notes:
Relative paths are resolved relative to the current working directory. For example: “dir/template.dfw” is resolved to “[working directory]/dir/template.dfw”.
dfw_paste Function
Pastes an item from the DocxFactory clipboard with all its field values on to the new document.
Declaration:
unsigned char dfw_paste(const char* p_itemName);
Parameters:
p_itemName – The item name.
Notes:
The item names are not case sensitive. For example: “Account” and “account” refer to the same item.
dfw_save Function
Saves the new document out to a file. After saving, the document is closed and no more data can be merged until a new template is loaded.
Declaration:
unsigned char save(const char* p_targetFile);
Parameters:
p_targetFile – The target file.
Notes:
If the target file is not a .DOCX file (for example: .PDF file) then DocxFactory first saves the document as a .DOCX file and then converts the file to the target file.
Relative paths are resolved relative to the current working directory. For example: “dir/template.docx” is resolved to “[working directory]/dir/template.docx”.
Open the created .DOCX file.
As you can see, every top level item starts in a new page (see picture below).
Save as .PDF file.
In your code, change the save file to "/tmp/top_level_items.pdf". Run and open the file (see picture below).
Note: DocxFactory can only create .DOCX files directly. To create .PDF, .HTML or other file formats, DocxFactory first creates a .DOCX file and then converts the file using conversion tools. Likewise to print, DocxFactory uses printing tools.
You must have printing and conversion tools installed to print and convert to other file formats (see the chapter on printing and conversion tools installation in the beginning for details).