Run Jasper report and export as pdf from Java Swing application

0
I have made a Java Swing application which produces an xml file with data from a mysql database.
Using that I have successfully made a Jasper (jrxml) report using the Jasper 5.5.0 plugin for NetBeans separately from my Swing application.
To do this I use the prepared xml file as datasource. So I have Jasper report with an xml datasource and one subreport. How can I run that .jasper report from my Swing application and export as a pdf file? As a final message box from my Swing app I get:
"XML file is successfully generated"
 
I want to make one button on that message box with option:
"Print xml to pdf file"
 
How can I run my Jasper report and export as a pdf from Java class?
 
I have searched for examples using Google, but I can't find anything useful.
duka.milan's picture
Joined: Dec 17 2013 - 1:54pm
Last seen: 5 years 5 months ago

1 Answer:

1

I found solution:

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperExportManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.JasperReportsContext;

import net.sf.jasperreports.engine.data.JRXmlDataSource;

public static void main(String[] args){

try { HashMap hm = new HashMap<>(); ,>

String filePath=System.getProperty("user.dir") + "\\somexmldatasource.xml"; ,>

InputStream inputStream = new FileInputStream(new File(filePath)); ,>

JRXmlDataSource ds=new JRXmlDataSource(inputStream,"/some/xpath/query"); ,>

JasperReport jasperReport; ,>

JasperPrint jasperPrint; ,>

jasperReport = JasperCompileManager.compileReport(System.getProperty("user.dir")+"\\yourreport.jrxml");,>

jasperPrint = JasperFillManager.fillReport(jasperReport, hm, ds); ,>

JasperExportManager.exportReportToPdfFile(jasperPrint, "someoutputpath\\simple_report.pdf"); ,>

} catch (Exception e) { e.printStackTrace(); },>

} ,>

for using Jasper in java you must include several jars from c:\Program Files (x86)\Jaspersoft\iReport-5.5.0\ireport\modules\ext\ (or some other path of iReport app): commons-collections-3.2.1.jar, commons-digester-2.1.jar, commons-logging-1.1.jar, jasperreports-5.5.0.jar, xml-apis.jar, commons-beanutils-1.8.2.jar, servlet-api-2.4.jar and iText-2.1.7.js2.jar,>

duka.milan's picture
Joined: Dec 17 2013 - 1:54pm
Last seen: 5 years 5 months ago

Hi,
Thanks for sharing the way out. I wonder whether the PDF processing tools can help with the related work or not? I am testing about the related projects these days.
http://www.yiigo.com/guides/vbnet/how-to-process-pdf.shtml
http://www.yiigo.com/guides/csharp/how-to-process-pdf.shtml
Do you have any ideas about it? Or any good suggestion? Thanks in advance.

Best regards,
Arron

arronwall1 - 5 years 4 months ago
Feedback
randomness