Skip to main content

Helpcenter

Sub Level Items

A sub level item is an item that is inside another item (sub level items can have sub level items inside them. There is no limit to the number of levels of nested sub level items you can have). When a sub level item is pasted it is repeated in its place.

Sub Level Items 1 Exercise
  1. Open sub_level_items_1.docx in the DocxFactory/exercises/templates/ directory (see picture below).

    image12.png
  2. Open the Bookmark dialog box to view the items in the template.

    There are 3 bookmarks for the items in the template. Highlight each bookmark and press the Go To button to view the highlighted area (see picture below).

    image13.png
  3. Compile the template. You can also compile the template from the command-line.

    See example below.

$ word-processing-compiler <source file> <target file>

Note: There is also a word-processing-merger command line tool to merge templates and XML or JSON. To show the tools options, launch the tools with the --help option.

  1. 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/sub_level_items_1.dfw");

if (dfw_getErrorFlag()) goto ERROR;

dfw_paste("Account");

for (int i = 0; i < 3; i++)

{

dfw_paste("Line");

dfw_paste("Comment");

}

dfw_save("/tmp/sub_level_items_1.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;

}

In general, when pasting an item the item is added to the end of the document. When a top level item is pasted it is simply added to the end of the document. When a sub level item is pasted it is added inside its parent in the end of the document because a sub level item must be added inside its parent and cannot be added by itself.

More specifically, when a sub level item is pasted DocxFactory looks for the parent of the item being pasted starting from the last item pasted and going up through its parents, when the parent is found, the item is added inside the parent.

With the following exceptions, if the item is not a direct child of the parent then all the item missing parents are first pasted before pasting the item. Similarly, if no parent was found except for the document then all the item parents upto the top level item are first pasted before pasting the item.

In our example, if you pasted the Line item without first pasting its Account parent item then DocxFactory would first paste its missing Account parent item and then paste the Line item.

  1. Open the created .DOCX file.

    As you can see, every sub level item is repeated in its place (see picture below).

    image14.png
Sub Level Items 2 Exercise
Item Groups

In the previous exercise, every sub level item was repeated in its own place separately from each other but what if you want to mix them together?

Items placed together one right after the other with nothing between them are called an Item Group. Items in the same group can be mixed together.

  1. Open sub_level_items_2.docx in the DocxFactory/exercises/templates/ directory (see picture below).

    image15.png
  2. Open the Bookmark dialog box to view the items in the template.

    Select sort by Location at the bottom of the dialog box and highlight the Deposit, Withdraw and Comment bookmarks. You can see that the Deposit, Withdraw highlighted areas are placed one right after the other with nothing between them. The Comment bookmark is placed separately with a space separating it from the other bookmarks (see picture below).

    image16.png
  3. Compile the template.

  4. 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/sub_level_items_2.dfw");

if (dfw_getErrorFlag()) goto ERROR;

dfw_paste("Account");

for (int i = 0; i < 3; i++)

{

dfw_paste("Deposit");

dfw_paste("Withdraw");

dfw_paste("Comment");

}

dfw_save("/tmp/sub_level_items_2.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;

}

  1. Open the created .DOCX file.

    As you can see, the Deposit and Withdraw items are mixed together in the order they were pasted and the Comment items are still separate from the other items (see picture below).

    image17.png