打印份数Jasper report / ireport

时间:2015-05-21 20:09:02

标签: java jasper-reports

我有一个按参数构建的报告,有3列和相同的值。

之后,打破页面。

我想自动打印此报告的X份数。

PS:无法循环同一个文档。我真的需要插入这些文件的“份数”。

public void imprimir(String codigo, String tamanho, Integer qtd) throws JRException
{       
    String arquivo = "relatorios_ireport/etiqueta_pausa.jasper"; //arquivo de relatorio
    java.io.InputStream file = getClass().getClassLoader().getResourceAsStream(arquivo); //carrego o arquivo

    //NUMBER OF REPEAT
   ArrayList array_qtd = new ArrayList();        

    for(int i = 0; i < 3; i++){  
            array_qtd.add("Número" + i);  
    }        

    JRBeanCollectionDataSource dados_qtd = new JRBeanCollectionDataSource(array_qtd);          

    Map parametros = new HashMap(); 

    //parametro 1 (codigo)
    parametros.put("codigo", codigo);

    //parametro 2 (tamanho)
    parametros.put("tamanho", tamanho);          

    JasperPrint printer = null;
    printer = JasperFillManager.fillReport(file, parametros, dados_qtd); 

    //SHOW DIALOG REPORT
    JRViewer view = new JRViewer(printer); //preview
        JDialog dialog = new JDialog(); 

        dialog.getContentPane().add(view); 
        dialog.setSize(600,300);
        dialog.setLocationRelativeTo(jPanel2);
        dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setModal(true);
        dialog.setVisible(true);

        //DIRECT PRINT
        //JasperPrintManager.printReport(printer,false);        
}//fim imprimir

2 个答案:

答案 0 :(得分:1)

解决

$ public void imprimir(String codigo,String tamanho,Integer qtd)抛出JRException {

    String arquivo = "relatorios_ireport/etiqueta_pausa.jasper"; //arquivo de relatorio

    java.io.InputStream file = getClass().getClassLoader().getResourceAsStream(arquivo); //carrego o arquivo

    //NUMERO DE REPETIÇÕES
    ArrayList array_qtd = new ArrayList();

    //completar as 3 colunas com mesmo valor
    for(int i = 0; i < 3; i++){  
            array_qtd.add("Número" + i);  
    }



    JRBeanCollectionDataSource dados_qtd = new JRBeanCollectionDataSource(array_qtd);  


    Map parametros = new HashMap(); 

    //parametro 1 (codigo)
    parametros.put("codigo", codigo);

    //parametro 2 (tamanho)
    parametros.put("tamanho", tamanho);



    //CRIANDO
    JasperPrint printer = null;
    printer = JasperFillManager.fillReport(file, parametros, dados_qtd); 

    //exportar para pdf
       //JasperExportManager.exportReportToPdfFile(printer,"D://sample_report.pdf");

        if (jCheckBox_visualizar.isSelected()){

        //CHAMAR VIEW 1 DE CADA VEZ (FECHOU ABRE O OUTRO)
        JRViewer view = new JRViewer(printer); //preview
        JDialog dialog = new JDialog(); //jdialog que contem o preview

        dialog.getContentPane().add(view); //adiciono o preview
        dialog.setSize(600,300);
        dialog.setLocationRelativeTo(jPanel2);
        dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setModal(true);
        dialog.setVisible(true);
        //CHAMAR VIEW DIRETO(TODOS AS QTD DE UMA VEZ)
        //JasperViewer.viewReport(printer, false);

        }else{

        //PROBLEM SOLVED WITH EXPORTER ATRIBUTESET.ADD(NEW Copies(qtd)) qtd = number of copies i want :D

            PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
            //printRequestAttributeSet.add(MediaSizeName.ISO_A4);
            printRequestAttributeSet.add(new Copies(qtd));

            PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
            //printServiceAttributeSet.add(new PrinterName("Argox_OS-214_plus_series_PPLA", null));
            //printServiceAttributeSet.add(new PrinterName("Argox OS-214 plus series PPLA em pcestamparia", Locale.getDefault()));
            printServiceAttributeSet.add(new PrinterName("Argox OS-214 plus series PPLA", Locale.getDefault()));
            //printServiceAttributeSet.add(new PrinterName("////pcestamparia//Argox OS-214 plus series PPLA", null));
            //printServiceAttributeSet.add(new PrinterName("//pcestamparia/Argox OS-214 plus series PPLA", null));
            //printServiceAttributeSet.add(new PrinterName("////pcestamparia//Argox_OS-214_plus_series_PPLA", Locale.getDefault()));
            JRPrintServiceExporter exporter = new JRPrintServiceExporter();

            exporter.setExporterInput(new SimpleExporterInput(printer));
            SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration();
            configuration.setPrintRequestAttributeSet(printRequestAttributeSet);
            configuration.setPrintServiceAttributeSet(printServiceAttributeSet);
            exporter.setConfiguration(configuration);
            //true para aprecer opções
            configuration.setDisplayPageDialog(false);
            configuration.setDisplayPrintDialog(false);

            exporter.setConfiguration(configuration);

            System.out.println("Inicializando impressão");  
            exporter.exportReport();  
            System.out.println("Impressão finalizada"); 



        //IMPRIMIR DIRETO
        //JasperPrintManager.printReport(printer,false);
        }


}//fim imprimir

$

现在,我的问题是从远程计算机打印,打印机在本地网络中的另一台计算机上打印。

此代码仅适用于本地计算机:

printServiceAttributeSet.add(new PrinterName(&#34; Argox OS-214 plus series PPLA&#34;,Locale.getDefault()));

答案 1 :(得分:0)

只需添加新的外部参数并在报告中显示此参数。

每次生成报告时,只需为此参数发送新的递增值。