alex.grinnin Posted November 27, 2015 Posted November 27, 2015 I developed custom component for barcode rendering. If I use only one instance it works properly. But if I want to pass data from my datasource I getting incorrect view - only last instance of barcode was rendered. Whereas common rendering text My datasource: public class ListDataSource implements JRDataSource{ private int index = -1; private String[][] data = { {"text PDf417", "PDF417"}, {"text Code128","Code128"}, }; @Override public boolean next() throws JRException { index++; return (index < data.length); } @Override public Object getFieldValue(JRField field) throws JRException { Object value = null; String fieldName = field.getName().toLowerCase(); if("codetext".equals(fieldName)) { value = data[index][0]; } else if("symbology".equals(fieldName)) { value = data[index][1]; } return value; }} My jrxml file: <?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="DataSource" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50"> <field name="codetext" class="java.lang.String"/> <field name="symbology" class="java.lang.String"/> <detail> <band height="200"> <textField isStretchWithOverflow="true"> <reportElement positionType="Float" x="315" y="4" width="100" height="150"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{codetext}]]></textFieldExpression> </textField> <textField isStretchWithOverflow="true"> <reportElement positionType="Float" x="415" y="4" width="100" height="150"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{symbology}]]></textFieldExpression> </textField> <componentElement> <reportElement x="10" y="30" width="300" height="100"/> <as:myBarcodeComponent xmlns:as="http://sourceforge.net/jasperreports/components/alex" xsi:schemaLocation= "http://jasperreports.sourceforge.net/jasperreports/components/alex alex-components.xsd"> <as:barCodeAttributes> <as:codeExpression><![CDATA[$F{codetext}]]></as:codeExpression> <as:symbologyExpression><![CDATA[$F{symbology}]]></as:symbologyExpression> </as:barCodeAttributes> </as:myBarcodeComponent> </componentElement> <line> <reportElement positionType="Float" x="0" y="153" width="515" height="1" forecolor="#808080"/> </line> </band> </detail></jasperReport> Test class try { String jrxmlFilePath = "DataSource.jrxml"; String jasperFileName = "DataSource.jasper"; String pdfFileName = "DataSource.pdf"; JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFileName); JRPdfExporter exporter = new JRPdfExporter(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFileName, null, dataSource ); ExporterInput exporterInput = new SimpleExporterInput(jasperPrint); exporter.setExporterInput(exporterInput); PdfExporterConfiguration pdfExporterConfiguration = new SimplePdfExporterConfiguration(); exporter.setConfiguration(pdfExporterConfiguration); File saveToFile = new File(pdfFileName); OutputStream outputStream = new FileOutputStream(saveToFile); SimpleOutputStreamExporterOutput outputStreamExporterOutput = new SimpleOutputStreamExporterOutput(outputStream); exporter.setExporterOutput(outputStreamExporterOutput); exporter.exportReport(); System.out.println("Report exported to " + saveToFile.getAbsolutePath()); System.out.println("Finished."); } catch (Exception e) { e.printStackTrace(); } As result I get two lines - first for PDF417 and second for Code128. But for both lines only last barcode from list in data source was rendered. Where I should search mistake in my implementation? Thanks in advance.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now