====== Jasper Report ====== Jasper report can help us to export a pdf file in Java. To make it simple, we will only focus on data source with ''JRBeanCollectionDataSource'', and use only the ''Text Field'', and ''Static Text'' basic elements to build this report. ===== Code ===== Assume we have already created a jasper template''japser_template.jasper''. In this example, we avoid to use the database data source, and use a ''JRBeanCollectionDataSource''. JRBeanCollectionDataSource would take a list of Java bean object, we can sort of use it like the returning list from a SQL query. Here is a simple code for doing so: public class MyBean{ private String item1; private String item2; public MyBean(String i1, String i2){ item1 = i1; item2 = i2 } public String getItem1(){return item1;} public String getItem2(){return item2;} } public class Test { public static void main(String[] args) throws Exception { FileInputStream main = new FileInputStream("./jasper_template.jasper"); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(main); Map params = new HashMap(); params.put("param1", "Something..."); params.put("param2", "Something Else..."); List myList = new ArrayList(); myList.add(new MyBean("1,1", "1,2")); myList.add(new MyBean("2,1", "2,2")); myList.add(new MyBean("3,1", "3,2")); myList.add(new MyBean("4,1", "4,2")); JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(myList); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, beanCollectionDataSource); byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint); FileOutputStream out = new FileOutputStream("./out.pdf"); out.write(pdfBytes); out.flush(); out.close(); } ===== Jasper Studio ===== We use Jasper Studio to create a jasper template. {{ :java:jasper1.png?direct&400 |}} Select a blank template, and then give your project a name. {{ :java:jasper2.png?direct&400 |}} Then select one empty record for data adapter. {{ :java:jasper3.png?direct&400 |}} In Column Header part, add two ''Static Text'' elements, and change their captions to anything you like. Then resize static text, and the column header height to make it looks nice. {{ :java:jasper4.png?direct&400 |}} Open ''Dataset and Query Dialog'', and then go to Java Bean tab. In The bottom, open ''Fields'' tab (should be opened by default), add two fields. Name ''item1'', and ''item2'', which will be the java bean field names we use in the code. {{ :java:jasper5.png?direct&400 |}} Then go to the ''Parameters'' tab, add two parameters. We call it param1, and param2 to match our code. You can pass, add as many as you need in your case. {{ :java:jasper6.png?direct&400 |}} In the template, add two ''Text Field'' in the Detail part. For the first one, double click in it, and select the ''item1'' fields we added previous. You will see it caption becomes ''$F{item1}''. Likewise, do the same thing for item2. {{ :java:jasper7.png?direct&400 |}} For parameters, we can use ''Text Field'' to display them. Add a text field on the template, and double clicks on it. Select the param1 we added in the previous steps. You will see it caption become ''$P{param1}'' Save the template, and compile it. You will notice that a new file with your template name and with extension .jasper is created. You can copy this file to your java project. In our code, we call this file ''jasper_template.jasper''. Copy it to root direct to your code. Run the code, and there would be a out.pdf file created. The list of java bean should be displayed on such pdf file.