sheanD
sheanD

Reputation: 211

Dynamicreports detail section with page height error

I'm Creating report using Dynamicreports. there is my code. when i run it give erros. my content it so big (Detail). please help me to fix this. when my content(Detail) is 10 lines, this code is woking well. but when my content(Detail) is big, it give error.

Error:

net.sf.dynamicreports.report.exception.DRException: net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
     1. The detail section, the page and column headers and footers and the margins do not fit the page height.
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReport(JasperReportBuilder.java:279)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:309)
    at dynamicjusper.NewJFrame.jButton5ActionPerformed(NewJFrame.java:606)
    at dynamicjusper.NewJFrame.access$400(NewJFrame.java:78)
    at dynamicjusper.NewJFrame$5.actionPerformed(NewJFrame.java:164) 

+ etc..

Code :

    StyleBuilder boldStyle = stl.style().bold();
    StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
    StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle).setBorder(stl.pen1Point()).setBackgroundColor(Color.LIGHT_GRAY);
    FontBuilder boldFont = stl.fontArialBold().setFontSize(12);

    StyleBuilder titleStyle        = stl.style(boldCenteredStyle).setVerticalAlignment(VerticalAlignment.MIDDLE).setFontSize(15);
    StyleBuilder DetailHeaderStyle = stl.style(boldStyle).setForegroundColor(Color.BLUE);

    try {
        JasperReportBuilder report = report();
        report.setDefaultFont(stl.fontCourierNew().setFontSize(8));


        //page header
        report.pageHeader(cmp.horizontalList()
           .add(
            cmp.image("./image.gif").setFixedDimension(80, 80).setHorizontalAlignment(HorizontalAlignment.LEFT),
            cmp.text("TEST").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
            cmp.text("Sub Text").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT))
            .newRow()
            .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10)));



        String Content = jTextArea1.getText();

    String[] PAGECONTENT = Content.split("");

    for (int i = 0; i < PAGECONTENT.length; i++) {

        String string = PAGECONTENT[i];


        String Line1;


        String[] dataandheader= string.split("-----------------------------------------------------------------------------------------------------------------------------------");


        for (int j = 0; j < dataandheader.length; j++) {
            String string1 = dataandheader[j];


            if(j<1){

                 try {
            Scanner sc = new Scanner(string1);
            while (sc.hasNextLine()) {
                Line1 = sc.nextLine();
            //Page Detail
           report.detail(cmp.horizontalList()

                   .newRow()
                   .add(cmp.text(Line1).setStyle(DetailHeaderStyle))

                   );

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

            }else{

                if(j>0 && j<dataandheader.length){
            //Page Detail
           report.detail(cmp.horizontalList()

                   .newRow()
                  // .add(cmp.text(text))
                   .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(5))

                   );

            }

                 try {
            Scanner sc = new Scanner(string1);
            while (sc.hasNextLine()) {


                Line1 = sc.nextLine();

                //Page Detail
           report.detail(cmp.horizontalList()

                   .newRow()
                   .add(cmp.text(Line1))


                   );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            }

        }

    }
        //page footer
        report.pageFooter(cmp.pageXofY());//shows number of page at page footer

        report.setDataSource(new JREmptyDataSource());//set datasource

       JRViewer jrviwer = new JRViewer(report.toJasperPrint());

       ((JPanel)jrviwer.getComponent(0)).remove(0); // remove save button
       ((JPanel)jrviwer.getComponent(0)).remove(1); // remove refresh button

       JFrame jf = new JFrame();
       jf.setTitle("Test viwer");
       jf.getContentPane().add(jrviwer);
       jf.validate();
       jf.setVisible(true);
       jf.setSize(new Dimension(800,600));
       jf.setLocation(300,100);
       jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);



    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 3

Views: 5876

Answers (1)

Hardik Sheth
Hardik Sheth

Reputation: 198

This is a very common problem in dynamicReport, Whenever You use any component in detail, header or footer with the height or width more than page dimensions.

Try one thing, Divide your detail component in chunks of horizontalList components(divide them to maximum parts). I do this and handles them logically. You should prefer to you add

report.detail(cmp1);

and then

report.detail(cmp2); 

insteand of adding these 2 components in one component 'cmp' which contains both cmp1 and cmp2 , when you are not sure about height.

Upvotes: 1

Related Questions