Aligning Table Entry Content Horizontally

This example shows how to specify the horizontal alignment of table entries in a DOM Table and FormalTable. It illustrates setting the alignment of all table entries, a specific row of entries, and an individual entry.

Document and Data Setup

The following code sets up a document, table styles, and data to be used to create the example tables.

Import the DOM package so you do not have to use long, fully-qualified class names.

import mlreportgen.dom.*

Create and open a document. To create a Word document, change the output type from pdf to docx. To create an HTML document, change pdf to html or html-file for a multi-file or single-file document, respectively.

d = Document("mydoc", "pdf");
open(d);

Define table, header row, and footer row styles.

tableStyle = { Width("60%"), ...
               Border("solid"), ...
               RowSep("solid"), ...
               ColSep("solid") };

headerStyle = { BackgroundColor("LightBlue"), ...
                Bold(true) };

footerStyle = { BackgroundColor("LightCyan"), ...
                ColSep("none"), ...
                WhiteSpace("preserve") };

Define content for the table header row, body, and footer row. The footer row content and style are used only in the FormalTable in the last example.

headerContent = {'Item', 'Quantity', 'Cost'};
bodyContent = {'Table', 1, 55; ...
               'Chair', 4, 100; ...
               'Bookshelf', 2, 40};

total = sum([bodyContent{:,3}]);
footerContent = {[], 'Total ', total};

tableContent = [headerContent; bodyContent];

Setting Alignment of All Table Entries

In the first table of this example, all table entries are centered horizontally by setting a single table property.

Append a heading to the document and create a DOM Table with the content defined earlier. Assign styles to the table and the first row of the table.

append(d, Heading1("Setting Alignment of all Table Entries"));

table = Table(tableContent);
table.Style = tableStyle;

firstRow = table.Children(1);
firstRow.Style = headerStyle;

Set the horizontal alignment of all table entries by using the table's TableEntriesHAlign property. The property can be set to left, center, or right. Append the table to the document.

table.TableEntriesHAlign = "center";

append(d, table);

Setting Alignment of a Table Row

In the next table, top row's table entries are centered by modifying the row's Style property.

Append a heading to the document to describe the next table and create a DOM Table with the content defined earlier. Similar to the previous table, assign styles to the table and the first row of the table. However, instead of setting the horizontal alignment with the TableEntriesHAlign property, create an HAlign object and include it in the Style property of the first row along with the header style defined earlier.

append(d, Heading1("Setting Alignment of Top Row"));

table = Table(tableContent);
table.Style = tableStyle;

firstRow = table.Children(1);
firstRow.Style = [{HAlign("center")}, headerStyle];

append(d, table);

Note: The Style property of a table row overrides the table's TableEntriesHAlign setting for that specific row. The TableEntriesHAlign property still can be used to set the alignment for the rest of the table entries.

Setting Alignment of a Single Table Entry

The final table in this example uses a DOM FormalTable. In the table's footer, the Style property of a single entry is modified to set horizontal alignment.

Append a new heading to the document and create a DOM FormalTable. Set the styles of the overall table, header, and footer. The header of a FormalTable is separate from the body, so the alignment of the header entries can be set using the TableEntriesHAlign property of the header without affecting the body or footer entries. Alternatively, the previous method of adding an HAlign object to the row's Style property can be used.

append(d, Heading1("Setting Alignment of a Single Table Entry"));

table = FormalTable(headerContent, bodyContent, footerContent);
table.Style = tableStyle;

table.Header.TableEntriesHAlign = "center";
table.Header.Style = headerStyle;

footer = table.Footer;
footer.Style = footerStyle;

Set the 'Total' entry of the footer to have right horizontal alignment by including an HAlign object in the entry's Style property. To make the entry stand out more, add a Bold object.

totalEntry = footer.entry(1,2);
totalEntry.Style = {HAlign("right"), Bold(true)};

append(d, table);

Close and view the document.

close(d);
rptview(d);