Printing the Report Template from Code
Our sample projects and report templates can help you learn the basics of working with our products.Render the report in Java and start the print workflow with settings supplied by the application.
First, create the
Printing from code. Load and render the report, apply the printer settings, and start the print workflow from application code.
Next, add the print button on the main panel. As the action, we define the print report to the default printer:
Next, add the elements for selecting the printer and the button for printing the report to the selected printer:
Also we need to load the report for printing. We use the 'SimpleList' report - load it and add a Demo database to report object. After these actions render the report:
First, create the
JFrame and set the necessary options:Printing from code. Load and render the report, apply the printer settings, and start the print workflow from application code.
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new Printing(frame));
frame.setSize(FRAME_SIZE);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} catch (Throwable e) {
StiExceptionProvider.show(e, null);
}
}
});
}Next, add the print button on the main panel. As the action, we define the print report to the default printer:
JButton button = new JButton("Print");
leftPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StiReport report = getReport();
PrinterJob printerJob = StiPrintHelper.preparePrinterJob(report.getRenderedPages());
try {
StiPrintHelper.printJob(printerJob, report, true);
} catch (PrinterException pe) {
StiExceptionProvider.show(pe, null);
}
}
});Next, add the elements for selecting the printer and the button for printing the report to the selected printer:
final JComboBox printerList = new JComboBox(PrintServiceLookup.lookupPrintServices(null, null));
rightPanel.add(printerList);
printerList.setSelectedIndex(0);
button = new JButton("Print");
rightPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StiReport report = getReport();
PrinterJob printerJob = StiPrintHelper.preparePrinterJob(report.getRenderedPages());
try {
AttributeSet attr_set = new HashAttributeSet();
PrintService printService = (PrintService) printerList.getSelectedItem();
attr_set.add(new PrinterName(printService.getName(), null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attr_set);
printerJob.setPrintService(services[0]);
StiPrintHelper.printJob(printerJob, report);
} catch (Exception e1) {
StiExceptionProvider.show(e1, parentFrame);
}
}
});Also we need to load the report for printing. We use the 'SimpleList' report - load it and add a Demo database to report object. After these actions render the report:
private StiReport getReport() {
if (report == null) {
try {
String demoDir = "Data/";
StiXmlDatabase xmlDatabase = new StiXmlDatabase("Demo", demoDir + "Demo.xsd", demoDir + "Demo.xml");
StiReport renderReport = StiSerializeManager.deserializeReport(new File("Reports/SimpleList.mrt"));
renderReport.getDictionary().getDatabases().add(xmlDatabase);
renderReport.setCalculationMode(StiCalculationMode.Interpretation);
renderReport.Render(false);
report = renderReport;
} catch (Exception e) {
StiExceptionProvider.show(e, null);
}
}
return report;
}