Main Content

Format Inheritance

The DOM API allows you to use template-based styles and format object-based styles (or equivalent format properties) to specify the appearance of an object. If you set the StyleName and the Style property of an object, the formats in the Style property override corresponding formats specified by the template-based style of the StyleName property. Consider, for example, this code.

import mlreportgen.dom.*;
d = Document('MyDoc','docx','MyTemplate');
p = Paragraph('Danger!');
p.StyleName = 'Warning';
p.Style = {Color('red')};
append(d,p);
close(d);

Suppose that the Warning style defines the color of a warning as yellow. In that case, the setting of the Style property on the paragraph overrides the color specified by the StyleName setting.

If a document object does not specify a value for StyleName, it inherits any formats that it does not specify from its container. The container inherits any formats that it does not specify from its container, and so on, all the way to the top of a container hierarchy. Format inheritance allows you to use a single statement to assign a format for all the objects contained by a container. For example, this code uses a single Style property to assign a color to all the entries in a table.

import mlreportgen.dom.*;
d = Document('MyDoc');
tableArray = {'a','b';'c','d'};
table = append(d,tableArray);
table.Style = {Color('blue')};
close(d);
rptview(d.OutputPath);

Related Topics