This sample shows how to print a report from Java to the specified printer (Swing).

First, create the JFrame and set the necessary options:
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;
}

In the screenshot below you can see the result of the sample code:

Printing the Report Template from Code

By using this website, you agree to the use of cookies for analytics and personalized content. Cookies store useful information on your computer to help us improve efficiency and usability. For more information, please read the privacy policy and cookie policy.