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
Open sub_level_items_1.docx in the DocxFactory/exercises/templates/ directory (see picture below).
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).
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.
Create the .DOCX file.
Copy and run the code below.
using System; using System.Diagnostics; using DocxFactory; public class Runme { public static void Main() { try { var stopwatch = Stopwatch.StartNew(); WordProcessingMerger.Load( "C:/Program Files/DocxFactory/exercises/templates/sub_level_items_1.dfw"); WordProcessingMerger.Paste("Account"); for (int i = 0; i < 3; i++) { WordProcessingMerger.Paste("Line"); WordProcessingMerger.Paste("Comment"); } WordProcessingMerger.Save("C:/temp/sub_level_items_1.docx"); Console.WriteLine( "Completed (in {0:F3} seconds).", (double) stopwatch.ElapsedMilliseconds / 1000); } catch (Exception e) { Console.WriteLine(e); } } } |
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.
Open the created .DOCX file.
As you can see, every sub level item is repeated in its place (see picture below).
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.
Open sub_level_items_2.docx in the DocxFactory/exercises/templates/ directory (see picture below).
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).
Compile the template.
Create the .DOCX file.
Copy and run the code below.
using System; using System.Diagnostics; using DocxFactory; public class Runme { public static void Main() { try { var stopwatch = Stopwatch.StartNew(); WordProcessingMerger.Load( "C:/Program Files/DocxFactory/exercises/templates/sub_level_items_2.dfw"); WordProcessingMerger.Paste("Account"); for (int i = 0; i < 3; i++) { WordProcessingMerger.Paste("Deposit"); WordProcessingMerger.Paste("Withdraw"); WordProcessingMerger.Paste("Comment"); } WordProcessingMerger.Save("C:/temp/sub_level_items_2.docx"); Console.WriteLine( "Completed (in {0:F3} seconds).", (double) stopwatch.ElapsedMilliseconds / 1000); } catch (Exception e) { Console.WriteLine(e); } } } |
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).