Report always prints in A4 format

Asked

Viewed 26 times

1

I am creating the entire report at runtime, however, at the time of printing it is not respecting the size I set. No ReportViewer it displays right, but in the printer or on PrimoPDF 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);
}

Resultado do <code>ReportViewer</code>

Resultado no PDF

1 answer

0


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

(X * 72) / 25,4

OBS: 72 is the resolution (quantity of pixels for polegada), the JasperReport sets this value at 72.
OBS²: The result of this formula has to be rounded, because the JasperReport receives these values in variables of type int.
OBS³: X has to be the size in mm.

At the end the code went 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.