HOME > SUPPORT > TEMPLATE MANIPULATION

Template Manipulation

 

Overview

ReportMill's goal is to make report generation a matter of template configuration, not programming, and thus we strive to solve all reporting problems by continually improving the layout application. However, the ReportMill graphics library is the most powerful Java web graphics API available, and it's inevitable that there will always be interesting things that can be done with this API directly. Even so, we highly recommend avoiding this functionality and sticking with the Basic API.

The basic idea is that instead of simply loading a template with the RMDocument() constructor and calling generateReport, a developer would either create a template programmatically or modify an existing template. The objects involved are pretty intuitive and correspond to the elements a developer sees in the layout application. There is an RMDocument to represent the template. The RMDocument has a set of RMPage objects, representing the document pages. And each page has a list of children that can be RMText, RMImage and RMTable (among many others). Complex shapes like RMTable have children like RMTableRow.

The JavaDoc is available here for hard-core users.

Example 1 - Modifying an existing template

Here is a basic example that loads a simple template and reconfigures an empty table to contain specific keys, defining specific attributes and relative column sizes. The template for this code is a plain template with a basic table created by simply hitting the "Add Table" button in the layout app.

// Get template and customize a table row
RMDocument template = new RMDocument("MyTemplate.rpt");
RMPage page = template.getPage(0);
RMTable table = (RMTable)page.getChildWithClass(RMTable.class); // or use childWithName()
RMTableRow row = (RMTableRow)table.getChildWithTitle("Objects Details");
row.setNumberOfColumns(3);
row.getColumn(0).setText("@title@");
row.getColumn(0).setFont(RMFont.getFont("Arial Bold", 12));
row.getColumn(0).setTextColor(new RMColor(1,0,0));
row.getColumn(1).setText("@revenue@");
row.getColumn(2).setText("@category@");

// Set relative sizes and arrange
row.getColumn(0).setWidth(2);
row.getColumn(1).setWidth(1);
row.getColumn(2).setWidth(1);
row.arrange();

// Generate Report
Map map = new RMXMLReader().read("BundleResource:examples/HollywoodDB.xml");
RMDocument report = template.generateReport(map.get("Movies"));
report.writePDF("MyReport.pdf");

Example 2 - Creating a template dynamically

// Create new 8.5" x 11" template (or 612 x 792 printers points - 1/72 in)
RMDocument template = new RMDocument(612, 792);
RMPage page = template.getPage(0);

// Create table, position and size and add to page
RMTable table = new RMTable();
table.setBounds(36, 36, 540, 720);
page.addChild(table);

// Add header row, set number of colums to 3 and configure
RMTableRow hrow = table.addHeader("objects");
hrow.setNumberOfColumns(3);
hrow.getColumn(0).setText("Title");
hrow.getColumn(1).setText("Show Date");
hrow.getColumn(2).setText("Revenue");

// Add details row, set number of columns to 3 and configure
RMTableRow drow = table.addDetails("objects");
drow.setNumberOfColumns(3);
drow.getColumn(0).setText("@getTitle@");
drow.getColumn(1).setText("@getShowDate@");
drow.getColumn(2).setText("@getRevenue@");

// Set something to a specific font and size
RMFont font = RMFont.getFont("Arial Bold", 12);
hrow.getColumn(0).getXString().addAttribute(font);

// Set something to align right
drow.getColumn(2).setAlign(RMText.ALIGN_RIGHT);

// Set something to have a formatter
RMFormat format = new RMNumberFormat("$ #,##0.00");
drow.getColumn(2).getXString().addAttribute(format);

// Add a light gray Alternate version
drow.setVersion("Alternate");
drow.setColor(RMColor.lightGray);

// Throw some movies at new template and write PDF
Map hollywood = new RMXMLReader().readObject("BundleResource:examples/HollywoodDB.xml");
List movies = (List)hollywood.get("Movies");
template.generateReport(movies).write("/tmp/Movies.pdf");