Wednesday, October 8, 2014

DynamicReports: How to make subreport display at a fixed page position / bottom of last page - resolved

Lets say that you have a subreport which displays a message. The length of the message can be from 0 to 20 lines. Following this message subreport, you have another subreport which displays some data and you would like it to be always displayed at the bottom one third of the page regardless the length of the message.

One way to make the second data subreport to be always displayed at a certain height of the page is to set the height of the first message subreport above it. The way to do it is to put the subreport in a vertical list or a horizontal list and set the height/width/dimension of that vertical list or horizontal list.

However, if the message has so many lines that it overflows the space you specified, the message subreport will expand and push your data subreport down. If this happens and you would like the data subreport to be displayed at the bottom one third of the next page, you need to count the lines of your message and need to know how many lines of message the space you specified can hold and the whole page can hold. If the lines of message is larger than the lines of message your space can hold, you need to add empty lines to the message until it starts to display on the next page. You need to let it display at least one line on the next page, only then the height you specified for the message subreport will apply to the message on the next page and let your data subreport be displayed at the right position.


import java.util.ArrayList;
import java.util.List;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.exp;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import net.sf.dynamicreports.report.builder.component.SubreportBuilder;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class ReportDimensionDemo {
    private int messageRows = 0;

    public static void main(String[] args){
        new ReportDimensionDemo();
    }
 
    public ReportDimensionDemo() {
        JasperReportBuilder report = DynamicReports.report();
        report.setDataSource(createDataSource());
     
        SubreportBuilder messageSubreport = cmp.subreport(buildMessageSubreport())
                .setDataSource(exp.subDatasourceBeanCollection("messageData"));
        SubreportBuilder dataSubreport = cmp.subreport(buildDataSubreport())
                .setDataSource(exp.subDatasourceBeanCollection("dataData"));
     
        report.detail(cmp.verticalList(messageSubreport).setFixedHeight(300),
               // .setFixedWidth(200)
                //.setFixedDimension(200, 300)
                dataSubreport );
       //to display at the bottom of last page
       //report.summary(dataSubreport);      
        try {
            report.show(false);
        }catch(DRException de){
            de.printStackTrace(System.out);
        }
    }
 
    private JasperReportBuilder buildMessageSubreport() {
        JasperReportBuilder report = DynamicReports.report();
        report.columns(col.column("message", DataTypes.stringType()));
     
        return report;
    }
 
    private JasperReportBuilder buildDataSubreport() {
        JasperReportBuilder report = DynamicReports.report();
        report.columns(col.column("Name", "name", DataTypes.stringType())
                    .setStyle(stl.style(stl.pen1Point())),
                col.column("Price", "price", DataTypes.doubleType())
                        .setStyle(stl.style(stl.pen1Point()))
                        .setPattern("$##0.00"));
     
        return report;
    }
 
    private JRDataSource createDataSource() {
        List<ReportData> dataSource = new ArrayList<ReportData>();
        ReportData reportData = new ReportData();
        List<MessageData> messageList = new ArrayList<MessageData>();
        MessageData messageData = new MessageData();
        List<DataData> dataList = new ArrayList<DataData>();
        DataData dataData = new DataData();
     
        for (i=0; i<50; i++) {
             messageData.setMessage("The most useful report in the universe - "+i);
             messageList.add(messageData);
             //count the lines of message
             messageRows++;
             messageData = new MessageData();
        }

        //If the message overflow the height you specified, push the message band
        //to the next page so that the data subreport is displayed at the right
         //position on the next page.
        int remainder = messageRows%<message lines per whole page>
        if (remainder > <message lines in specified space>) {
              while (remainder < (<message lines per whole page> + 2)) {
                      messageData.setMessage("");
                      messageList.add(messageData);
                      messageData = new MessageData();
              }
       }
     
        reportData.setMessageData(messageList);
     
        dataData.setName("Apple");
        dataData.setPrice(1.68);
        dataList.add(dataData);
        dataData = new DataData();
        dataData.setName("Papaya");
        dataData.setPrice(3.14);
        dataList.add(dataData);
        reportData.setDataData(dataList);
     
        dataSource.add(reportData);
        return new JRBeanCollectionDataSource(dataSource);
    }
 
    public class ReportData {
        private List<MessageData> messageData;
        private List<DataData> dataData;
     
        public List<MessageData> getMessageData() {return messageData;}
        public void setMessageData(List<MessageData> m) {messageData=m;}
        public List<DataData> getDataData() {return dataData;}
        public void setDataData(List<DataData> d) {dataData=d;}
    }
 
    public class MessageData{
        private String message;
     
        public String getMessage() {return message;}
        public void setMessage(String m) {message=m;}
    }
 
    public class DataData {
        private String name;
        private Double price;
     
        public String getName() {return name;}
        public void setName(String n) {name=n;}
        public Double getPrice() {return price;}
        public void setPrice(Double p) {price=p;}
    }
}
   
----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment