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);
}