Jump to content

This is my code design compile and run jasper but it create blank pdf. please suggest if there is any problem in code.


vaibhav.lather

Recommended Posts

package com.innoeye.crosstab;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JROrigin;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.type.BandTypeEnum;
import net.sf.jasperreports.engine.type.PositionTypeEnum;
import net.sf.jasperreports.engine.util.JRLoader;
import com.mysql.*;
public class DesignJasperWithJava {
    
    private static Connection getConnection() throws ClassNotFoundException, SQLException
    {
        //Change these settings according to your local configuration
        String driver = "com.mysql.jdbc.Driver";
        String connectString = "jdbc:mysql://localhost:3306/jasper";
        String user = "root";
        String password = "root";


        Class.forName(driver);
        Connection conn = DriverManager.getConnection(connectString, user, password);
        return conn;
    }
    
    public JasperDesign getDesign() {
        JasperDesign jasperDesign=null;
    try{    
        jasperDesign = new JasperDesign();
        
        jasperDesign.setName("NewJasper");
        jasperDesign.setPageHeight(842);
        jasperDesign.setPageWidth(595);
        jasperDesign.setColumnWidth(515);
        jasperDesign.setColumnSpacing(0);
        jasperDesign.setLeftMargin(40);
        jasperDesign.setRightMargin(40);
        jasperDesign.setTopMargin(50);
        jasperDesign.setBottomMargin(50);
// Parameters        
        JRDesignParameter parameter=new JRDesignParameter();
        parameter.setName("Year");
        parameter.setValueClass(java.lang.String.class);
        jasperDesign.addParameter(parameter);
        
        parameter=new JRDesignParameter();
        parameter.setName("Department");
        parameter.setValueClass(java.lang.String.class);
        jasperDesign.addParameter(parameter);
        
        parameter=new JRDesignParameter();
        parameter.setName("Amount");
        parameter.setValueClass(java.lang.Double.class);
        jasperDesign.addParameter(parameter);
//Style        
        JRDesignStyle style=new JRDesignStyle();
        style.setName("vaibhav");
        style.setFontName("Aerial");
        style.setFontSize(10);
        jasperDesign.addStyle(style);
//Query        
        JRDesignQuery query = new JRDesignQuery();
        query.setText("select * from myDepartment");
        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("department");
        field.setValueClass(java.lang.String.class);
        jasperDesign.addField(field);

        field = new JRDesignField();
        field.setName("year");
        field.setValueClass(java.lang.String.class);
        jasperDesign.addField(field);

        field = new JRDesignField();
        field.setName("budget_amount");
        field.setValueClass(java.lang.Double.class);
        jasperDesign.addField(field);

        field = new JRDesignField();
        field.setName("months");
        field.setValueClass(java.lang.String.class);
        jasperDesign.addField(field);
        
        
//Band        
        JROrigin origin=new JROrigin(BandTypeEnum.COLUMN_HEADER);
        JRDesignBand band=new JRDesignBand();
        band.setHeight(20);
    //    JRDesignSection designSection = (JRDesignSection) jasperDesign.getColumnHeader();
        JRDesignSection designSection = new JRDesignSection(origin);
        JRDesignTextField textField=new JRDesignTextField();
        textField.setBlankWhenNull(true);
        textField.setBold(true);
        textField.setFontSize(12);
        textField.setX(0);
        textField.setY(4);
        textField.setWidth(50);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_BOTTOM);
        JRDesignExpression expression = new JRDesignExpression();
        expression.setText("$P{Year}");
        textField.setExpression(expression);
        band.addElement(textField);
        
        textField=new JRDesignTextField();
        textField.setBlankWhenNull(true);
        textField.setBold(true);
        textField.setFontSize(12);
        textField.setX(55);
        textField.setY(4);
        textField.setWidth(200);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_BOTTOM);
        expression = new JRDesignExpression();
        expression.setText("$P{Department}");
        textField.setExpression(expression);
        band.addElement(textField);
        
        textField=new JRDesignTextField();
        textField.setBlankWhenNull(true);
        textField.setBold(true);
        textField.setFontSize(12);
        textField.setX(260);
        textField.setY(4);
        textField.setWidth(255);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_BOTTOM);
        expression = new JRDesignExpression();
        expression.setText("$P{Amount}");
        textField.setExpression(expression);
        band.addElement(textField);
        System.out.println("  Design  "+designSection+"  Band  "+band);
        designSection.addBand(band);
        
//Detail
        origin=new JROrigin(BandTypeEnum.DETAIL);
        designSection = new JRDesignSection(origin);
        band = new JRDesignBand();
        band.setHeight(20);
        textField = new JRDesignTextField();
        textField.setX(0);
        textField.setY(4);
        textField.setWidth(50);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_TOP);
    //    textField.setTextAlignment(JRTextElement.TEXT_ALIGN_RIGHT);
    //    textField.setFont(normalFont);
        expression = new JRDesignExpression();
        expression.setText("$F{year}");
        textField.setExpression(expression);
        band.addElement(textField);
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(true);
        textField.setX(55);
        textField.setY(4);
        textField.setWidth(200);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_TOP);
    //    textField.setFont(normalFont);
        expression = new JRDesignExpression();
    //    expression.setValueClass(java.lang.String.class);
        expression.setText("$F{department_name}");
        textField.setExpression(expression);
        band.addElement(textField);
        textField = new JRDesignTextField();
        textField.setStretchWithOverflow(true);
        textField.setX(260);
        textField.setY(4);
        textField.setWidth(255);
        textField.setHeight(19);
        textField.setPositionType(PositionTypeEnum.FIX_RELATIVE_TO_TOP);
    //    textField.setFont(normalFont);
        expression = new JRDesignExpression();
    //    expression.setValueClass(java.lang.String.class);
        expression.setText("$F{budget_amount}");
        textField.setExpression(expression);
        band.addElement(textField);
        System.out.println("  Design   "+designSection+"  Band  "+band);
        designSection.addBand(band);
        

        designSection.addBand(band);
                
    }catch(Exception e){
        
        e.printStackTrace();
    }
        
         
        return jasperDesign;    
    }
    
    public static void main(String args[]) throws JRException{
        try{
         DesignJasperWithJava obj=new DesignJasperWithJava();
         String fileName="c:/myReport";
         JasperDesign jasperDesign =obj.getDesign();
         JasperCompileManager.compileReportToFile(jasperDesign, fileName);
         System.err.println("Compile time : " + (System.currentTimeMillis()));
         System.out.println("   export file  ");
        
         Map parameters = new HashMap();
         parameters.put("Year", "Year");
         parameters.put("Department", "Department");
         parameters.put("Amount", "Amount");

       
         Connection conn=DesignJasperWithJava.getConnection();
         JasperPrint jprint = (JasperPrint)JasperFillManager.fillReport(fileName, parameters,conn);
         System.err.println("Filling time : " + (System.currentTimeMillis()));
     
         JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jprint);
         exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"c:/myReport.pdf");
         exporter.exportReport();
         System.out.println("PDF creation time : " + (System.currentTimeMillis()));
         System.exit(0);
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }

}
 

Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...