2
votes

I am editing .jasper file using report tool. I am changing the alignment for parameter left to centre then i save that file. it generates the "jrxml" file also. in my Java code I can pass the .jasper location to print some items. but my changes not affect and the old design remains same..

Help me , How can I edit and save .jasper???

public static JasperPrint createRefundPrint(Ticket ticket, HashMap map) throws Exception { final String FILE_RECEIPT_REPORT = "/com/floreantpos/report/template/RefundReceipt.jasper";

    TicketDataSource dataSource = new TicketDataSource(ticket);
    return createJasperPrint(FILE_RECEIPT_REPORT, map, new JRTableModelDataSource(dataSource));
}
3
delete the old jasper file from your location and compile the jasperReport (.jrxml file) in IReport tool.. you will get the updated jasper file - techGaurdian

3 Answers

1
votes

if you use a IReport for editing your .jrxml reports, you should just press the "Preview" button. Then the .jasper report file will be generated automatically in folder that contains the .jrxml file

0
votes

To edit jasper report, you make your changes in the JRXML file. Once you build or compile your report it will generate a .jasper file which is used in your java code.

0
votes

you try make dynamic report. You do not have a location problem when you generate your own report. Example;

public JasperDesign createDesign() throws JRException {
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("sampleDynamicJasperDesign");
    jasperDesign.setPageWidth(595); // page width
    jasperDesign.setPageHeight(842); // page height
    jasperDesign.setColumnWidth(515);   // column width of page
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(20);
    jasperDesign.setBottomMargin(20);

    JRDesignExpression expression = new JRDesignExpression();

    //Set style of page.
    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);


    /*
    * Generate field dynamically
    * */

    JRDesignField field = new JRDesignField();
    field.setName("firstName");
    field.setValueClass(String.class);
    jasperDesign.addField(field);

    field = new JRDesignField();
    field.setName("lastName");  // set name for field.
    field.setValueClass(String.class);  // set class for field. Its always depends upon data type which we want to get in this field.
    jasperDesign.addField(field);   // Added field in design.

    field = new JRDesignField();
    field.setName("age");
    field.setValueClass(Integer.class);
    jasperDesign.addField(field);

    JRDesignBand band = new JRDesignBand();

    //Title Band
    band = new JRDesignBand();
    band.setHeight(30);


    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setText("Person's Specification");
    staticText.setX(0);
    staticText.setY(0);
    staticText.setHeight(20);
    staticText.setWidth(515);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    band.addElement(staticText);
    jasperDesign.setTitle(band);


    band = new JRDesignBand(); // New band
    band.setHeight(20); // Set band height

    /*Create text field dynamically*/
    JRDesignTextField textField = new JRDesignTextField();
    textField.setX(0);  // x position of text field.
    textField.setY(0);  // y position of text field.
    textField.setWidth(160);    // set width of text field.
    textField.setHeight(20);    // set height of text field.
    JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression.
    jrExpression.setText(""" + "First Name: " + """ + "+" + "$F{firstName}"); //  Added String before field in expression.
    textField.setExpression(jrExpression);  // set expression value in textfield.
    band.addElement(textField); // Added element in textfield.

    textField = new JRDesignTextField();
    textField.setX(160);
    textField.setY(0);
    textField.setWidth(160);
    textField.setHeight(20);
    jrExpression = new JRDesignExpression();
    jrExpression.setText("$F{lastName}" + "+" + """ + " :Last Name" + """); // Added string after field value
    textField.setExpression(jrExpression);
    band.addElement(textField);

    textField = new JRDesignTextField();
    textField.setX(320);
    textField.setY(0);
    textField.setWidth(160);
    textField.setHeight(20);
    jrExpression = new JRDesignExpression();
    String age = """ + "<font>" + """ + "+" + """ + "Age is: " + """ + "+" + """ + "</font><font>" + """ + "+" + "$F{age}" + "+" + """ + "</font>" + """;  // added html in text field with different color.
    jrExpression.setText(age);
    textField.setExpression(jrExpression);
    textField.setMarkup("html"); // By Default markup is none, We need to set it as html if we set expression as html.
    band.addElement(textField);
    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);


    return jasperDesign;
}