Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto stretch detail band when print order is set to "horizontal" in JasperReport?

I have a main report which is printed horizontally. It has 5 columns. On every column i want to put a sub report. So i created this:

Main report

And the sub-report just like this:

Subreport

The problem is, when i run i get the following exception:

net.sf.jasperreports.engine.JRRuntimeException: Subreport overflowed on a band that does not support overflow.

Looks like jasper reports can't stretch the detail band vertically when there is a sub-report in it and the print order is set to horizontal.

What can I do to avoid this error and achieve what i want?

like image 724
Mateus Viccari Avatar asked Apr 18 '15 12:04

Mateus Viccari


1 Answers

I found the solution for this problem. After a deep search i found that, sadly, there's no way to do this on Jasper Reports because, no matter what, when you have a report printed horizontally, the "Detail" band will never change it's height. So, subreports or text fields which overflows will throw an exception.

The workaround for this problem is to work without reports, with a PDF generator like iText, for example. This is the code i did to achive what i wanted with iText if somebody needs it:

Document document = new Document();
File arquivo = new File("C:\\Users\\Mateus\\Desktop\\testezãozarãozão.pdf");
PdfWriter.getInstance(document, new FileOutputStream(arquivo));
document.open();

LinkedHashMap<Produto, LinkedHashMap<String, List<PrePedidoItem>>> produtos = createStructuredHashMap();

for (Produto produto : produtos.keySet()) {
    PdfPTable table = new PdfPTable(5);
    PdfPCell cellProduto = new PdfPCell();
    Phrase phraseProduto = new Phrase(String.valueOf(produto));
    phraseProduto.setFont(new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD|Font.UNDERLINE, new BaseColor(50, 65, 200)));
    cellProduto.addElement(phraseProduto);
    cellProduto.setColspan(5);
    cellProduto.setHorizontalAlignment(PdfPCell.ALIGN_MIDDLE);
    cellProduto.setBorder(Rectangle.NO_BORDER);
    cellProduto.setPaddingBottom(10);
    cellProduto.setPaddingTop(20);
    table.addCell(cellProduto);
    LinkedHashMap<String, List<PrePedidoItem>> mapas = produtos.get(produto);
    int mapasAdicionados = 0;
    for (String mapa : mapas.keySet()) {
        PdfPCell cellMapa = new PdfPCell();
        Phrase phraseMapa = new Phrase(mapa);
        phraseMapa.setFont(new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD, new BaseColor(215, 100, 0)));
        cellMapa.addElement(phraseMapa);
        List<PrePedidoItem> itensDoMapa = mapas.get(mapa);
        for (PrePedidoItem item : itensDoMapa) {
            DecimalFormat df = new DecimalFormat("###,##0.00");
            Phrase phraseItem = new Phrase(df.format(item.getLargura()) + " x " + df.format(item.getComprimento()));
            phraseItem.setFont(new Font(Font.FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.BLACK));
            cellMapa.addElement(phraseItem);
        }
        cellMapa.setBorder(Rectangle.NO_BORDER);
        table.addCell(cellMapa);
        mapasAdicionados ++;
        if(mapasAdicionados == 5) {
            mapasAdicionados = 0;
        }
    }
    PdfPCell celulaPreenchimentoMapas = new PdfPCell();
    celulaPreenchimentoMapas.setColspan(5 - mapasAdicionados);
    celulaPreenchimentoMapas.setBorder(Rectangle.NO_BORDER);
    table.addCell(celulaPreenchimentoMapas);
    document.add(table);
}

document.close();
Desktop.getDesktop().open(arquivo);
like image 155
Mateus Viccari Avatar answered Sep 30 '22 14:09

Mateus Viccari