Report always prints in A4 format

1

I'm creating the whole report at runtime, however, at the time of printing it is not respecting the size I set. In%% of it displays right, but the printer or ReportViewer does not print correctly.

Code:

static void Imprimir(int impressora) throws JRException{

    //Criar o Design do relatório
    JasperDesign report = new JasperDesign();
    report.setName("teste");
    report.setPageWidth(90);
    report.setPageHeight(45);
    report.setBottomMargin(0);
    report.setTopMargin(0);
    report.setLeftMargin(0);
    report.setRightMargin(0);
    report.setColumnWidth(89);

    //Criar a banda de detalhes
    JRDesignBand band = new JRDesignBand();
    band.setHeight(45);

    //Criar um campo de texto
    JRDesignStaticText text = new JRDesignStaticText();
    text.setText("Primeira impressão");
    text.setHeight(20);
    text.setWidth(60);
    text.setX(1);
    text.setY(1);
    band.addElement(text);

    //Adicionar a banda de detalhes ao Design deo relatório
    ((JRDesignSection)report.getDetailSection()).addBand(band);

    //Compilar o relatório
    JasperReport relatorio = JasperCompileManager.compileReport(report);

    //Criar o print
    JasperPrint print = JasperFillManager.fillReport(relatorio, new HashMap<>(), new JREmptyDataSource(1));
    print.setPageHeight(report.getPageHeight());
    print.setPageWidth(report.getPageWidth());
    print.setBottomMargin(report.getBottomMargin());
    print.setTopMargin(report.getTopMargin());
    print.setLeftMargin(report.getLeftMargin());
    print.setRightMargin(report.getRightMargin());

    //Recuperar todas as impressoras disponíveis
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

    //Imprimir o relatório na impressora selecionada
    JRExporter exporter = new JRPrintServiceExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[impressora].getAttributes());
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
    exporter.exportReport();

    //Visualizar o relatório para comparar o resultado
    JasperViewer viewer = new JasperViewer(print);
    viewer.setVisible(true);
}

    
asked by anonymous 05.04.2018 / 21:24

1 answer

0

This problem occurred because the page had less than 1cm , to solve, I had to do the calculation to convert mm to px , the formula is this:

  

(X * 72) / 25.4

OBS: 72 is the resolution (amount of pixels per polegada ), JasperReport sets this value to 72.
OBS2: The result of this formula has to be rounded, because JasperReport receives these values in variables of type int . NOTE: X must be the size in mm .

At the end the code looks like this:

//Criar o Design do relatório
JasperDesign report = new JasperDesign();
report.setName("teste");
report.setPageWidth((int)Math.round((90 * 72) / 25.4f)); //9cm
report.setPageHeight((int)Math.round((45 * 72) / 25.4f)); //4.5cm
report.setBottomMargin(0);
report.setTopMargin(0);
report.setLeftMargin(0);
report.setRightMargin(0);
report.setColumnWidth(report.getPageWidth());

//Criar a banda de detalhes
JRDesignBand band = new JRDesignBand();
band.setHeight(report.getPageHeight());

//Criar um campo de texto
JRDesignStaticText text = new JRDesignStaticText();
text.setText("Impressão Teste");
text.setHeight((int)Math.round((10 * 72) / 25.4f));
text.setWidth((int)Math.round((88 * 72) / 25.4f));
text.setX(1);
text.setY(1);
text.setBackcolor(Color.blue);
band.addElement(text);

//Adicionar a banda de detalhes ao Design deo relatório
((JRDesignSection)report.getDetailSection()).addBand(band);

//Compilar o relatório
JasperReport relatorio = JasperCompileManager.compileReport(report);

//Criar o print
JasperPrint print = JasperFillManager.fillReport(relatorio, new HashMap<>(), new JREmptyDataSource(1));
print.setPageHeight(report.getPageHeight());
print.setPageWidth(report.getPageWidth());
print.setBottomMargin(report.getBottomMargin());
print.setTopMargin(report.getTopMargin());
print.setLeftMargin(report.getLeftMargin());
print.setRightMargin(report.getRightMargin());

//Recuperar todas as impressoras disponíveis
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

//Imprimir o relatório na impressora selecionada
JRExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[impressora].getAttributes());
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
exporter.exportReport();

//Visualizar o relatório para comparar o resultado
JasperViewer viewer = new JasperViewer(print);
viewer.setVisible(true);
    
12.04.2018 / 21:12