<html xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://jasperreports.sourceforge.net/jasperreports"> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JasperReports 4.0.2 - No XML Design Sample</title> <style type="text/css"> .title { font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 28px; font-weight: normal; } .toc { font-family: Courier New, Courier, serif; font-size: 12px; font-weight: normal; } .name { font-family: Courier New, Courier, serif; font-size: 16px; font-weight: bold; } .label { font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; font-weight: bold; font-style: italic; } .description { font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; font-weight: normal; } .value { font-family: Courier New, Courier, serif; font-size: 12px; font-weight: normal; } .element { font-family: Courier New, Courier, serif; font-size: 12px; font-weight: normal; } .attribute { font-family: Courier New, Courier, serif; font-size: 12px; font-weight: bold; } .code { font-family: Courier New, Courier, serif; font-size: 12px; font-weight: normal; } .copy { font-decoration: none; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; font-style: normal; color: #000000; } .subtitle { font-family: inherit; font-size: inherit; font-style: inherit; font-weight: bold; text-decoration: none; color: inherit; } </style> </head> <body bgcolor="#FFFFFF"> <a name="top"></a> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td colspan="2" align="right"><span class="element"><a href="../../sample.reference.html">Sample Reference</a> - <a href="../../schema.reference.html">Schema Reference</a> - <a href="../../config.reference.html">Configuration Reference</a> - <a href="http://jasperreports.sourceforge.net/api/index.html">API (Javadoc)</a></span> <br> </td> </tr> <tr> <td colspan="2"> <hr size="1"> </td> </tr> <tr valign="middle"> <td nowrap="true"><span class="title">JasperReports - No XML Design Sample (version 4.0.2)</span></td><td align="right"><img src="../../resources/jasperreports.png" border="0"></td> </tr> <tr> <td colspan="2"> <hr size="1"> </td> </tr> </table> <br> <span class="description"><span class="description">Shows how in-memory JasperDesign objects can be created without using the JRXML design template, but only the API directly.</span></span> <br> <br> <span class="element"><a href="http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.0.2/jasperreports-4.0.2-project.zip/download" target="_blank">Download All Sample Source Files</a></span> <br> <span class="element"><a href="http://jasperforge.org/scm/viewvc.php/tags/jr-4-0-2/jasperreports/demo/samples/noxmldesign/?root=jasperreports" target="_blank">Browse Sample Source Files on SVN</a></span> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td style="width: 20px;"> <br> </td><td> <br> </td> </tr> <tr> <td colspan="2"><span class="label">Main Features in This Sample</span></td> </tr> <tr> <td> <br> </td><td><span class="element"><a href="#noxmldesign">Creating In-Memory JasperDesign Objects Using the API Directly (Without JRXML Report Templates)</a></span></td> </tr> </table> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td><img src="../../resources/px.gif" border="0" width="20" height="1"></td><td width="80%"> <br> </td><td width="20%"> <br> </td> </tr> <tr> <td colspan="6" align="right"><a name="noxmldesign"></a><a href="#top" class="toc">top</a></td> </tr> <tr> <td colspan="6"> <hr size="1"> </td> </tr> <tr valign="top"> <td><img src="../../resources/jr-16x16.png" border="0"></td><td colspan="4"><span class="name">Creating In-Memory JasperDesign Objects Using the API Directly (Without JRXML Report Templates)</span></td><td align="right"><span class="copy">Documented by <a href="mailto:shertage@users.sourceforge.net" class="copy">Sanda Zaharia</a></span></td> </tr> <tr> <td colspan="6"> <br> </td> </tr> <tr valign="top"> <td> <br> </td><td nowrap="true"><span class="label">Description / Goal</span></td><td> <br> </td><td colspan="3"><span class="description"> How to create in-memory JasperDesign objects without processing a JRXML file, using the object model of the JasperReports API. </span></td> </tr> <tr valign="top"> <td> <br> </td><td colspan="1"><span class="label">Since</span></td><td> <br> </td><td colspan="3"><span class="description">0.4.2</span></td> </tr> <tr> <td colspan="6"> <br> </td> </tr> <tr> <td> <br> </td><td colspan="5"><span class="description"> <b>The JasperDesign Object</b> <br> <br> Standard in reporting applications use report templates to define the layout of generated documents. To start a report generation process a report template is needed. Once created, a report template can be filled with appropriate data and generate output document objects during the report generation. <br> In JasperReports the report generation relies on creating, compiling and filling report templates. <br> Report templates are stored in <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> objects. Instances of this class are the raw material that the engine uses to generate reports. <br> During the report generation a report template object is compiled and completed with additional information about the runtime expressions evaluation into a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperReport.html" target="_blank">JasperReport</a></span> object, then is filled with significant data creating a ready-to-print pixel-perfect <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object. <br> One can obtain <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> instances either by parsing the JRXML report template files, or independently, through API calls, where using JRXML files is not a possibility. The main purpose is to make available an in-memory <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> object with all its properties set, in order to generate a valid report. <br> <br> Taking a look inside the <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> class, one can see that it extends <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/base/JRBaseReport.html" target="_blank">JRBaseReport</a></span> and inherits all protected member properties below: <pre> protected String name; protected String language = LANGUAGE_JAVA; protected int columnCount = 1; protected PrintOrderEnum printOrderValue = PrintOrderEnum.VERTICAL; protected RunDirectionEnum columnDirection = RunDirectionEnum.LTR; protected int pageWidth = 595; protected int pageHeight = 842; protected OrientationEnum orientationValue = OrientationEnum.PORTRAIT; protected WhenNoDataTypeEnum whenNoDataTypeValue = WhenNoDataTypeEnum.NO_PAGES; protected int columnWidth = 555; protected int columnSpacing; protected int leftMargin = 20; protected int rightMargin = 20; protected int topMargin = 30; protected int bottomMargin = 30; protected boolean isTitleNewPage; protected boolean isSummaryNewPage; protected boolean isSummaryWithPageHeaderAndFooter; protected boolean isFloatColumnFooter; protected boolean ignorePagination; /** * */ protected String formatFactoryClass; /** * */ protected Set importsSet; /** * Report templates. */ protected JRReportTemplate[] templates; protected JRReportFont defaultFont; protected JRReportFont[] fonts; protected JRStyle defaultStyle; protected JRStyle[] styles; /** * The main dataset of the report. */ protected JRDataset mainDataset; /** * Sub datasets of the report. */ protected JRDataset[] datasets; protected JRBand background; protected JRBand title; protected JRBand pageHeader; protected JRBand columnHeader; protected JRSection detailSection; protected JRBand columnFooter; protected JRBand pageFooter; protected JRBand lastPageFooter; protected JRBand summary; protected JRBand noData;</pre> Therefore, creating a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> instance may be accomplished by creating an empty <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> object by calling the default constructor, and then applying setter methods to its members with relevance in the given report. <br> <br> <b>The No XML Design Sample</b> <br> <br> This sample shows how to create an in-memory <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> instance. The sample doesn't contain any JRXML template file. The <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> object is hardcoded in the <span class="code"><code>src/NoXmlDesignApp.java</code></span> file (see the <span class="code"><code>getJasperDesign()</code></span> method). <pre> private static JasperDesign getJasperDesign() throws JRException { //JasperDesign JasperDesign jasperDesign = new JasperDesign(); jasperDesign.setName("NoXmlDesignReport"); jasperDesign.setPageWidth(595); jasperDesign.setPageHeight(842); jasperDesign.setColumnWidth(515); jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(40); jasperDesign.setRightMargin(40); jasperDesign.setTopMargin(50); jasperDesign.setBottomMargin(50); //Fonts JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("Sans_Normal"); normalStyle.setDefault(true); normalStyle.setFontName("DejaVu Sans"); normalStyle.setFontSize(12); normalStyle.setPdfFontName("Helvetica"); normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false); jasperDesign.addStyle(normalStyle); JRDesignStyle boldStyle = new JRDesignStyle(); boldStyle.setName("Sans_Bold"); boldStyle.setFontName("DejaVu Sans"); boldStyle.setFontSize(12); boldStyle.setBold(true); boldStyle.setPdfFontName("Helvetica-Bold"); boldStyle.setPdfEncoding("Cp1252"); boldStyle.setPdfEmbedded(false); jasperDesign.addStyle(boldStyle); JRDesignStyle italicStyle = new JRDesignStyle(); italicStyle.setName("Sans_Italic"); italicStyle.setFontName("DejaVu Sans"); italicStyle.setFontSize(12); italicStyle.setItalic(true); italicStyle.setPdfFontName("Helvetica-Oblique"); italicStyle.setPdfEncoding("Cp1252"); italicStyle.setPdfEmbedded(false); jasperDesign.addStyle(italicStyle); //Parameters JRDesignParameter parameter = new JRDesignParameter(); parameter.setName("ReportTitle"); parameter.setValueClass(java.lang.String.class); jasperDesign.addParameter(parameter); parameter = new JRDesignParameter(); parameter.setName("OrderByClause"); parameter.setValueClass(java.lang.String.class); jasperDesign.addParameter(parameter); //Query JRDesignQuery query = new JRDesignQuery(); query.setText("SELECT * FROM Address $P!{OrderByClause}"); jasperDesign.setQuery(query); //Fields JRDesignField field = new JRDesignField(); field.setName("Id"); field.setValueClass(java.lang.Integer.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("FirstName"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("LastName"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("Street"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("City"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); //Variables JRDesignVariable variable = new JRDesignVariable(); variable.setName("CityNumber"); variable.setValueClass(java.lang.Integer.class); variable.setResetType(ResetTypeEnum.GROUP); JRDesignGroup group = new JRDesignGroup(); group.setName("CityGroup"); variable.setResetGroup(group); variable.setCalculation(CalculationEnum.SYSTEM); JRDesignExpression expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("($V{CityNumber} != null)?(new Integer($V{CityNumber}.intValue() + 1)):(new Integer(1))"); variable.setInitialValueExpression(expression); jasperDesign.addVariable(variable); variable = new JRDesignVariable(); variable.setName("AllCities"); variable.setValueClass(java.lang.String.class); variable.setResetType(ResetTypeEnum.REPORT); variable.setCalculation(CalculationEnum.SYSTEM); jasperDesign.addVariable(variable); //Groups group.setMinHeightToStartNewPage(60); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{City}"); group.setExpression(expression); JRDesignBand band = new JRDesignBand(); band.setHeight(20); JRDesignTextField textField = new JRDesignTextField(); textField.setX(0); textField.setY(4); textField.setWidth(515); textField.setHeight(15); textField.setBackcolor(new Color(0xC0, 0xC0, 0xC0)); textField.setMode(ModeEnum.OPAQUE); textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT); textField.setStyle(boldStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("\" \" + String.valueOf($V{CityNumber}) + \". \" + String.valueOf($F{City})"); textField.setExpression(expression); band.addElement(textField); JRDesignLine line = new JRDesignLine(); line.setX(0); line.setY(19); line.setWidth(515); line.setHeight(0); band.addElement(line); ((JRDesignSection)group.getGroupHeaderSection()).addBand(band); band = new JRDesignBand(); band.setHeight(20); line = new JRDesignLine(); line.setX(0); line.setY(-1); line.setWidth(515); line.setHeight(0); band.addElement(line); JRDesignStaticText staticText = new JRDesignStaticText(); staticText.setX(400); staticText.setY(0); staticText.setWidth(60); staticText.setHeight(15); staticText.setHorizontalAlignment(HorizontalAlignEnum.RIGHT); staticText.setStyle(boldStyle); staticText.setText("Count : "); band.addElement(staticText); textField = new JRDesignTextField(); textField.setX(460); textField.setY(0); textField.setWidth(30); textField.setHeight(15); textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT); textField.setStyle(boldStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("$V{CityGroup_COUNT}"); textField.setExpression(expression); band.addElement(textField); ((JRDesignSection)group.getGroupFooterSection()).addBand(band); jasperDesign.addGroup(group); //Title band = new JRDesignBand(); band.setHeight(50); line = new JRDesignLine(); line.setX(0); line.setY(0); line.setWidth(515); line.setHeight(0); band.addElement(line); textField = new JRDesignTextField(); textField.setBlankWhenNull(true); textField.setX(0); textField.setY(10); textField.setWidth(515); textField.setHeight(30); textField.setHorizontalAlignment(HorizontalAlignEnum.CENTER); textField.setStyle(normalStyle); textField.setFontSize(22); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$P{ReportTitle}"); textField.setExpression(expression); band.addElement(textField); jasperDesign.setTitle(band); //Page header band = new JRDesignBand(); band.setHeight(20); JRDesignFrame frame = new JRDesignFrame(); frame.setX(0); frame.setY(5); frame.setWidth(515); frame.setHeight(15); frame.setForecolor(new Color(0x33, 0x33, 0x33)); frame.setBackcolor(new Color(0x33, 0x33, 0x33)); frame.setMode(ModeEnum.OPAQUE); band.addElement(frame); staticText = new JRDesignStaticText(); staticText.setX(0); staticText.setY(0); staticText.setWidth(55); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(ModeEnum.OPAQUE); staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER); staticText.setStyle(boldStyle); staticText.setText("ID"); frame.addElement(staticText); staticText = new JRDesignStaticText(); staticText.setX(55); staticText.setY(0); staticText.setWidth(205); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(ModeEnum.OPAQUE); staticText.setStyle(boldStyle); staticText.setText("Name"); frame.addElement(staticText); staticText = new JRDesignStaticText(); staticText.setX(260); staticText.setY(0); staticText.setWidth(255); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(ModeEnum.OPAQUE); staticText.setStyle(boldStyle); staticText.setText("Street"); frame.addElement(staticText); jasperDesign.setPageHeader(band); //Column header band = new JRDesignBand(); jasperDesign.setColumnHeader(band); //Detail band = new JRDesignBand(); band.setHeight(20); textField = new JRDesignTextField(); textField.setX(0); textField.setY(4); textField.setWidth(50); textField.setHeight(15); textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("$F{Id}"); textField.setExpression(expression); band.addElement(textField); textField = new JRDesignTextField(); textField.setStretchWithOverflow(true); textField.setX(55); textField.setY(4); textField.setWidth(200); textField.setHeight(15); textField.setPositionType(PositionTypeEnum.FLOAT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{FirstName} + \" \" + $F{LastName}"); textField.setExpression(expression); band.addElement(textField); textField = new JRDesignTextField(); textField.setStretchWithOverflow(true); textField.setX(260); textField.setY(4); textField.setWidth(255); textField.setHeight(15); textField.setPositionType(PositionTypeEnum.FLOAT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{Street}"); textField.setExpression(expression); band.addElement(textField); line = new JRDesignLine(); line.setX(0); line.setY(19); line.setWidth(515); line.setHeight(0); line.setForecolor(new Color(0x80, 0x80, 0x80)); line.setPositionType(PositionTypeEnum.FLOAT); band.addElement(line); ((JRDesignSection)jasperDesign.getDetailSection()).addBand(band); //Column footer band = new JRDesignBand(); jasperDesign.setColumnFooter(band); //Page footer band = new JRDesignBand(); jasperDesign.setPageFooter(band); //Summary band = new JRDesignBand(); jasperDesign.setSummary(band); return jasperDesign; }</pre> The ant <span class="code"><code>compile</code></span> task only calls the <span class="code"><code>getJasperDesign()</code></span> method to load the <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> object created above, and compiles it into a <span class="code"><code>.jasper</code></span> file, which is further used at report filling time: <pre> public void compile() throws JRException { long start = System.currentTimeMillis(); JasperDesign jasperDesign = getJasperDesign(); JasperCompileManager.compileReportToFile(jasperDesign, "build/reports/NoXmlDesignReport.jasper"); System.err.println("Compile time : " + (System.currentTimeMillis() - start)); }</pre> <b>Running the Sample</b> <br> <br> Running the sample requires the <a href="http://ant.apache.org/" target="_blank" class="element">Apache Ant</a> library. Make sure that <span class="code"><code>ant</code></span> is already installed on your system (version 1.5 or later). <br> In a command prompt/terminal window set the current folder to <span class="code"><code>demo/samples/noxmldesign</code></span> within the JasperReports source project and run the <span class="code"><code>> ant test view</code></span> command. <br> It will generate all supported document types containing the sample report in the <span class="code"><code>demo/samples/noxmldesign/build/reports</code></span> directory. <br> Then the report will open in the JasperReports internal viewer. </span></td> </tr> <tr> <td colspan="6"> <br> </td> </tr> </table> <br> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td> <hr size="1"> </td> </tr> <tr> <td align="center"><span class="copy">© 2001-2010 Jaspersoft Corporation <a href="http://www.jaspersoft.com" target="_blank" class="copy">www.jaspersoft.com</a></span></td> </tr> </table> </body> </html>