Alternating Colors
Items marked as alternating color in the template will alternate between two colors in the new document.
The two alternating colors are taken from the color of the first table cell in the first table row of the first and second items in the group (alternating colors is currently only supported for tables). If there is only one item then the second color is no color.
The items in the group (that are marked as alternating color) color will alternate between the two colors in the new document.
Alternating Colors Exercise
Open alternating_colors.docx in the DocxFactory/exercises/templates/ directory (see picture below).
Note: Continuing from the previous chapter, the template has a dummy item to separate between the Withdraw item and the report summary.
Add _alternate to the bookmark name to mark the item as alternating color.
You cannot change a bookmark name so you will need to delete and re-enter the bookmark with a different name.
In the bookmark Dialog box, highlight the bookmark by selecting the bookmark and pressing the Go To.
Delete the bookmark with the Delete button.
Re-enter the bookmark name and add _alternate to the name. Press the Add button to add the bookmark for the highlighted area (see picture below).
Change the color of the second item so the color of the first and second items is different.
First, highlight the Withdraw item (you can either highlight the area manually or using the Bookmark dialog box). Then select a light gray from the Shading drop down menu in the Design ribbon (see picture below).
The exercise introduces the alternate feature (see details below).
_alternate
_alternate marks an item as alternating color.
Note: Item features are written to the end of the bookmark name.
Note: The features in the bookmark name are removed from the item name. For example: The bookmark "MyItem_alternate" item name is "MyItem".
Note: The features in the bookmark name like the item names are not case sensitive. For example: "_alternate" and "_Alternate" refer to the same feature.
Compile the template.
Create the .DOCX file.
Copy and run the code below.
#include "WordProcessingMerger.h" #include <exception> #include <iostream> #include <ctime> using namespace DocxFactory; using namespace std; int main() { try { WordProcessingMerger& l_merger = WordProcessingMerger::getInstance(); time_t l_start = clock(); l_merger.load( "/opt/DocxFactory/exercises/templates/alternating_colors.dfw"); l_merger.paste("Account"); for (int i = 0; i < 3; i++) { l_merger.paste("Deposit"); } for (int i = 0; i < 3; i++) { l_merger.paste("Withdraw"); } l_merger.save("/tmp/alternating_colors.docx"); cout<< "Completed (in " << (double) (clock() - l_start) / CLOCKS_PER_SEC << " seconds)." << endl; } catch (const exception& p_exception) { cout << p_exception.what() << endl; } } |
Open the created .DOCX file.
As you can see, in the new document the color of the items in the group alternates between the two colors (see picture below).