<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 Report 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 Report 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 final documents can be created without using the reporting engine, 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/noreport/?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="#noreport">Creating Final Documents Using the API Directly (No Report Filling)</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="noreport"></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 Final Documents Using the API Directly (No Report Filling)</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 final in-memory documents without using the report generation process, but rather 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 JasperPrint Object</b> <br> <br> When generating reports, the main purpose is to create a pixel-perfect document, ready for viewing, printing, or exporting to other formats. These documents are stored in serializable <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> objects, that can be saved on the disk, or transfered over the network if needed. <br> Usually, one can obtain a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object by compiling and filling a report template, following the steps below: <ol> <li>Create a report template containing all information needed for the report design. Usually, report templates are stored in JRXML source files.</li> <li>Parse the report template source file using the <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/xml/JRXmlLoader.html" target="_blank">JRXmlLoader</a></span> in order to obtain a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JasperDesign.html" target="_blank">JasperDesign</a></span> design template object.</li> <li>Compile the design template object using a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/design/JRCompiler.html" target="_blank">JRCompiler</a></span> in order to complete all consistency validations and create a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperReport.html" target="_blank">JasperReport</a></span> object.</li> <li>Fill the <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperReport.html" target="_blank">JasperReport</a></span> object with all its data and generate the pixel-perfect document stored in a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object.</li> </ol> This is the most common way to generate documents in JasperReports. But there are situations when the steps above cannot be all performed. Even in this case, a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object can be generated from scratch as in-memory document. Once generated, it can be serialized and stored on disk, either in a <span class="code"><code>*.jrprint</code></span> file, or in a specific XML file with the <span class="code"><code>.jrpxml</code></span> extension. Or it can be then exported to various other output formats. <br> <br> Taking a look inside the <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object, one can see that it has a series of member properties that can be set: <pre> private String name; private int pageWidth; private int pageHeight; private Integer topMargin; private Integer leftMargin; private Integer bottomMargin; private Integer rightMargin; private OrientationEnum orientationValue = OrientationEnum.PORTRAIT; private Map fontsMap = new HashMap(); private List fontsList = new ArrayList(); private Map stylesMap = new HashMap(); private List stylesList = new ArrayList(); private Map originsMap = new HashMap(); private List originsList = new ArrayList(); private List pages = new ArrayList(); private transient Map anchorIndexes; private DefaultStyleProvider defaultStyleProvider; private String formatFactoryClass; private String localeCode; private String timeZoneId; private JRPropertiesMap propertiesMap; /** * Creates a new empty document. */ public JasperPrint() { defaultStyleProvider = new DefaultStyleProvider(null, null); propertiesMap = new JRPropertiesMap(); }</pre> Therefore, creating a <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object may be accomplished by creating an empty <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> instance using the default constructor, and then setting its members with appropriate values. <br> <br> <b>The No Report 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/JasperPrint.html" target="_blank">JasperPrint</a></span> object. The sample doesn't contain any JRXML template file. The <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object is hardcoded in the <span class="code"><code>src/NoReportApp.java</code></span> file (see the getJasperPrint() method). <br> Report compilation and filling are no more necessary. <pre> private static JasperPrint getJasperPrint() throws JRException { //JasperPrint JasperPrint jasperPrint = new JasperPrint(); jasperPrint.setName("NoReport"); jasperPrint.setPageWidth(595); jasperPrint.setPageHeight(842); //Fonts JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("Sans_Normal"); normalStyle.setDefault(true); normalStyle.setFontName("DejaVu Sans"); normalStyle.setFontSize(8); normalStyle.setPdfFontName("Helvetica"); normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false); jasperPrint.addStyle(normalStyle); JRDesignStyle boldStyle = new JRDesignStyle(); boldStyle.setName("Sans_Bold"); boldStyle.setFontName("DejaVu Sans"); boldStyle.setFontSize(8); boldStyle.setBold(true); boldStyle.setPdfFontName("Helvetica-Bold"); boldStyle.setPdfEncoding("Cp1252"); boldStyle.setPdfEmbedded(false); jasperPrint.addStyle(boldStyle); JRDesignStyle italicStyle = new JRDesignStyle(); italicStyle.setName("Sans_Italic"); italicStyle.setFontName("DejaVu Sans"); italicStyle.setFontSize(8); italicStyle.setItalic(true); italicStyle.setPdfFontName("Helvetica-Oblique"); italicStyle.setPdfEncoding("Cp1252"); italicStyle.setPdfEmbedded(false); jasperPrint.addStyle(italicStyle); JRPrintPage page = new JRBasePrintPage(); JRPrintLine line = new JRBasePrintLine(jasperPrint.getDefaultStyleProvider()); line.setX(40); line.setY(50); line.setWidth(515); line.setHeight(0); page.addElement(line); JRPrintImage image = new JRBasePrintImage(jasperPrint.getDefaultStyleProvider()); image.setX(45); image.setY(55); image.setWidth(165); image.setHeight(40); image.setScaleImage(ScaleImageEnum.CLIP); image.setRenderer( JRImageRenderer.getInstance( JRLoader.loadBytesFromLocation("jasperreports.png") ) ); page.addElement(image); JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider()); text.setX(210); text.setY(55); text.setWidth(345); text.setHeight(30); text.setTextHeight(text.getHeight()); text.setHorizontalAlignment(HorizontalAlignEnum.RIGHT); text.setLineSpacingFactor(1.3133681f); text.setLeadingOffset(-4.955078f); text.setStyle(boldStyle); text.setFontSize(18); text.setText("JasperReports Project Description"); page.addElement(text); text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider()); text.setX(210); text.setY(85); text.setWidth(325); text.setHeight(15); text.setTextHeight(text.getHeight()); text.setHorizontalAlignment(HorizontalAlignEnum.RIGHT); text.setLineSpacingFactor(1.329241f); text.setLeadingOffset(-4.076172f); text.setStyle(italicStyle); text.setFontSize(12); text.setText((new SimpleDateFormat("EEE, MMM d, yyyy")).format(new Date())); page.addElement(text); text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider()); text.setX(40); text.setY(150); text.setWidth(515); text.setHeight(200); text.setTextHeight(text.getHeight()); text.setHorizontalAlignment(HorizontalAlignEnum.JUSTIFIED); text.setLineSpacingFactor(1.329241f); text.setLeadingOffset(-4.076172f); text.setStyle(normalStyle); text.setFontSize(14); text.setText( "JasperReports is a powerful report-generating tool that has the ability to deliver rich " + "content onto the screen, to the printer or into PDF, HTML, XLS, CSV or XML files.\n\n" + "It is entirely written in Java and can be used in a variety of Java enabled applications, " + "including J2EE or Web applications, to generate dynamic content.\n\n" + "Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner." ); page.addElement(text); jasperPrint.addPage(page); return jasperPrint; }</pre> The ant <span class="code"><code>compile</code></span> task is no more necessary. When the ant <span class="code"><code>fill</code></span> task is performed, the only things it has to perform is to call the <span class="code"><code>getJasperPrint()</code></span> method, which produces an already filled <span class="element"><a href="http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JasperPrint.html" target="_blank">JasperPrint</a></span> object, and to save this in-memory object in a <span class="code"><code>*.jrprint</code></span> file: <pre> public void fill() throws JRException { long start = System.currentTimeMillis(); JasperPrint jasperPrint = getJasperPrint(); JRSaver.saveObject(jasperPrint, "build/reports/NoReport.jrprint"); System.err.println("Filling 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/noreport</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/noreport/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>